1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
| #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <vector> #include <stack> #include <queue> #include <cmath> #include <set> #include <map> #define mp make_pair #define pb push_back #define pii pair<int,int> #define link(x) for(edge *j=h[x];j;j=j->next) #define inc(i,l,r) for(int i=l;i<=r;i++) #define dec(i,r,l) for(int i=r;i>=l;i--) const int MAXN=1e6+10; const double eps=1e-8; const int inf=1e9+9; #define ll long long using namespace std; struct edge{int t;edge*next;}e[MAXN<<1],*h[MAXN],*o=e; void add(int x,int y){o->t=y;o->next=h[x];h[x]=o++;} namespace superRead { #define BUF_SIZE 5000010 char buf[BUF_SIZE]; int cur = BUF_SIZE; FILE *in = stdin; inline char gnc() { if (cur == BUF_SIZE) { fread(buf, BUF_SIZE, 1, in); cur = 0; } return buf[cur++]; } template <typename T> inline void read(T &x) { int f = 0; char c = gnc(); x = 0; while (!isdigit(c)) f |= c == '-', c = gnc(); while (isdigit(c)) x = x * 10 + c - 48, c = gnc(); if (f) x = -x; } template <typename T, typename... Args> inline void read(T& t, Args&... args) { read(t); read(args...); } #undef BUF_SIZE } using superRead::read; template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + 48); } template <typename T> void writeln(T x) { write(x); puts(""); } template <typename T> inline bool chkmin(T& x, const T& y) { return y < x ? (x = y, true) : false; } template <typename T> inline bool chkmax(T& x, const T& y) { return x < y ? (x = y, true) : false; }
typedef struct Matrix{ int num[2][2]; void init(int x){ num[0][0]=num[0][1]=0; num[1][0]=x;num[1][1]=-inf; } Matrix operator*(const Matrix &v){ Matrix ans; inc(i,0,1)inc(j,0,1)ans.num[i][j]=-inf; ans.num[0][0]=max(num[0][0]+v.num[0][0],num[0][1]+v.num[1][0]); ans.num[0][1]=max(num[0][0]+v.num[0][1],num[0][1]+v.num[1][1]); ans.num[1][0]=max(num[1][0]+v.num[0][0],num[1][1]+v.num[1][0]); ans.num[1][1]=max(num[1][0]+v.num[0][1],num[1][1]+v.num[1][1]); return ans; } }Matrix; Matrix s[MAXN],p[MAXN]; int f[MAXN],son[MAXN],num[MAXN]; void dfs(int x,int pre){ f[x]=pre;num[x]=1; link(x){ if(j->t==pre)continue; dfs(j->t,x); num[x]+=num[j->t]; if(son[x]==-1||num[son[x]]<num[j->t])son[x]=j->t; } } int tp[MAXN],ch[MAXN][2]; void _dfs(int x,int td){ tp[x]=td; if(son[x]!=-1)_dfs(son[x],td); link(x)if(j->t!=son[x]&&j->t!=f[x])_dfs(j->t,j->t); } void up(int x){ s[x]=p[x]; if(ch[x][0])s[x]=s[ch[x][0]]*s[x]; if(ch[x][1])s[x]=s[x]*s[ch[x][1]]; } void __dfs(int x){ link(x){ if(j->t==f[x])continue; __dfs(j->t); if(j->t!=son[x]){ p[x].num[0][0]+=max(s[j->t].num[0][0],s[j->t].num[1][0]); p[x].num[0][1]+=max(s[j->t].num[0][0],s[j->t].num[1][0]); p[x].num[1][0]+=s[j->t].num[0][0]; } } s[x]=p[x]; if(son[x]!=-1)s[x]=s[x]*s[son[x]]; } int st[MAXN],tot,size[MAXN]; int fa[MAXN],a[MAXN]; int fbuilt(int l,int r){ if(l>r)return 0; ll sum1=0,sum2=0; inc(i,l,r)sum1+=size[i]; inc(i,l,r){ if(2*(sum2+size[i])>=sum1){ int lx=fbuilt(l,i-1);int rx=fbuilt(i+1,r); ch[st[i]][0]=lx;ch[st[i]][1]=rx; fa[lx]=st[i];fa[rx]=st[i]; up(st[i]); return st[i]; } else sum2+=size[i]; } } bool check(int x,int y){ if(x==0||y==0)return 1; if(tp[x]==tp[y])return 1; return 0; } int built(int x){ tot=0; while(x!=-1){ st[++tot]=x; if(son[x]!=-1)size[tot]=num[x]-num[son[x]]; else size[x]=1; x=son[x]; } return fbuilt(1,tot); }
void update(int x,int y){ p[x].num[1][0]+=(y-a[x]);a[x]=y; while(x){ if(fa[x]&&!check(x,fa[x])){ p[fa[x]].num[1][0]-=s[x].num[0][0]; p[fa[x]].num[0][0]-=max(s[x].num[0][0],s[x].num[1][0]); p[fa[x]].num[0][1]-=max(s[x].num[0][0],s[x].num[1][0]); up(x); p[fa[x]].num[1][0]+=s[x].num[0][0]; p[fa[x]].num[0][0]+=max(s[x].num[0][0],s[x].num[1][0]); p[fa[x]].num[0][1]+=max(s[x].num[0][0],s[x].num[1][0]); } else up(x); x=fa[x]; } } int n,m; int main(){ read(n,m); inc(i,1,n)read(a[i]),p[i].init(a[i]),s[i].init(-inf),son[i]=-1; int x,y; inc(i,2,n)read(x,y),add(x,y),add(y,x); dfs(1,0);_dfs(1,1);__dfs(1); inc(i,1,n)if(tp[i]==i)fa[built(i)]=f[i]; int rt; inc(i,1,n)if(!fa[i])rt=i; int Ans=0; while(m--){ read(x,y);x^=Ans; update(x,y); Ans=max(s[rt].num[0][0],s[rt].num[1][0]); writeln(Ans); } return 0; }
|