Describe
There are many trees outside the school gate, there are apple trees, banana trees, there will throw stones, there can be eaten to replenish the physical ...
Now the school decides to plant a tree at some point in a certain period, to ensure that no two segments of the same tree are present at any one time, there are two operations:
k=1, read into l,r represents a tree planted between the l~r
k=2, read into L,r to ask how many kinds of trees can be seen between L~r
(l,r>0)
Input Format
The first line of N,M indicates that the total road length is n, with a total of M operations
next m behavior m operations
output Format
for each k=2 output an answer
Notes
Range: 20% data guarantee, n,m<=100
60% Data Guarantee, n <=1000,m<=50000
100% Data Guarantee, n,m<=50000
Note: Trees can overlap, for example, 1th can be planted in a variety of trees
Analysis: The problem can be done, the establishment of two line segment tree, respectively maintained this point before there are several left parenthesis and this point after a few right parenthesis, so you can do it.
Code:
#include <iostream> #include <cstdio>using namespacestd;structnode{intS,t,l,r;} tr[800001];intn,q;inta[200001];intRead () {CharC=GetChar (); intA=0; while(c<'0'|| C>'9') c=GetChar (); while(c>='0'&&c<='9') {a=a*Ten+c-'0'; C=GetChar (); } returnA;}voidBuildintKintXinty) {TR[K].S=x; tr[k].t=y; if(x==y) {tr[k].l=tr[k].r=0;return; } intMid= (x+y) >>1; Build (k<<1, X,mid); Build (k<<1|1, mid+1, y); return;}voidInsertl (intNowintXinty) { intL=tr[now].s,r=tr[now].t; //if (Y if(X==l && y==r) {TR[NOW].L+=1; return; } intMid= (l+r) >>1; if(X>mid) Insertl (now<<1|1, x, y); Else if(Y<=mid) Insertl (now<<1, x, y); Else{Insertl (now<<1, X,mid); Insertl (now<<1|1, mid+1, y); } return;}voidINSERTR (intNowintXinty) { intL=tr[now].s,r=tr[now].t; //if (Y if(X==l && y==r) {TR[NOW].R+=1; return; } intMid= (l+r) >>1; if(X>mid) Insertr (now<<1|1, x, y); Else if(Y<=mid) Insertr (now<<1, x, y); Else{INSERTR (now<<1, X,mid); INSERTR (now<<1|1, mid+1, y); } return;}intFindl (intKintx) { intL=tr[k].s,r=tr[k].t; //if (Y if(L==R)returnTR[K].L; intMid= (l+r) >>1; if(X<=mid)returnTr[k].l+findl (k<<1, x); Else returnTr[k].l+findl (k<<1|1, x); }intFindr (intKintx) { intL=tr[k].l,r=TR[K].R; if(L==R)returnTR[K].R; intMid= (l+r) >>1; if(X<=mid)returnTr[k].r+findr (k<<1, x); Else returnTr[k].r+findr (k<<1|1, x);}intMain () {intTotal=0; N=read (); Build (1,0, N); Q=read (); for(intI=1; i<=q; i++) { intnow=read (); intX=read (), y=read (); if(now==1) {Insertl (1,0, X-1); INSERTR (1, y+1, N); Total++; } Else if(now==2) { intAns1=findr (1, x); intAns2=findl (1, y); intans=ans1+Ans2; //cout << ans1 << ' << ans2 << ' hh ' << Endl;printf"%lld\n", total-ans); } } return 0;}
Back | Application of tree 3| segment tree outside school gate