P2184 greedy Mainland topic background to face the crazy attack of ants, small FF tower defence declared failure ... Humans were driven to a bay on the island of greed by ants. Now, the rear of the small FF is the endless sea, the front is a mutant super Ant. Small FF also has a good future, he does not want to die, so he sent his last batch of reform SCV layout mines to stop ants attack. Title Description
The last line of defense of the small FF is a trench of length n, with small FF having countless mines, and the SCV can bury the same type of mine in the [L, R] zone at a time different from the one previously planted. Due to the urgency of the situation, the small FF may at some point ask you how many different mines are in the [L ', R '] zone, and he would like you to reply as soon as possible.
For 30% data: 0<=n, m<=1000;
For 100% data: 0<=n, m<=10^5.
Input output Format input format:
The first behavior is two integers n and m; n represents the line length, and M represents the sum of the number of SCV mines and the number of small FF queries.
Next there are m lines, three integers per line q,l, R; if q=1 indicates that the SCV has a mine on [L, R] This interval, if q=2 indicates how many mines are in the current [L, R] interval.
Output format:
For each query of the small FF, output an answer (a single line) that represents the total number of mines in the current interval.
Input and Output Sample input example # #:
5 41 1 32 2 51 2 42 3 5
Sample # # of output:
12
Exercises
When you see this topic, there is a wrong way of thinking, thought is a simple interval summation, but after careful study found that is not the case. It is not the maximum value or the output of the interval, because sometimes it is possible for two adjacent intervals to be placed in the item, which will default to one, so there is an error. So I started looking for ideas (thanks to the web).
{A similar prefix and the idea}
We can insert the left and right end points of each interval into different trees, the number of items in an interval, in fact, the number of the left end of the 1~r minus 1~ (L-1) in the number of endpoints, sorting out: ans=tot-(l[r. N]+R[1..L-1]) {tot= Total number of bombs released}
That is, the tot-in the Code (QUERY1 (n)-query1 (y) +query2 (x-1)), the output can be
Tree-like array version AC code:
#include <cstdio>using namespacestd;Const intn=1e5+Ten;intn,m,tot,tr[n][2];inlineintLowbit (intx) { returnx&-x;} InlinevoidUpdata1 (intPintv) { for(intI=p;i<=n;i+=lowbit (i)) tr[i][0]+=v; }inlineintQuery1 (intp) { intans=0; for(inti=p;i>=1; I-=lowbit (i)) ans+=tr[i][0]; returnans;} InlinevoidUPDATA2 (intPintv) { for(intI=p;i<=n;i+=lowbit (i)) tr[i][1]+=v; }inlineintQuery2 (intp) { intans=0; for(inti=p;i>=1; I-=lowbit (i)) ans+=tr[i][1]; returnans;}intMain () {scanf ("%d%d",&n,&m); for(intI=1, x,y,z;i<=m;i++) {scanf ("%d%d%d",&z,&x,&y); if(z==1) Updata1 (x,1), Updata2 (Y,1), tot++; Elseprintf"%d\n", tot-(Query1 (n)-query1 (y) +query2 (X-1))); } return 0;}
P2184 Greedy Continent