[Dynamic Point Division] BZOJ3730: The wave of the waves _ graph theory-

Source: Internet
Author: User
the

Given the n-point of a tree with a side right of 1, each point has a bit of power. M-time operation, two types
1. Single Point right modification.
2. Give x and K, ask to x distance no more than K of the right and.
Operation encryption, forcing online.
n,m<=100000

Dynamic Point Division.
The same information is collected to the root. For each point tree, put all the points in a tree array, the subscript of the digital array is the distance to the root. To note that the same subtree in the calculation of the duplicate, in order to remove the repeat part, you also need to open a digital array of each point, maintaining the point to the parent point to the root of the tree distance. Such modifications and inquiries are violent climbing trees, the change in the way of the tree-like array of single point of change, ask each time statistics through the current root of the path of the corresponding weights and can.

#include <cstdio> #include <queue> #include <algorithm> using namespace std;
const int maxn=100005, maxe=200005;
int n,m,w[maxn],last_print;
int Fir[maxn],nxt[maxe],son[maxe],tot;
BOOL VIS[MAXN];
int SZ[MAXN];
------------------------------------------------------------------------int RT,ALLMIN,ALLSZ,MAXD,D[MAXN];
    int dfs_info (int x,int fa) {sz[x]=1; for (int j=fir[x];j;j=nxt[j]) if (Son[j]!=fa&&!vis[son[j]]) {d[son[j]]=d[x]+1;
        Maxd=max (Maxd,d[son[j]]); Dfs_info (SON[J],X);
    SZ[X]+=SZ[SON[J]];  
     } void Dfs_g (int x,int fa) {int _max=0;  
        for (int j=fir[x];j;j=nxt[j]) if (SON[J]!=FA&AMP;&AMP;!VIS[SON[J)]) {dfs_g (son[j],x);  
     _max=max (_max,sz[son[j]]);  
} if (Allmin>max (_max,allsz-sz[x])) Allmin=max (_max,allsz-sz[x)), rt=x; 
    int find_g (int x) {allmin=1e+9; maxd=d[x]=0; Dfs_info (x,0);
    ALLSZ=SZ[X];
    Dfs_g (x,0);
return RT; }
//------------------------------------------------------------------------int dep[maxn],anc[maxn][25];
    void Dfs_lca (int x,int fa) {ANC[X][0]=FA;
    for (int j=1;j<=20;j++) anc[x][j]=anc[anc[x][j-1]][j-1];
for (int j=fir[x];j;j=nxt[j]) if (SON[J]!=FA) dep[son[j]]=dep[x]+1, Dfs_lca (son[j],x);
    int LCA (int x,int y) {if (Dep[x]<dep[y]) swap (x,y); 
    for (int j=20;j>=0;j--) if (Dep[anc[x][j]]>=dep[y]) x=anc[x][j];
    if (x==y) return x;
    for (int j=20;j>=0;j--) if (Anc[x][j]!=anc[y][j]) x=anc[x][j], y=anc[y][j];
return anc[x][0]; int Getdis (int x,int y) {return Dep[x]+dep[y]-dep[lca (x,y)]*2;}//-------------------------------------------------
-----------------------vector< int > bit[maxn][2];
    void Bit_updata (int k1,int k2,int x,int val) {int len=bit[k1][k2].size ()-1;
For (x++;x<=len;x+= (x& (x)) Bit[k1][k2][x]+=val;
    int bit_query (int k1,int k2,int x) {int res=0, len=bit[k1][k2].size ()-1;
    For (X=min (X+1,len); x;x-= (x& (x))) res+=bit[k1][k2][x];
return res; }
//------------------------------------------------------------------------int PRT[MAXN]; 
    void Dfs_push (int x,int fa,int ID) {bit_updata (Id,0,getdis (X,id), w[x]);    
    if (Prt[id]) bit_updata (Id,1,getdis (X,prt[id)), w[x]);
for (int j=fir[x];j;j=nxt[j]) if (son[j]!=fa&&!vis[son[j)]) Dfs_push (son[j],x,id); 
    } void Divtree (int x,int.) {int g=find_g (x); Prt[g]=fag;
    Vis[g]=true; Bit[g][0].resize (allsz+3); 
    Bit[g][1].resize (allsz+3);
    Dfs_push (G,0,G);
for (int j=fir[g];j;j=nxt[j]) if (!vis[son[j)]) Divtree (SON[J],G);
        } void Updata (int x,int val) {for (int y=x;y;y=prt[y]) {Bit_updata (Y,0,getdis (X,y), val-w[x]);       
    if (Prt[y]) bit_updata (Y,1,getdis (x,prt[y)), val-w[x]);
    int Query (int x,int K) {int res=bit_query (X,0,K); for (int y=x;prt[y];y=prt[y]) if (K>=getdis (x,prt[y))) Res+=bit_query (Prt[y],0,k-getdis (x,prt[y))-bit_Query (y,1
    , K-getdis (X,prt[y]);
return res; }
//------------------------------------------------------------------------void Add (int x,int y) {son[++tot]=y; nxt[tot]=fir[x]; fir[x]=tot;} int getint () {int res=0; char Ch=get
    char (); while (!) ('
    0 ' <=ch&&ch<= ' 9 ') Ch=getchar ();
    while (' 0 ' <=ch&&ch<= ' 9 ') res=res*10+ch-' 0 ', Ch=getchar ();
return res;
    int main () {freopen ("bzoj3730.in", "R", stdin);
    Freopen ("Bzoj3730.out", "w", stdout);
    scanf ("%d%d", &n,&m);
    for (int i=1;i<=n;i++) w[i]=getint (); 
        for (int i=1;i<=n-1;i++) {int x=getint (), Y=getint (); Add (x,y);
    Add (y,x);
    } DFS_LCA (1,0);
    Divtree (1,0);
        while (m--) {int pd=getint (), X=getint (), Y=getint (); X^=last_print;
        Y^=last_print;
           if (!PD) printf ("%d\n", Last_print=query (X,y));
    else Updata (x,y), w[x]=y;
return 0; }

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.