@OceanEye7年前
05/3
19:03
某码农题
动态点分治[或者说点分树?]
转化一下[差分一下]就可以看出
要维护一棵树
可以在log~log方的时间复杂度内跑完整棵树的前缀和[1,l-1],[1,r]
于是用了点分树
树链剖分可不可以还没想过,目测是不行的
用了vector来控制空间
当然有大佬愿意写动态开点线段树或者平衡树也是很强的orz
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
#include <cstdio> #include <algorithm> #include <cstring> #include <cstdlib> #include <vector> #include <iostream> #define ll long long #define INF 1000000000 #define p(x) ('0'<=x&&x<='9') char cc; int C; template <class T> void read( T &x ) { x=0; cc=getchar(); C=1; while(!p(cc)) { if(cc=='-') C=-1; cc=getchar(); } while(p(cc)) { x=x*10+cc-48; cc=getchar(); } x*=C; } using namespace std; #define N 150010 #define Q 200010 int TO[N<<1],val[N<<1],NXT[N<<1],fst[N],e_tot; void add_e(int _,int __,int ___) { TO[e_tot]=__; val[e_tot]=___; NXT[e_tot]=fst[_]; fst[_]=e_tot++; TO[e_tot]=_; val[e_tot]=___; NXT[e_tot]=fst[__]; fst[__]=e_tot++; } int age[N]; // struct sav { ll tot; int age; sav(int _,int __) : tot(_),age(__){} bool operator < (const sav &x) const { return age<x.age; } }; struct rec { int fa,id; int son[3],_tot; vector <sav> Arr[3]; vector <sav> dist; int operator [] (const int &x) { return son[x]; } }pool[N]; int root,siz[N],mxsiz[N],d[N],T,now; bool use[N]; void dfs_push(int x,int f,ll dist) { pool[now].dist.push_back(sav(dist,x)); siz[x]=1; pool[now].Arr[pool[now]._tot].push_back(sav(dist,age[x])); for(int i=fst[x];~i;i=NXT[i]) { if(use[TO[i]]||TO[i]==f) continue; dfs_push(TO[i],x,dist+val[i]); siz[x]+=siz[TO[i]]; } } void getroot(int x,int f) { siz[x]=1; mxsiz[x]=0; for(int i=fst[x];~i;i=NXT[i]) { if(TO[i]==f||use[TO[i]]) continue; getroot(TO[i],x); siz[x]+=siz[TO[i]]; mxsiz[x]=max(mxsiz[x],siz[TO[i]]); } mxsiz[x]=max(mxsiz[x],T-siz[x]); if(mxsiz[x]<mxsiz[root]) root=x; } void dfs_build(int x,int f) { now=x; pool[x].fa=f; use[x]=true; for(int i=fst[x];~i;i=NXT[i]) { if(use[TO[i]]) continue; root=0; T=siz[TO[i]]; dfs_push(TO[i],0,val[i]); getroot(TO[i],0); pool[now].son[pool[now]._tot]=root; siz[root]=siz[TO[i]]; sort(pool[now].Arr[pool[now]._tot].begin(),pool[now].Arr[pool[now]._tot].end()); for(int j=1;j<pool[now].Arr[pool[now]._tot].size();j++) pool[now].Arr[pool[now]._tot][j].tot+=pool[now].Arr[pool[now]._tot][j-1].tot; pool[now]._tot++; } pool[now].dist.push_back(sav(0,now)); sort(pool[now].dist.begin(),pool[now].dist.end()); for(int i=0;i<pool[x]._tot;i++) dfs_build(pool[x][i],x); } int n,m,A; void pre() { int _,__,___; read(n); read(m); read(A); for(int i=1;i<=n;i++) { fst[i]=-1; read(age[i]); age[i]++; } for(int i=1;i<n;i++) { read(_); read(__); read(___); add_e(_,__,___); } T=n; mxsiz[0]=INF; getroot(1,0); dfs_build(root,0); } ll get_ans(int x,int l,int r) { sav _(0,l-1),__(0,r); vector <sav> :: iterator it,it2; int L,R,pre=0,tot=0,___,S=x; ll dist=0,ret=0; while(x) { tot=0; for(int i=0;i<pool[x]._tot;i++) { if(pool[x][i]==pre) continue; it = upper_bound(pool[x].Arr[i].begin(),pool[x].Arr[i].end(),_); if(it!=pool[x].Arr[i].begin()) { it--; ret-=it->tot; it++; } it2 = upper_bound(pool[x].Arr[i].begin(),pool[x].Arr[i].end(),__); if(it2!=pool[x].Arr[i].begin()) { it2--; ret+=it2->tot; it2++; } tot+=distance(it,it2); } if(age[x]<=r&&age[x]>=l) tot++; it=lower_bound(pool[x].dist.begin(),pool[x].dist.end(),sav(0,S)); dist=it->tot; ret+=dist*tot; pre=x; x=pool[x].fa; } return ret; } ll lastans; int main() { // freopen("BZOJ4012.in","r",stdin); ll _,__,___,L,R; pre(); while(m--) { read(_); read(__); read(___); __+=lastans; ___+=lastans; __%=A; ___%=A; __++; ___++; L=min(__,___); R=max(__,___); lastans=get_ans(_,L,R); printf("%lld\n",lastans); } return 0; } |