"BZOJ4285" Messenger CDQ Division + scan Line + tree-like array

Source: Internet
Author: User

"BZOJ4285" Messenger description A.D. 8192, human entered the era of interstellar navigation. Under unremitting efforts, humans have captured n planets in the universe and built n-1 Interstellar lanes between these planets, allowing any two planets to reach each other through a single path. At the same time, there are some space jumping points in the universe, some jumping points have been found, and some are unknown, each jumping point connected to two planets, so that either of the two planets can be reached through this jumping point to another planet. These jumps are prone to collapse because they are filled with huge energy. There are also aliens in the universe who intend to destroy humans, who send out messengers lurking on the planets. Now the messengers want to move away from the planet they are hiding from u, to other planet V to gather intelligence. As these messengers are very careful, they do not pass through any of the interstellar lanes on the path that belong to the two planets (that is, the interstellar fairway that does not walk on the U-v path). So that they can only use some jumping points to reach their destination, but these aliens are very vulnerable to the body, so they can only pass at most one jumping point. Now tell you a number of events given in chronological order, each of which may be a jumping point being discovered, a jumping point crashing, and possibly an extraterrestrial messenger wanting to leave planet U to Planet v. How many different paths can I choose for each alien messenger? Input first line a positive integer n. The next n-1 line is two integers per line u, V, which represents an interstellar channel connecting Planet U with Planet v. The next line is a positive integer m, indicating the number of hops that have been discovered. Next m line two integers per line s, t, represents a jumping point connecting the planet S with the planet T. The next line is a positive integer q, which represents the number of events. The next Q line behaves as one of the following three events: "1 x Y": Indicates that there is a jumping point connecting Planet X with Planet Y was found; "2 x Y": Indicates that there is a jumping point connecting Planet X to Planet Y crashes (guaranteed to have such a jumping point); "3 x Y": Indicates that an extraterrestrial messenger wanted to Planet X to Planet Y to gather intelligence. Output is an integer for each extraterrestrial messenger, indicating the number of different path bars that the extraterrestrial Messenger can choose. Sample Input13
1 2
1 3
1 4
2 5
5 9
5 10
5 11
10 13
3 6
4 7
4 8
7 12
6
2 4
10 12
9 8
6 7
3 11
7 10
5
1 1 5
3 5 4
2 7 10
2 10 12
3 5 4Sample Output3
1Hint for 100% data, the N≤10^5,M≤5X10^4,Q≤5X10^4 data guarantees that x is not equal to Y

The problem: it is also mental pollution.

We see each jumping point as a point on the two-dimensional plane in the DFS sequence, and ask you to look at the number of rectangles in a rectangle, so you can use the tree-like array + scan line. But what about a little bit of deletion? Set on the CDQ division can be.

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const int Maxn=100010;int N,m,q,cnt,np,nq,now;int TO[MAXN&LT;&LT;1],NEXT[MAXN&LT;&LT;1],HEAD[MAXN],FA[20][MAXN ],log[maxn],dep[maxn],siz[maxn],p1[maxn],p2[maxn];int s[maxn],tim[maxn],op[maxn],ans[maxn];struct Point{int x,y,k , Org;point () {}point (int a,int b,int c,int d) {x=a,y=b,k=c,org=d;}} P[maxn];struct Query{int l,r,x,k,org; QUERY () {}query (int a,int b,int c,int d,int e) {l=a,r=b,x=c,k=d,org=e;}} q[maxn<<1];inline void Add (int a,int b) {to[cnt]=b,next[cnt]=head[a],head[a]=cnt++;} void Dfs (int x) {siz[x]=1,p1[x]=++p2[0];for (int i=head[x];i!=-1;i=next[i]) if (to[i]!=fa[0][x]) fa[0][to[i]]=x,dep[to [I]] =dep[x]+1,dfs (To[i]), Siz[x]+=siz[to[i]];p 2[x]=p2[0];} inline int FA (int x,int y) {for (int i=log[y];i>=0;i--) if (y>= (1<<i)) y-= (1<<i), X=fa[i][x];return x;} BOOL Cmpx (const point &a,const point &b) {return a.x<b.x;} BOOL Cmqx (const query &a,const query&AMP;B) {return a.x<b.x;} inline void updata (int x,int v) {for (int i=x;i<=n;i+=i&-i) {if (Tim[i]<now) tim[i]=now,s[i]=0;s[i]+=v;}} inline int query (int x) {int ret=0,i;for (i=x;i;i-=i&-i) {if (Tim[i]<now) tim[i]=now,s[i]=0;ret+=s[i];} return ret;} void Solve (int l,int r,int l,int r) {if (l==r) {for (int i=l;i<=r;i++) if (p[l].org<q[i].org&&p[l].x<=q[i ].X&AMP;&AMP;P[L].Y&GT;=Q[I].L&AMP;&AMP;P[L].Y&LT;=Q[I].R) Ans[q[i].org]+=p[l].k*q[i].k;return;} if (l>r) return, int mid= (l+r) >>1,mid,i,j;for (i=l;i<=r;i++) if (q[i].org>p[mid].org) break; Mid=i-1;solve (L,mid,l,mid), Solve (mid+1,r,mid+1,r), sort (p+l,p+mid+1,cmpx), sort (Q+MID+1,Q+R+1,CMQX), Now++;for (i= mid+1,j=l;i<=r;i++) {for (; j<=mid&&p[j].x<=q[i].x;j++) Updata (P[J].Y,P[J].K); ans[q[i].org]+= ( Query (Q[I].R)-query (Q[I].L-1)) *q[i].k;}} inline Int rd () {int Ret=0,f=1;char gc=getchar (); while (gc< ' 0 ' | | Gc> ' 9 ') {if (gc== '-') F=-f;gc=getchar ();} while (gc>= ' 0 ' &&gc<= ' 9 ') ret=ret*10+ (gc^ ' 0 '), Gc=getchar ();return ret*f;} int main () {n=rd (); int I,j,a,b,c;memset (head,-1,sizeof (head)), for (i=1;i<n;i++) a=rd (), B=rd (), add (A, B), add (B,a); DFS (1); for (i=2;i<=n;i++) log[i]=log[i>>1]+1;for (j=1; (1<<j) <=n;j++) for (i=1;i<=n;i++) fa[j][i ]=fa[j-1][fa[j-1][i]];m=rd (); for (i=1;i<=m;i++) {a=rd (), B=rd (), if (P1[a]>p1[b]) swap (A, b);p [++np]=point (P1[a ],p1[b],1,0);} Q=rd (); for (i=1;i<=q;i++) {op[i]=rd (), A=rd (), B=rd (), if (P1[a]>p1[b]) swap (A, B), if (op[i]==1) P[++np]=point (p1[ A],p1[b],1,i), if (op[i]==2) P[++np]=point (p1[a],p1[b],-1,i), if (op[i]==3) {if (P1[b]<=p2[a]) {C=FA (b,dep[b]-dep[a ]-1); Q[++nq]=query (p1[b],p2[b],p1[c]-1,1,i); Q[++nq]=query (P2[c]+1,n,p1[b]-1,-1,i), Q[++nq]=query (p2[c]+1,n,p2[b ],1,i);} Elseq[++nq]=query (P1[b],p2[b],p1[a]-1,-1,i), Q[++nq]=query (P1[b],p2[b],p2[a],1,i);}} Solve (1,NP,1,NQ); for (i=1;i<=q;i++) if (op[i]==3) printf ("%d\n", Ans[i]); return 0;}

"BZOJ4285" Messenger CDQ Division + scan Line + tree-like array

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.