Codeforces669e:little Artem and Time Machine (CDQ division)

Source: Internet
Author: User

Little Artem has invented a time machine! He could go anywhere in time, and all his thoughts of course is with computer. 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 are the 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 no exceptions, so he assumes that every time remove operation are called, that's an integer is pre Sented in the multiset.
    3. Count the number of instances of the given integer that is stored in the multiset.

What is the time machine? Artem doesn ' t simply apply operations to the multiset one by one, he then travels to different moments of time and apply Hi s 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 is there in the multiset at moment 6. The answer is 1.
  • Then Artem returns back in time and asks how many integers 3 is there in the set at Moment 4. Since 3 was added for moment 5, the number of integers 3 at moment 4 equals to 0.
  • Then Artem goes-again and removes 5 from the multiset at Moment 3.
  • Finally Artyom asks at moment 7 how many integers 5 is there in the set. The result is 0, since we had removed 5 at the moment 3.

Note that Artem dislikes exceptions so much that he assures the after each change he makes all delete operations is APPL IED only to element which is present in the multiset. The answer to the third type was computed at the moment Artem makes the corresponding query and was not affect Ed in any-by-future changes he makes.

Help Artem implement time travellers Multiset.

Input

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

Then Follow  n  lines with queries descriptions. Each of them contains three Integers  a i ,   t i  and  x i   (1≤ a i ≤3, 1≤ t I , x i ≤109)  -type of the query, moment Of time Artem travels to an order to execute this query and the value of the query itself, respectively. It's guaranteed that all moments of time be distinct and that after each operation was applied all operations of the first and second types is consistent.

Output

For each ask operation output the number of instances of an 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

Test instructions: Given some actions or inquiries in order. Action: Add elements in the collection, delete the elements, and then have the execution time. or inquire.

Ideas: 3-dimensional partial order, 1th Dimension: given order; second dimension: time; third dimension: size.

The first dimension is already sequenced, the second dimension can be manually sorted, and then the third dimension is done.

Complexity O (NLGNLGN), but it's pretty quick to run.

#include <bits/stdc++.h>using namespacestd;Const intmaxn=100010;intB[maxn],ans[maxn],num[maxn],cnt,tn;struct inch{intid,opt,t,x; }S[MAXN];BOOLCMP1 (inchWinchV) {returnw.t<v.t;}BOOLCMP2 (inchWinchV) {returnw.id<v.id;}voidSolveintLintR) {    if(L==R)return ; intMid= (L+R)/2;     Solve (L,mid); Solve (Mid+1, R); Sort (S+l,s+r+1, CMP1);  for(inti=l;i<=r;i++){        if(s[i].id<=Mid) {           intPos=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 {            intPos=lower_bound (b +1, b+tn+1, s[i].x)-b; if(s[i].opt==3) ans[s[i].id]+=Num[pos]; }    }     for(inti=l;i<=r;i++){        if(s[i].id<=Mid) {           intPos=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);}intMain () {intn,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 division)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.