#include <iostream> #include <vector> using namespace std; #define pb push_back #define PIL pair<int,long long> #define PLI pair<long long, int> #define MP make_pair const long long inf = 2e18; const long long mininf = -1*inf; int n,q; vector<PIL>V[100001]; int subtrees_size[20][100001], centroid_tree_depth[100001], nr[20][100001], centroid[20][100001]; int id[20], base=1; PLI max_tree[20][300000]; long long add_tree[20][300000], edge[20][100001], cost[20][100001], profit[100001]; void count_max(int depth, int a){ if( a < base ){ max_tree[depth][a] = max(max_tree[depth][2*a], max_tree[depth][2*a+1]); max_tree[depth][a].first += add_tree[depth][a]; } } void insert(int a, int b, long long c, int depth){ a += base; b += base; while(a/2 != b/2){ if( a % 2 == 0 ){ add_tree[depth][a+1] += c; max_tree[depth][a+1].first += c; } if( b % 2 == 1 ){ add_tree[depth][b-1] += c; max_tree[depth][b-1].first += c; } count_max(depth, a); count_max(depth, b); a = a >> 1; b = b >> 1; } count_max(depth, a); count_max(depth, b); a = a>>1; while( a >= 1 ){ count_max(depth,a); a = a>>1; } } PLI query(int a, int b, int depth){ a += base; b += base; PLI res_a = MP(mininf*2,0); PLI res_b = MP(mininf*2,0); while( a/2 != b/2 ){ res_a.first += add_tree[depth][a]; res_b.first += add_tree[depth][b]; if( a%2 == 0 ) res_a = max(res_a, max_tree[depth][a+1]); if( b%2 == 1) res_b = max(res_b, max_tree[depth][b-1]); a = a>>1; b = b>>1; } while( a>=1 ){ res_a.first += add_tree[depth][a]; res_b.first += add_tree[depth][b]; a = a>>1; b = b>>1; } return max(res_a,res_b); } void read() { cin>>n>>q; while(base<n+2) base=base<<1; //cout<<base<<" base"<<endl; for(int d = 1; d < 20; d++){ for(int i =0 ; i <= 2*base; i++){ max_tree[d][i] = MP(mininf*2,0); } } for(int i=1;i<=n;i++) cin>>profit[i]; for(int i=0;i<n-1;i++){ int a,b; long long c; cin>>a>>b>>c; c *= (-1); V[a].pb(MP(b,c)); V[b].pb(MP(a,c)); } for(int i=1;i<=n;i++) centroid_tree_depth[i]=n; } void count_subtrees(int a, int parent, int depth) { subtrees_size[depth][a]=1; for(unsigned int i=0;i<V[a].size();i++){ int tmp=V[a][i].first; if(tmp != parent && depth < centroid_tree_depth[tmp]){ count_subtrees(tmp,a,depth); subtrees_size[depth][a]+=subtrees_size[depth][tmp]; } } } int find_centroid(int a, int parent, int depth, int size) { int res=a; for(unsigned int i=0;i<V[a].size();i++){ int tmp=V[a][i].first; if(tmp != parent && depth < centroid_tree_depth[tmp] && subtrees_size[depth][tmp] > size/2){ res=find_centroid(tmp,a,depth,size); } } return res; } void assign_nr(int a, int parent, int depth, int centr, long long c, long long e) { id[depth]++; nr[depth][a] = id[depth]; centroid[depth][a] = centr; edge[depth][ nr[depth][a] ] = e; cost[depth][a] = c + profit[a]; for(unsigned int i=0;i<V[a].size();i++){ int tmp=V[a][i].first; if(tmp != parent && depth < centroid_tree_depth[tmp]){ assign_nr(tmp,a,depth,centr, c+V[a][i].second, V[a][i].second); } } } void centroid_decompostion(int a, int depth) { count_subtrees(a,0,depth); a=find_centroid(a,0,depth,subtrees_size[depth][a]); //cout<<a<<" centr "<<depth<<endl; centroid_tree_depth[a]=depth; count_subtrees(a,0,depth); assign_nr(a,0,depth,a,0,0); for(unsigned int i=0;i<V[a].size();i++){ int tmp=V[a][i].first; if(depth < centroid_tree_depth[tmp]){ centroid_decompostion(tmp,depth+1); } } } int city; long long tmp; void rm_city(int a){ int depth = centroid_tree_depth[a]; tmp = mininf/2 - profit[a]; while(depth>=1){ insert(nr[depth][a]-1, nr[depth][a]+1, tmp, depth); depth--; } } void add_city(int a){ int depth = centroid_tree_depth[a]; while(depth>=1){ insert(nr[depth][a]-1, nr[depth][a]+1, (-1) * tmp, depth); depth--; } } void change_edge(){ int a, b; long long c; cin>>a>>b>>c; //cout<<"edge "<<a<<" "<<b<<" "<<c<<endl; c *= (-1); int depth = min(centroid_tree_depth[a],centroid_tree_depth[b]); while(depth>=1){ int nra = nr[depth][a]; int nrb = nr[depth][b]; int size = min(subtrees_size[depth][a], subtrees_size[depth][b]); int x = max(nra,nrb); insert( x-1, x+size, c - edge[depth][x], depth); //cout<<c - edge[depth][x]<< "ced "<<size<<endl; edge[depth][x] = c; depth--; } } void change_city(){ int a; long long c; cin>>a>>c; int depth = centroid_tree_depth[a]; long long change = c - profit[a]; profit[a] = c; while(depth>=1){ insert(nr[depth][a]-1, nr[depth][a]+1, change, depth); depth--; } } void questions() { city = 1; rm_city(city); for(int i=0;i<q;i++){ int x; cin>>x; if( x == 1 ) change_city(); else change_edge(); int depth = centroid_tree_depth[city]; int new_city = 0; long long minn = mininf*2; while(depth>=1){ int centr = centroid[depth][city]; PLI kan = query(nr[depth][centr] - 1, nr[depth][centr] + subtrees_size[depth][centr], depth ); long long dist = query(nr[depth][city] - 1, nr[depth][city] + 1, depth).first + kan.first; //cout<<dist<<" dist "<<kan.first<<endl; if( dist > minn){ new_city = kan.second * (-1); minn = dist; } if(dist == minn && new_city > kan.second * (-1) ) new_city = kan.second * (-1); depth--; } add_city(city); city = new_city; //cout<<city<<" city"<<endl; cout<<city<<" "; rm_city(city); } //cout<<endl; } int main() { ios_base::sync_with_stdio(false); read(); centroid_decompostion(1,1); for( int d = 1; d < 20; d++ ){ for( int i = 1; i <= n; i++ ){ if(centroid_tree_depth[i] >= d) max_tree[d][ nr[d][i] + base ] = MP(cost[d][i],(-1)*i); } for( int i = base ; i >= 1; i-- ) count_max(d,i); } for(int d = 1; d < 4; d++){ //cout<<d<<" max_tree "<<max_tree[d][1].first<<endl; } questions(); }
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | #include <iostream> #include <vector> using namespace std; #define pb push_back #define PIL pair<int,long long> #define PLI pair<long long, int> #define MP make_pair const long long inf = 2e18; const long long mininf = -1*inf; int n,q; vector<PIL>V[100001]; int subtrees_size[20][100001], centroid_tree_depth[100001], nr[20][100001], centroid[20][100001]; int id[20], base=1; PLI max_tree[20][300000]; long long add_tree[20][300000], edge[20][100001], cost[20][100001], profit[100001]; void count_max(int depth, int a){ if( a < base ){ max_tree[depth][a] = max(max_tree[depth][2*a], max_tree[depth][2*a+1]); max_tree[depth][a].first += add_tree[depth][a]; } } void insert(int a, int b, long long c, int depth){ a += base; b += base; while(a/2 != b/2){ if( a % 2 == 0 ){ add_tree[depth][a+1] += c; max_tree[depth][a+1].first += c; } if( b % 2 == 1 ){ add_tree[depth][b-1] += c; max_tree[depth][b-1].first += c; } count_max(depth, a); count_max(depth, b); a = a >> 1; b = b >> 1; } count_max(depth, a); count_max(depth, b); a = a>>1; while( a >= 1 ){ count_max(depth,a); a = a>>1; } } PLI query(int a, int b, int depth){ a += base; b += base; PLI res_a = MP(mininf*2,0); PLI res_b = MP(mininf*2,0); while( a/2 != b/2 ){ res_a.first += add_tree[depth][a]; res_b.first += add_tree[depth][b]; if( a%2 == 0 ) res_a = max(res_a, max_tree[depth][a+1]); if( b%2 == 1) res_b = max(res_b, max_tree[depth][b-1]); a = a>>1; b = b>>1; } while( a>=1 ){ res_a.first += add_tree[depth][a]; res_b.first += add_tree[depth][b]; a = a>>1; b = b>>1; } return max(res_a,res_b); } void read() { cin>>n>>q; while(base<n+2) base=base<<1; //cout<<base<<" base"<<endl; for(int d = 1; d < 20; d++){ for(int i =0 ; i <= 2*base; i++){ max_tree[d][i] = MP(mininf*2,0); } } for(int i=1;i<=n;i++) cin>>profit[i]; for(int i=0;i<n-1;i++){ int a,b; long long c; cin>>a>>b>>c; c *= (-1); V[a].pb(MP(b,c)); V[b].pb(MP(a,c)); } for(int i=1;i<=n;i++) centroid_tree_depth[i]=n; } void count_subtrees(int a, int parent, int depth) { subtrees_size[depth][a]=1; for(unsigned int i=0;i<V[a].size();i++){ int tmp=V[a][i].first; if(tmp != parent && depth < centroid_tree_depth[tmp]){ count_subtrees(tmp,a,depth); subtrees_size[depth][a]+=subtrees_size[depth][tmp]; } } } int find_centroid(int a, int parent, int depth, int size) { int res=a; for(unsigned int i=0;i<V[a].size();i++){ int tmp=V[a][i].first; if(tmp != parent && depth < centroid_tree_depth[tmp] && subtrees_size[depth][tmp] > size/2){ res=find_centroid(tmp,a,depth,size); } } return res; } void assign_nr(int a, int parent, int depth, int centr, long long c, long long e) { id[depth]++; nr[depth][a] = id[depth]; centroid[depth][a] = centr; edge[depth][ nr[depth][a] ] = e; cost[depth][a] = c + profit[a]; for(unsigned int i=0;i<V[a].size();i++){ int tmp=V[a][i].first; if(tmp != parent && depth < centroid_tree_depth[tmp]){ assign_nr(tmp,a,depth,centr, c+V[a][i].second, V[a][i].second); } } } void centroid_decompostion(int a, int depth) { count_subtrees(a,0,depth); a=find_centroid(a,0,depth,subtrees_size[depth][a]); //cout<<a<<" centr "<<depth<<endl; centroid_tree_depth[a]=depth; count_subtrees(a,0,depth); assign_nr(a,0,depth,a,0,0); for(unsigned int i=0;i<V[a].size();i++){ int tmp=V[a][i].first; if(depth < centroid_tree_depth[tmp]){ centroid_decompostion(tmp,depth+1); } } } int city; long long tmp; void rm_city(int a){ int depth = centroid_tree_depth[a]; tmp = mininf/2 - profit[a]; while(depth>=1){ insert(nr[depth][a]-1, nr[depth][a]+1, tmp, depth); depth--; } } void add_city(int a){ int depth = centroid_tree_depth[a]; while(depth>=1){ insert(nr[depth][a]-1, nr[depth][a]+1, (-1) * tmp, depth); depth--; } } void change_edge(){ int a, b; long long c; cin>>a>>b>>c; //cout<<"edge "<<a<<" "<<b<<" "<<c<<endl; c *= (-1); int depth = min(centroid_tree_depth[a],centroid_tree_depth[b]); while(depth>=1){ int nra = nr[depth][a]; int nrb = nr[depth][b]; int size = min(subtrees_size[depth][a], subtrees_size[depth][b]); int x = max(nra,nrb); insert( x-1, x+size, c - edge[depth][x], depth); //cout<<c - edge[depth][x]<< "ced "<<size<<endl; edge[depth][x] = c; depth--; } } void change_city(){ int a; long long c; cin>>a>>c; int depth = centroid_tree_depth[a]; long long change = c - profit[a]; profit[a] = c; while(depth>=1){ insert(nr[depth][a]-1, nr[depth][a]+1, change, depth); depth--; } } void questions() { city = 1; rm_city(city); for(int i=0;i<q;i++){ int x; cin>>x; if( x == 1 ) change_city(); else change_edge(); int depth = centroid_tree_depth[city]; int new_city = 0; long long minn = mininf*2; while(depth>=1){ int centr = centroid[depth][city]; PLI kan = query(nr[depth][centr] - 1, nr[depth][centr] + subtrees_size[depth][centr], depth ); long long dist = query(nr[depth][city] - 1, nr[depth][city] + 1, depth).first + kan.first; //cout<<dist<<" dist "<<kan.first<<endl; if( dist > minn){ new_city = kan.second * (-1); minn = dist; } if(dist == minn && new_city > kan.second * (-1) ) new_city = kan.second * (-1); depth--; } add_city(city); city = new_city; //cout<<city<<" city"<<endl; cout<<city<<" "; rm_city(city); } //cout<<endl; } int main() { ios_base::sync_with_stdio(false); read(); centroid_decompostion(1,1); for( int d = 1; d < 20; d++ ){ for( int i = 1; i <= n; i++ ){ if(centroid_tree_depth[i] >= d) max_tree[d][ nr[d][i] + base ] = MP(cost[d][i],(-1)*i); } for( int i = base ; i >= 1; i-- ) count_max(d,i); } for(int d = 1; d < 4; d++){ //cout<<d<<" max_tree "<<max_tree[d][1].first<<endl; } questions(); } |