您的位置:首页 > 其它

CodeForces - 668D Little Artem and Time Machine(线段树||树状数组)

2017-05-05 14:12 375 查看

Little Artem and Time Machine

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:

Add integer to the multiset. Note that the difference between set and multiset is that multiset may store several instances of one integer.

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.

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 moment 3.

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 futu
4000
re 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 ai, ti 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

分析:对每一个节点来说,它的权值为一个集合,所以用map来作权值

注意,线段树要对时间进行离散化(具体详见代码)

代码:

#include<stdio.h>
#include<map>
#include<string.h>
#include<algorithm>
using namespace std;

#define maxn 100010
struct query
{
int op,a,x;
} p[maxn];
struct node
{
int le,ri;
map<int,int>mp;
int mid()
{
return (le+ri)>>1;
}
} tree[maxn<<2];
int pos[maxn];
bool vis[maxn];

void Build(int rt,int le,int ri)
{
tree[rt].le=le;
tree[rt].ri=ri;
tree[rt].mp.clear();
if(le==ri)
return ;
int mid=tree[rt].mid();
Build(rt<<1,le,mid);
Build(rt<<1|1,mid+1,ri);
}

void Update(int rt,int t,int k,int val)
{
if(tree[rt].le>t||tree[rt].ri<t)
return ;
if(tree[rt].le==tree[rt].ri)
{
tree[rt].mp[k]+=val;
return ;
}
int mid=tree[rt].mid();
if(t<=mid)
Update(rt<<1,t,k,val);
if(t>mid)
Update(rt<<1|1,t,k,val);
tree[rt].mp[k]=tree[rt<<1].mp[k]+tree[rt<<1|1].mp[k];

}

int Query(int rt,int t,int k)
{
if(tree[rt].le>t)
return 0;
if(tree[rt].ri<=t)
return tree[rt].mp[k];
return Query(rt<<1,t,k)+Query(rt<<1|1,t,k);
}

int main()
{
int n,tot=0;
scanf("%d",&n);
map<int,bool>q;
for(int i=1; i<=n; ++i)
{
scanf("%d%d%d",&p[i].op,&p[i].a,&p[i].x);
if(!q[p[i].a])
{
q[p[i].a]=true;
pos[++tot]=p[i].a;
}
}
sort(pos+1,pos+tot+1);
Build(1,1,tot);
for(int i=1; i<=n; ++i)
{
int k=lower_bound(pos+1,pos+tot,p[i].a)-pos;
if(p[i].op==1)
Update(1,k,p[i].x,1);
else if(p[i].op==2)
Update(1,k,p[i].x,-1);
else
printf("%d\n",Query(1,k,p[i].x));
}
return 0;
}


这里有一种树状数组的写法,更厉害。

代码:

#include<stdio.h>
#include<map>
using namespace std;

const int maxn=1e6+10;
map<int,int>c[maxn];
map<int,int>vis;

int lowbit(int x)
{
return x&-x;
}

void Update(int rt,int t,int num)
{
for(int i=t;i<1e9+5;i+=lowbit(i))
c[rt][i]+=num;
}

int Query(int rt,int t)
{
int ans=0;
for(int i=t;i;i-=lowbit(i))
ans+=c[rt][i];
return ans;
}

int main()
{
int n;
scanf("%d",&n);
int op,t,x,tot=0;
for(int i=0;i<n;++i)
{
scanf("%d%d%d",&op,&t,&x);
if(op==1&&!vis[x])//对x进行离散化
vis[x]=++tot;
if(op==1)
Update(vis[x],t,1);
else if(op==2)
Update(vis[x],t,-1);
else
printf("%d\n",Query(vis[x],t));
}
return 0;
}


总结:map也可以当权值。。。get了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: