Topic Links:
E. Little Artem and Time Machine
Time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output
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:
- Add integer to the multiset. Note that the difference between set and Multiset are the 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 no exceptions, so he assumes that every time remove operation are called, that's an integer is pre Sented in the multiset.
- 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 moment3.
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 single 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 ≤10 9) -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.
ExamplesInput
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
There's a time machine that can go to different times to change the number of values, 1 is to increase 1, 2 is minus 1, 3 is asking;
Ideas:
A look is a line tree, blue and the range of time and value is too large, time can be discretized, the number of words then the node of the line tree into map rage godless, I just open the brain hole and then write to play, Blue after accidentally godless;
hahaha, the recent brain hole is too big, often messed up a problem, I can not stand it;
AC Code:
/*2014300227 669E-33 GNU c++11 Accepted 1091 Ms 78840 KB*/#include<bits/stdc++.h>using namespaceStd;typedefLong Longll;Constll mod=1e9+7;Constll inf=1e15;Const intn=1e5+6;intN;structpo{intA,t,va,pos;} Po[n];intcmp1 (PO X,po y) {returnx.t<y.t;}intcmp2 (PO X,po y) {returnx.pos<Y.pos;}structtree{intL,r; Map<int,int>MP1,MP2;}; Tree tree[4*N];voidBuildintNodeintLintR) {TREE[NODE].L=L; TREE[NODE].R=R; if(L==R)return ; intMid= (l+r) >>1; Build (2*node,l,mid); Build (2*node+1, mid+1, R);}voidUpdateintNodeintPosintNumintflag) { if(tree[node].l==tree[node].r&&tree[node].l==POS) { if(flag==1) Tree[node].mp1[num]++; Elsetree[node].mp2[num]++; return ; } intMid= (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]++; Elsetree[node].mp2[num]++;}intQueryintNodeintLintRintNumintflag) { if(l<=tree[node].l&&r>=TREE[NODE].R) { if(flag==1)returnTree[node].mp1[num]; Else returnTree[node].mp2[num]; } intMid= (TREE[NODE].L+TREE[NODE].R) >>1; if(r<=mid) {returnQuery2*Node,l,r,num,flag); } Else if(l>mid) {returnQuery2*node+1, L,r,num,flag); } Else { returnQuery2*node,l,mid,num,flag) +query (2*node+1, mid+1, R,num,flag); }}intMain () {scanf ("%d",&N); for(intI=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(intI=1; i<=n;i++) {po[i].t=i; } Sort (PO+1, po+n+1, CMP2); Build (1,1, N); for(intI=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\n", Query (1,1, Po[i].t,po[i].va,1)-query (1,1, Po[i].t,po[i].va,2)); } } return 0;}
Codeforces 669E E. Little Artem and Time Machine (node is a map-type segment tree)