Bzoj4154--ipsc2015 Generating Synergy

Source: Internet
Author: User
Tags getcolor

Test instructions: AC Channel
Exercises
There is a very ingenious solution to this problem.
Because there are sub-tree modifications, but not the whole subtrees tree modification, directly on the DFS sequence is not possible.
Considering that it is only a child node each time it is modified, and there is a distance limit, we think that this is a problem with the depth of the point.
So we take the point of the tree to the two-dimensional plane, the horizontal axis for its DFS sequence, the ordinate for its depth, so that each time the modification corresponds to a region of the dyeing operation, this thing with Kdtree can casually engage, get a lazytag on it.

A bunch of debugging code, so it looks very long.

#include <bits/stdc++.h>//#include <conio.h>UsingnamespaceStd;typedef Long Long ll;typedef double db;constintinf=0x3f3f3f3f;intGetint () {intf=1, g=0; Char C=getchar (); while(c<' 0 '|| C>' 9 '){if(c=='-') f=-1; C=getchar ();} while(c>=' 0 '&& c<=' 9 ') g= (g<<3) + (g<<1) +c-' 0 ', C=getchar ();returnF*g;} Constintmaxn=100005; constintMod=1000000007;intDimintN,m;struct node{intd[2];int Max[2];int min[2];intL,r;intTagint Color;int& operator [] (const unsignedint& num) {returnD[num]; } BOOL operator < (const node& NO) const {returnd[dim]<no.d[dim]; } bool operator = = (const node& NO) const {returnd[0]==no.d[0] && d[1]==no.d[1]; }};node P[MAXN];intRoot;struct kdtree{node T[MAXN]; #define LC T[X].L #define RC T[X].R #definelsT[T[X].L] #define RS t[t[x].r] void update (intx) {t[x].min[0]=T[X].Max[0]=t[x][0]; T[X].min[1]=T[X].Max[1]=t[x][1];if(LC) {T[x].min[0]=min(T[x].min[0],ls.min[0]); T[X].Max[0]=Max(T[x].Max[0],ls.Max[0]); T[X].min[1]=min(T[x].min[1],ls.min[1]); T[X].Max[1]=Max(T[x].Max[1],ls.Max[1]); }if(RC) {t[x].min[0]=min(T[x].min[0],rs.min[0]); T[X].Max[0]=Max(T[x].Max[0],rs.Max[0]); T[X].min[1]=min(T[x].min[1],rs.min[1]); T[X].Max[1]=Max(T[x].Max[1],rs.Max[1]); }} void Push_down (intx) {if(!x | |!t[x].tag)return; T[X].Color=t[x].tag;ls. Tag=rs.tag=t[x].tag; t[x].tag=0; } #define MID (l+r>>1)intBuildintLintRintNow) {Dim=now; Nth_element (p+l,p+mid,p+r+1);intX=mid; T[X].Color=1; for(intI=0;i<2; i++) {t[x].min[I]=t[x].Max[I]=t[x][i]=p[x][i]; }if(L<mid) Lc=build (l,mid-1, now^1);if(R>mid) Rc=build (mid+1, r,now^1); Update (x);returnX } #undef mid node t1,t2;intCol void SetColor (intx) {if(!x)return; Push_down (x);if(T[x].min[0]>t2[0] || T[X].Max[0]<t1[0] || T[X].min[1]>t2[1] || T[X].Max[1]<t1[1])return;if(T[x].min[0]>=t1[0] && t[x].Max[0]<=t2[0] && t[x].min[1]>=t1[1] && t[x].Max[1]<=t2[1]) {t[x].tag=col;return; }if(t[x][0]>=t1[0] && t[x][0]<=t2[0] && t[x][1]>=t1[1] && t[x][1]<=t2[1]) t[x].Color=col;    SetColor (LC); SetColor (RC); } node T;intGetColorintx) {Push_down (x);if(t[0]&LT;T[X].min[0] || t[0]&GT;T[X].Max[0] || t[1]&LT;T[X].min[1] || t[1]&GT;T[X].Max[1])return 0;if(t[x]==t)returnT[X].Color;returnGetColor (LC) +getcolor (RC); } void Set (intXintYintXxintYyintCO) {t1[0]=x; t1[1]=y; t2[0]=xx; t2[1]=yy;        Col=co;    SetColor (root); }intQueryintXintY) {t[0]=x; t[1]=y;returnGetColor (root); } void Init () {memset (T,0, sizeof T); Memset (P,0, sizeof P); root=0; } Void Run (intx) {if(!x)return;        Run (LC); printf( %d,%d), (%d,%d):%d (%d) (%d,%d) \ n ", T[x].min[0],T[X].min[1],T[X].Max[0],T[X].Max[1],T[X].Color, t[x].tag,t[x][0],t[x][1]);    Run (RC); #undef LC #undef RC #undefls#undef Rs}kdtree;Vector<int> G[maxn];void Addedge (intFromintTo) {g[from].push_back (to); G[to].push_back (from);}intDEP[MAXN];intST[MAXN];intED[MAXN];intIndintFather[maxn];void DFS (intx) {st[x]=++ind; for(intI=0; I<g[x].size(); i++) {intTo=g[x][i];if(father[x]==to)Continue; dep[to]=dep[x]+1;    DFS (to); } Ed[x]=ind;}intMain () {//Freopen ("In.txt", "R", stdin);    intT=getint (); while(t--) { for(intI=0; i<maxn;i++) G[i].Clear();        Kdtree.init (); Ind=0; N=getint ();intTemp=getint (); M=getint (); for(intI=2; i<=n;i++) {father[i]=getint ();        Addedge (I,father[i]); } dep[1]=1; Dfs1);/* for (int i=1;i<=n;i++) {printf ("%d", st[i]);        } puts ("");        for (int i=1;i<=n;i++) {printf ("%d", ed[i]);        } puts ("");        for (int i=1;i<=n;i++) {printf ("%d", dep[i]);        } puts (""); */         for(intI=1; i<=n;i++) {p[i][0]=st[i]; p[i][1]=dep[i]; } root=kdtree.build (1N0);/* Kdtree.run (root);            Puts ("");        Getch (); */        intx, y;intOpt ll ans=0; for(intI=1; i<=m;i++) {x=getint ();            Y=getint (); Opt=getint ();if(!opt) {ll t=kdtree.query (st[x],dep[x]);//printf ("%d\n", t);ans+= (LL) t*(ll) I; Ans%=MoD }Else{/ * printf ("We are painting (%d,%d), (%d,%d):%d\n", st[x],dep[x],ed[x],dep[x]+y,opt);                Getch (); */Kdtree.set (st[x],dep[x],ed[x],dep[x]+y,opt); }/* Kdtree.run (root);            Puts ("");            Getch (); */} printf ("%d\n", ans); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bzoj4154--ipsc2015 Generating Synergy

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.