codeforces 669E E. Little Artem and Time Machine(节点为map型的线段树)

题目链接:

E. Little Artem and Time Machine

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Artem has invented a time machine! He could go anywhere in time, but all his thoughts of course are with computer science. He wants to apply this time machine to a well-known data structure: multiset.

Artem wants to create a basic multiset of integers. He wants these structure to support operations of three types:

  1. Add integer to the multiset. Note that the difference between set and multiset is that multiset may store several instances of one integer.
  2. Remove integer from the multiset. Only one instance of this integer is removed. Artem doesn't want to handle any exceptions, so he assumes that every time remove operation is called, that integer is presented in the multiset.
  3. Count the number of instances of the given integer that are stored in the multiset.

But what about time machine? Artem doesn't simply apply operations to the multiset one by one, he now travels to different moments of time and apply his operation there. Consider the following example.

  • First Artem adds integer 5 to the multiset at the 1-st moment of time.
  • Then Artem adds integer 3 to the multiset at the moment 5.
  • Then Artem asks how many 5 are there in the multiset at moment 6. The answer is 1.
  • Then Artem returns back in time and asks how many integers 3 are there in the set at moment 4. Since 3 was added only at moment 5, the number of integers 3 at moment 4 equals to 0.
  • Then Artem goes back in time again and removes 5 from the multiset at moment 3.
  • Finally Artyom asks at moment 7 how many integers 5 are there in the set. The result is 0, since we have removed 5 at the moment3.

Note that Artem dislikes exceptions so much that he assures that after each change he makes all delete operations are applied only to element that is present in the multiset. The answer to the query of the third type is computed at the moment Artem makes the corresponding query and are not affected in any way by future changes he makes.

Help Artem implement time travellers multiset.

Input
 

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of Artem's queries.

Then follow n lines with queries descriptions. Each of them contains three integers aiti and xi (1 ≤ ai ≤ 3, 1 ≤ ti, xi ≤ 109) — type of the query, moment of time Artem travels to in order to execute this query and the value of the query itself, respectively. It's guaranteed that all moments of time are distinct and that after each operation is applied all operations of the first and second types are consistent.

Output
 

For each ask operation output the number of instances of integer being queried at the given moment of time.

Examples
 
input
6
1 1 5
3 5 5
1 2 5
3 6 5
2 3 5
3 7 5
output
1
2
1
input
3
1 1 1
2 2 1
3 3 1
output
0


题意:

就是有一个时光机可以去不同的时刻对一些数值的数目进行修改;1是增加1,2是减1,3是询问;


思路:

一看就是个线段树,蓝而时间和数值的范围太大,时间可以进行离散化,数值的话那么就把线段树的节点变成map怒怼一发,我就是开开脑洞然后写写玩玩,蓝后一不小心怼过了;
哈哈哈,最近脑洞太大,经常乱搞来A题;我自己都受不了了;


AC代码:

/*2014300227    669E - 33    GNU C++11    Accepted    1091 ms    78840 KB*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const ll inf=1e15;
const int N=1e5+6;
int n;
struct PO
{
    int a,t,va,pos;
}po[N];
int cmp1(PO x,PO y)
{
    return x.t<y.t;
}
int cmp2(PO x,PO y)
{
    return x.pos<y.pos;
}
struct Tree
{
    int l,r;
    map<int,int>mp1,mp2;
};
Tree tree[4*N];
void build(int node,int L,int R)
{
    tree[node].l=L;
    tree[node].r=R;
    if(L==R)return ;
    int mid=(L+R)>>1;
    build(2*node,L,mid);
    build(2*node+1,mid+1,R);
}
void update(int node,int pos,int num,int flag)
{
    if(tree[node].l==tree[node].r&&tree[node].l==pos)
    {
        if(flag==1)
        tree[node].mp1[num]++;
        else tree[node].mp2[num]++;
        return ;
    }
    int mid=(tree[node].l+tree[node].r)>>1;
    if(pos<=mid)
    {
        update(2*node,pos,num,flag);
    }
    else
    {
        update(2*node+1,pos,num,flag);
    }
    if(flag==1)tree[node].mp1[num]++;
    else tree[node].mp2[num]++;
}
int query(int node,int L,int R,int num,int flag)
{
    if(L<=tree[node].l&&R>=tree[node].r)
    {
        if(flag==1)return tree[node].mp1[num];
        else return tree[node].mp2[num];
    }
    int mid=(tree[node].l+tree[node].r)>>1;
    if(R<=mid)
    {
        return query(2*node,L,R,num,flag);
    }
    else if(L>mid)
    {
        return query(2*node+1,L,R,num,flag);
    }
    else
    {
        return query(2*node,L,mid,num,flag)+query(2*node+1,mid+1,R,num,flag);
    }
}
int main()
{

    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d",&po[i].a,&po[i].t,&po[i].va);
        po[i].pos=i;
    }
    sort(po+1,po+n+1,cmp1);
    for(int i=1;i<=n;i++)
    {
        po[i].t=i;
    }
    sort(po+1,po+n+1,cmp2);
    build(1,1,n);
    for(int i=1;i<=n;i++)
    {
        if(po[i].a==1)
        {
            update(1,po[i].t,po[i].va,1);
        }
        else if(po[i].a==2)
        {
            update(1,po[i].t,po[i].va,2);
        }
        else
        {

            //cout<<"#"<<endl;
            printf("%d
",query(1,1,po[i].t,po[i].va,1)-query(1,1,po[i].t,po[i].va,2));
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zhangchengc919/p/5433262.html