Bzoj 2243: [SDOI2011] staining [tree-chain split]

Source: Internet
Author: User

2243: [SDOI2011] dyeing time limit:20 Sec Memory limit:512 MB
submit:6651 solved:2432
[Submit] [Status] [Discuss] Description

Given a root tree and M operations with N nodes , there are 2 classes of operations:

1, the node A to node B path on all points are dyed color c;

2. Ask node A to the number of color segments on the path of Node B (continuous same color is considered the same paragraph), such as "112221" consists of 3 segments: "One", "222" and "1" .

Please write a program to complete the m operation in turn.

Input

The first line contains 2 integers N and m, each representing the number of nodes and operands;

The second row contains n positive integers representing the initial color of n nodes

below each row of rows contains two integers x and y, indicating that there is an no-forward edge between X and Y.

below each row of rows describes an action:

"C A B C" means that this is a dyeing operation, the node A to node B path on all points (including A and b) are dyed color C;

"Q a B" indicates that this is a query operation asking for the number of color segments on Node A to Node B (including A and b) paths.

Output

For each query operation, output one line of answers.

Sample Input6 5

2 2 1 2 1 1

1 2

1 3

2 4

2 5

2 6

Q 3 5

C 2 1 1

Q 3 5

C 5 1 2

Q 3 5

Sample Output3

1

2
HINT

Number n<=10^5, operand m<=10^5, all color c is an integer and between [0, 10^9].

Source

First round Day1

Bare Tree chain split

That's stupid, I said, all WA. Finally, the node allocation color of the segment tree is mistaken.

Line tree is normal color coverage, note that two heavy chain between the same color

With that ordinary mark-pruning, Bzoj rank16.

////main.cpp//sdoi2011 Staining////Created by Candy on 2016/12/14.//copyright©2016 year Candy. All rights reserved.//#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;#defineLC O<<1#defineRC O<<1|1#defineM ((l+r) >>1)#defineLson o<<1,l,m#defineRson O<<1|1,m+1,rConst intn=1e5+5;intRead () {CharC=getchar ();intx=0, f=1;  while(c<'0'|| C>'9'){if(c=='-') f=-1; C=GetChar ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; C=GetChar ();} returnx*F;}intn,q,ww[n],w[n],x,y,a,b,c;Chars[Ten];structedge{intV,ne;} E[n<<1];intH[n],cnt;inlinevoidInsintUintv) {CNT++; E[CNT].V=v;e[cnt].ne=h[u];h[u]=CNT; CNT++; E[CNT].V=u;e[cnt].ne=h[v];h[v]=CNT;}intTop[n],size[n],tid[n],tot,fa[n],deep[n],mx[n];voidDfsintu) {Size[u]++;  for(intI=h[u];i;i=e[i].ne) {        intv=e[i].v; if(V==fa[u])Continue; FA[V]=u;deep[v]=deep[u]+1;        DFS (v); Size[u]+=Size[v]; if(Size[v]>size[mx[u]]) mx[u]=v; }}voidDfsintUintANC) {    if(!u)return; Tid[u]=++tot;top[u]=ANC;    DFS (MX[U],ANC);  for(intI=h[u];i;i=e[i].ne)if(e[i].v!=fa[u]&&e[i].v!=Mx[u]) DFS (E[I].V,E[I].V);}structnode{intLv,rv,cnt,tag;} T[n<<2];inlinevoidMergeinto) {T[o].tag=-1; t[o].lv=t[lc].lv; T[O].RV=T[RC].RV; T[o].cnt=t[lc].cnt+t[rc].cnt-(t[lc].rv==t[rc].lv);} InlinevoidPaintintOintv) {T[o].tag=t[o].lv=t[o].rv=W; T[o].cnt=1;}voidBuildintOintLintR) {    if(l==r) Paint (O,w[l]); Else{build (Lson);        Build (Rson);    Merge (o); }}inlinevoidPushdown (into) {    if(t[o].tag!=-1) {paint (Lc,t[o].tag);        Paint (Rc,t[o].tag); T[o].tag=-1; }}voidChangeintOintLintRintQlintQrintv) {    if(ql<=l&&r<=qr) paint (o,v); Else{pushdown (o); if(ql<=m) Change (LSON,QL,QR,V); if(m<qr) change (rson,ql,qr,v);    Merge (o); }}intQueryintOintLintRintQlintqr) {    if(QL&LT;=L&AMP;&AMP;R&LT;=QR)returnt[o].cnt; Else if(t[o].tag!=-1)return 1; Else{pushdown (o); intans=0; if(ql<=m) ans+=query (LSON,QL,QR); if(M&LT;QR) ans+=query (RSON,QL,QR); if(ql<=m&&m<qr&&t[lc].rv==t[rc].lv) ans--; returnans; }}intGetcol (intOintLintRintp) {    if(L==R)returnt[o].lv; Else if(t[o].tag!=-1)returnT[o].tag; Else{        if(p<=m)returnGetcol (lson,p); Else returnGetcol (rson,p); }}voidSol1 (intXintYintc) {     while(top[x]!=Top[y]) {        if(deep[top[x]]<Deep[top[y]]) swap (x, y); Change (1,1, N,tid[top[x]],tid[x],c); X=Fa[top[x]]; }    if(tid[x]>Tid[y]) swap (x, y); Change (1,1, n,tid[x],tid[y],c);}intSol2 (intXinty) {    intans=0;  while(top[x]!=Top[y]) {        if(deep[top[x]]<Deep[top[y]]) swap (x, y); Ans+=query (1,1, n,tid[top[x]],tid[x]); if(Getcol (1,1, N,tid[top[x]]) ==getcol (1,1, N,tid[fa[top[x]]) ans--; X=Fa[top[x]]; }    if(tid[x]>Tid[y]) swap (x, y); Ans+=query (1,1, N,tid[x],tid[y]); returnans;}intMainintargcConst Char*argv[]) {N=read (); q=read ();  for(intI=1; i<=n;i++) ww[i]=read ();  for(intI=1; i<=n-1; i++) X=read (), y=read (), ins (x, y); DFS (1);d FS (1,1);  for(intI=1; i<=n;i++) w[tid[i]]=Ww[i]; Build (1,1, N);  while(q--) {scanf ("%s", s); A=read (); b=read (); if(s[0]=='C') {C=read (); Sol1 (A,B,C); }Elseprintf"%d\n", Sol2 (A, b)); }    return 0;}

Bzoj 2243: [SDOI2011] staining [tree-chain split]

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.