Bzoj_4765_ General Calculation Ji _ block +dfs sequence + tree-like array

Source: Internet
Author: User

Bzoj_4765_ General Calculation Ji _ block

Description"Fight for three weeks, build a computer". Small g response to the call, took three hours to build a common computing Ji. General computing Kippie Ordinary computer to be a bit more powerful. The ordinary computer can calculate the sequence interval and, while the common computation can the tree and the neutron tree. More specifically, the calculation of small G Jackie to solve this problem: Given a tree of n nodes, the node number is 1 to n, Root, and sum[p] represents the right of all nodes in this subtrees tree with point P as rootvalues and. The following two types of operations are supported by the calculation:1 given two integer u,v, the weight of the modified point U is v. 2 given two integers l,r, calculate sum[l]+sum[l+1]+....+sum[r-1]+sum[r]Although the calculation can quickly complete the problem, but little G does not know whether its answer is correct, you can help him? InputThe first line, two integer n,m, represents the number of nodes and the number of operations in the tree. The next line is n integers, and the first I integer di represents the initial weight of the point I. The next n rows are two integers per line ai,bi, representing the edges on a tree, and if ai=0 indicates that bi is the root. the next m line is three integers per line, and the first integer op represents the type of operation. if Op=1 then the next two integer u,v indicates that the weight of the point U is modified to V. if op=2 then the next two integers l,r indicate the query. n<=10^5,m<=10^50<=di,v<2^31,1<=l<=r<=n,1<=u<=nOutputAn integer representing the answer for each operation type 2 output. Sample Input6 4
0 0 3 4 0 1
0 1
1 2
2 3
2 4
3 5
5 6
2 1 2
1 1 1
2 3 6
2 3 5Sample Output16
10
9 the node number is chunked, and the tree array maintains the DFS sequence. preprocessing time complexity $o (n\sqrt N) $ to pre-preprocess the point within the number of J blocks that I went to the root pathmodify all blocks directly with the pre-processing of the tag, but also in the tree array to be modified. query when the whole block directly tagged, scattered in the tree-like array of violence. Total time complexity $o (n\sqrt n logn) $, can be over. In fact, you can also change the $o (\sqrt N) $ query $o (1) $, can do $o (n\sqrt N) $ But I can't write a log. code:
#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h>using namespace STD; #define N 100050typedef unsigned long long ll;int size,block,l[n],r[n],pos[n],n,m;int dep[n],fa[n],head[n],to[n <<1],nxt[n<<1],val[n],cnt,root,poi[n][350],dfn[n],son[n];ll sum[n],tag[n],s[n],c[n];inline void Add ( int U,int v) {to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt;} void fix (int x,ll v) {for (;x<=n;x+=x& (x)) C[x]+=v;}    ll inq (int x) {ll re=0; For (;x;x-=x& (x)) re+=c[x]; return re;}    void Dfs (int x,int y) {int i,j; fa[x]=y; dep[x]=dep[y]+1; sum[x]=val[x]; dfn[x]=++dfn[0];    poi[x][pos[x]]++;            for (I=head[x];i;i=nxt[i]) {if (to[i]!=y) {for (j=1;j<=block;j++) poi[to[i]][j]=poi[x][j];            DFS (TO[I],X);        Sum[x]+=sum[to[i]]; }} Son[x]=dfn[0];}    void Modify (int x,int y) {int i; for (i=1;i<=block;i++) tag[i]+=1ll*y*poi[x][i];}  ll query (int x,int y) {int p=pos[x],q=pos[y],i;  ll Ans=0;    if (p==q) {for (i=x;i<=y;i++) ans+=inq (Son[i])-inq (dfn[i]-1);        }else {for (i=p+1;i<q;i++) ans+=s[i]+tag[i];        for (i=x;i<=r[p];i++) {ans+=inq (Son[i])-inq (dfn[i]-1);        } for (i=l[q];i<=y;i++) {ans+=inq (Son[i])-inq (dfn[i]-1); }} return ans;    int main () {scanf ("%d%d", &n,&m);     int i,x,y,opt,j;    SIZE=SQRT (n);    Block=n/size; for (i=1;i<=block;i++) {l[i]=r[i-1]+1;        R[i]=size*i;        for (j=l[i];j<=r[i];j++) {pos[j]=i; }} if (r[block]!=n) {block++; l[block]=r[block-1]+1;        R[block]=n;    for (i=l[block];i<=n;i++) Pos[i]=block;    } for (i=1;i<=n;i++) scanf ("%d", &i[val]);        for (i=1;i<=n;i++) {scanf ("%d%d", &x,&y);        if (x) {add [x, y]; add (y,x);    }else root=y;    } dfs (root,0);    for (i=1;i<=n;i++) S[pos[i]]+=sum[i],fix (Dfn[i],val[i]); while (m--) {scanf ("%d%d%d", & opt,&x,&y);            if (opt==1) {modify (x,-val[x]); fix (dfn[x],-val[x]); val[x]=y; Modify (X,val[x]);        Fix (Dfn[x],val[x]);        }else {printf ("%llu\n", query (x, y)); }    }}

Bzoj_4765_ General Calculation Ji _ block +dfs sequence + 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.