CodeForces669E:Little Artem and Time Machine(CDQ分治)

Posted ---学习ing---

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces669E:Little Artem and Time Machine(CDQ分治)相关的知识,希望对你有一定的参考价值。

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 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 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

题意:按顺序给定一些操作或者询问。操作:在集合里加元素,删元素,然后有执行时间。 或询问。 

思路:3维偏序,第1维:给出的顺序; 第二维:时间; 第三维:大小。

第一维已经排好序了,第二维也可以手动排序,然后搞定第三维。

复杂度O(NlgNlgN),不过跑起来还挺快的。

#include<bits/stdc++.h>
using namespace std;
const int maxn=100010;
int b[maxn],ans[maxn],num[maxn],cnt,tN;
struct in{  int id,opt,t,x;  }s[maxn];
bool cmp1(in w,in v){ return w.t<v.t ;}
bool cmp2(in w,in v){ return w.id<v.id; }
void solve(int L,int R)
{
    if(L==R) return ;
    int Mid=(L+R)/2;
    solve(L,Mid); 
    solve(Mid+1,R);
    sort(s+L,s+R+1,cmp1);
    for(int i=L;i<=R;i++){
        if(s[i].id<=Mid){
           int pos=lower_bound(b+1,b+tN+1,s[i].x)-b;
           if(s[i].opt==1) num[pos]++;
           if(s[i].opt==2) num[pos]--;
        }
        else {
            int pos=lower_bound(b+1,b+tN+1,s[i].x)-b;
            if(s[i].opt==3) ans[s[i].id]+=num[pos];
        }
    }
    for(int i=L;i<=R;i++){
        if(s[i].id<=Mid){
           int pos=lower_bound(b+1,b+tN+1,s[i].x)-b;
           if(s[i].opt==1) num[pos]--;
           if(s[i].opt==2) num[pos]++;
        }
    }
    sort(s+L,s+R+1,cmp2);
}
int main()
{
    int N,i,j;
    scanf("%d",&N);
    for(i=1;i<=N;i++) scanf("%d%d%d",&s[i].opt,&s[i].t,&s[i].x);
    for(i=1;i<=N;i++) s[i].id=i, b[i]=s[i].x;
    sort(b+1,b+N+1);
    tN=unique(b+1,b+N+1)-(b+1);
    solve(1,N);
    for(i=1;i<=N;i++)  if(s[i].opt==3) printf("%d\n",ans[i]);
    return 0;
}

 

以上是关于CodeForces669E:Little Artem and Time Machine(CDQ分治)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 220B - Little Elephant and Array 离线树状数组

[CodeForces-259C] Little Elephant and Bits

『题解』Codeforces220B Little Elephant and Array

Little Girl and Maximum XOR CodeForces - 276D

Little Girl and Maximum Sum CodeForces - 276C

CodeForces - 258D Little Elephant and Broken Sorting