結果
問題 | No.1030 だんしんぐぱーりない |
ユーザー | LayCurse |
提出日時 | 2020-04-17 21:58:37 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 63 ms / 2,000 ms |
コード長 | 11,488 bytes |
コンパイル時間 | 2,544 ms |
コンパイル使用メモリ | 220,968 KB |
実行使用メモリ | 16,028 KB |
最終ジャッジ日時 | 2024-10-03 12:52:27 |
合計ジャッジ時間 | 6,542 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 54 ms
11,776 KB |
testcase_06 | AC | 41 ms
10,368 KB |
testcase_07 | AC | 27 ms
7,424 KB |
testcase_08 | AC | 28 ms
7,936 KB |
testcase_09 | AC | 45 ms
11,904 KB |
testcase_10 | AC | 19 ms
6,016 KB |
testcase_11 | AC | 46 ms
8,832 KB |
testcase_12 | AC | 44 ms
10,752 KB |
testcase_13 | AC | 35 ms
10,112 KB |
testcase_14 | AC | 50 ms
10,624 KB |
testcase_15 | AC | 23 ms
6,016 KB |
testcase_16 | AC | 41 ms
9,344 KB |
testcase_17 | AC | 40 ms
11,776 KB |
testcase_18 | AC | 49 ms
10,880 KB |
testcase_19 | AC | 29 ms
6,784 KB |
testcase_20 | AC | 32 ms
8,448 KB |
testcase_21 | AC | 32 ms
9,728 KB |
testcase_22 | AC | 31 ms
8,704 KB |
testcase_23 | AC | 38 ms
8,448 KB |
testcase_24 | AC | 29 ms
7,040 KB |
testcase_25 | AC | 34 ms
11,284 KB |
testcase_26 | AC | 23 ms
10,692 KB |
testcase_27 | AC | 28 ms
7,364 KB |
testcase_28 | AC | 43 ms
11,344 KB |
testcase_29 | AC | 31 ms
12,880 KB |
testcase_30 | AC | 34 ms
10,956 KB |
testcase_31 | AC | 34 ms
10,952 KB |
testcase_32 | AC | 42 ms
12,932 KB |
testcase_33 | AC | 39 ms
12,656 KB |
testcase_34 | AC | 19 ms
7,364 KB |
testcase_35 | AC | 61 ms
16,028 KB |
testcase_36 | AC | 61 ms
13,900 KB |
testcase_37 | AC | 60 ms
13,900 KB |
testcase_38 | AC | 62 ms
15,396 KB |
testcase_39 | AC | 63 ms
15,284 KB |
testcase_40 | AC | 3 ms
6,820 KB |
testcase_41 | AC | 2 ms
6,816 KB |
コンパイルメッセージ
main.cpp: In function 'int main()': main.cpp:454:11: warning: 'res' may be used uninitialized [-Wmaybe-uninitialized] 454 | wt_L(cmx[res]); | ~~~~^~~~~~~~~~ main.cpp:512:5: note: 'res' was declared here 512 | T res; | ^~~ In member function 'int HLD::lca(int, int)', inlined from 'void segtree_pg_func(int&, int&, int&)' at main.cpp:408:16, inlined from 'T segtree_pg<T>::get(int, int) [with T = int]' at main.cpp:549:20, inlined from 'int main()' at main.cpp:453:18: main.cpp:285:16: warning: 'tmp' may be used uninitialized [-Wmaybe-uninitialized] 285 | y1 = group[y]; | ^ main.cpp: In function 'int main()': main.cpp:513:5: note: 'tmp' was declared here 513 | T tmp; | ^~~
ソースコード
#pragma GCC optimize ("Ofast") #include<bits/stdc++.h> using namespace std; void *wmem; char memarr[96000000]; template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){ static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] ); (*arr)=(T*)(*mem); (*mem)=((*arr)+x); } inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(int x){ int s=0; int m=0; char f[10]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } template<class S, class T> inline S chmax(S &a, T b){ if(a<b){ a=b; } return a; } template<class T> struct segtree_pg{ int N; int logN; T *val; void malloc(int maxN, int once = 0); void walloc(int maxN, int once = 0, void **mem = &wmem); void free(void); T& operator[](int i); void setN(int n, int zerofill = 1, int dobuild = 1); void build(void); inline void build(int a); inline void change(int a, T v); inline T get(int a, int b); } ; struct graph{ int N; int *es; int **edge; void setEdge(int N__, int M, int A[], int B[], void **mem = &wmem){ int i; N = N__; walloc1d(&es, N, mem); walloc1d(&edge, N, mem); for(i=(0);i<(N);i++){ es[i] = 0; } for(i=(0);i<(M);i++){ es[A[i]]++; es[B[i]]++; } for(i=(0);i<(N);i++){ walloc1d(&edge[i], es[i], mem); } for(i=(0);i<(N);i++){ es[i] = 0; } for(i=(0);i<(M);i++){ edge[A[i]][es[A[i]]++] = B[i]; edge[B[i]][es[B[i]]++] = A[i]; } } } ; struct HLD{ int N; int *es; int **edge; int *group; int *groupind; int groupNum; int *groupSize; int **groupNode; int *groupUpNode; int *groupDepth; void init(graph g, void **mem = &wmem){ init(g.N, g.es, g.edge, mem); } void init(int N__, int *es__, int **edge__, void **mem = &wmem){ int i; int j; int k; int x; int y; int mx; int *q; int q_st; int q_ed; int *sz; char *vis; void *tmpmem; N = N__; es = es__; edge = edge__; walloc1d(&group, N, mem); walloc1d(&groupind, N, mem); tmpmem = *mem; walloc1d(&q, N, &tmpmem); walloc1d(&sz, N, &tmpmem); walloc1d(&vis, N, &tmpmem); for(i=(0);i<(N);i++){ vis[i] = 0; } q_st = 0; q_ed = 1; q[0] = 0; vis[0] = 1; while(q_st < q_ed){ i = q[q_st++]; for(j=(0);j<(es[i]);j++){ k = edge[i][j]; if(!vis[k]){ vis[k] = 1; q[q_ed++] = k; } } } for(i=(0);i<(N);i++){ sz[i] = 0; } for(j=N-1;j>=0;j--){ i = q[j]; sz[i] = 1; for(k=(0);k<(es[i]);k++){ sz[i] += sz[edge[i][k]]; } } for(i=(0);i<(N);i++){ group[i] = -1; } groupNum = 0; for(j=(0);j<(N);j++){ i = q[j]; if(group[i]>=0){ continue; } group[i] = groupNum++; groupind[i] = 0; for(;;){ mx = -1; for(k=(0);k<(es[i]);k++){ if(group[edge[i][k]] != -1){ continue; } if(mx==-1){ mx = k; } else if(sz[edge[i][k]] > sz[edge[i][mx]]){ mx = k; } } if(mx==-1){ break; } group[edge[i][mx]] = group[i]; groupind[edge[i][mx]] = groupind[i]+1; i = edge[i][mx]; } } walloc1d(&groupSize, groupNum, mem); walloc1d(&groupUpNode, groupNum, mem); walloc1d(&groupDepth, groupNum, mem); for(i=(0);i<(groupNum);i++){ groupSize[i] = 0; } for(i=(0);i<(N);i++){ groupSize[group[i]]++; } walloc1d(&groupNode, groupNum, mem); for(i=(0);i<(groupNum);i++){ walloc1d(&groupNode[i], groupSize[i], mem); } for(i=(0);i<(N);i++){ groupNode[group[i]][groupind[i]] = i; } for(i=(0);i<(groupNum);i++){ groupDepth[i] = -1; } groupUpNode[0] = -1; groupDepth[0] = 0; for(x=(0);x<(groupNum);x++){ for(y=(0);y<(groupSize[x]);y++){ i = groupNode[x][y]; for(j=(0);j<(es[i]);j++){ k = edge[i][j]; if(x != group[k] && groupDepth[group[k]]==-1){ groupUpNode[group[k]] = i; groupDepth[group[k]] = groupDepth[x] + 1; } } } } } int lca(int x, int y){ int x1; int y1; int x2; int y2; x1 = group[x]; x2 = groupind[x]; y1 = group[y]; y2 = groupind[y]; while(groupDepth[x1] > groupDepth[y1]){ x = groupUpNode[x1]; x1 = group[x]; x2 = groupind[x]; } while(groupDepth[x1] < groupDepth[y1]){ y = groupUpNode[y1]; y1 = group[y]; y2 = groupind[y]; } while(x1 != y1){ x = groupUpNode[x1]; x1 = group[x]; x2 = groupind[x]; y = groupUpNode[y1]; y1 = group[y]; y2 = groupind[y]; } if(x2 <= y2){ return x; } return y; } int depth(int x){ int x1; int x2; int res = 0; x1 = group[x]; x2 = groupind[x]; while(groupUpNode[x1] != -1){ res += x2 + 1; x = groupUpNode[x1]; x1 = group[x]; x2 = groupind[x]; } return res + x2; } int dist(int x, int y){ int x1; int y1; int x2; int y2; int res = 0; x1 = group[x]; x2 = groupind[x]; y1 = group[y]; y2 = groupind[y]; while(groupDepth[x1] > groupDepth[y1]){ res += x2 + 1; x = groupUpNode[x1]; x1 = group[x]; x2 = groupind[x]; } while(groupDepth[x1] < groupDepth[y1]){ res += y2 + 1; y = groupUpNode[y1]; y1 = group[y]; y2 = groupind[y]; } while(x1 != y1){ res += x2 + y2 + 2; x = groupUpNode[x1]; x1 = group[x]; x2 = groupind[x]; y = groupUpNode[y1]; y1 = group[y]; y2 = groupind[y]; } if(x2 <= y2){ return res + y2 - x2; } return res + x2 - y2; } int up(int x){ int x1 = group[x]; int x2 = groupind[x]; if(x2==0){ return groupUpNode[x1]; } return groupNode[x1][x2-1]; } int up(int x, int d){ int x1 = group[x]; int x2 = groupind[x]; while(d > x2){ if(groupUpNode[x1]==-1){ return -1; } d -= x2 + 1; x = groupUpNode[x1]; x1 = group[x]; x2 = groupind[x]; } return groupNode[x1][x2-d]; } } ; int N; int K; int Q; int C[100000]; int A[100000]; int E[100000]; int F[100000]; int T[3]; int cmx[100000]; graph g; HLD hld; segtree_pg<int> t; void dfs(int n, int b = -1){ int Lj4PdHRW; chmax(cmx[n], C[n]); for(Lj4PdHRW=(0);Lj4PdHRW<(g.es[n]);Lj4PdHRW++){ auto &i = g.edge[n][Lj4PdHRW]; if(i!=b){ chmax(cmx[i], cmx[n]); dfs(i, n); } } } void segtree_pg_func(int &res, int &a, int &b){ res = hld.lca(a,b); } int main(){ int hCmBdyQB, i; wmem = memarr; int res; rd(N); rd(K); rd(Q); { int e98WHCEY; for(e98WHCEY=(0);e98WHCEY<(N);e98WHCEY++){ rd(C[e98WHCEY]); } } { int FmcKpFmN; for(FmcKpFmN=(0);FmcKpFmN<(K);FmcKpFmN++){ rd(A[FmcKpFmN]);A[FmcKpFmN] += (-1); } } { int t_ynMSdg; for(t_ynMSdg=(0);t_ynMSdg<(N-1);t_ynMSdg++){ rd(E[t_ynMSdg]);E[t_ynMSdg] += (-1); rd(F[t_ynMSdg]);F[t_ynMSdg] += (-1); } } g.setEdge(N,N-1,E,F); hld.init(g); dfs(0); t.malloc(K); t.setN(K); for(i=(0);i<(K);i++){ t[i] = A[i]; } t.build(); for(hCmBdyQB=(0);hCmBdyQB<(Q);hCmBdyQB++){ rd(T[0]); rd(T[1]);T[1] += (-1); rd(T[2]);T[2] += (-1); if(T[0]==1){ t.change(T[1], T[2]); } else{ res = t.get(T[1], T[2]+1); wt_L(cmx[res]); wt_L('\n'); } } return 0; } template<class T> void segtree_pg<T>::malloc(int maxN, int once /*= 0*/){ int i; for(i=1;i<maxN;i*=2){ ; } val = new T[2*i]; if(once){ setN(maxN); } } template<class T> void segtree_pg<T>::walloc(int maxN, int once /*= 0*/, void **mem /*= &wmem*/){ int i; for(i=1;i<maxN;i*=2){ ; } walloc1d(&val, 2*i, mem); if(once){ setN(maxN); } } template<class T> void segtree_pg<T>::free(void){ delete [] val; } template<class T> T& segtree_pg<T>::operator[](int i){ return val[N+i]; } template<class T> void segtree_pg<T>::setN(int n, int zerofill /*= 1*/, int dobuild /*= 1*/){ int i; for(i=1,logN=0;i<n;i*=2,logN++){ ; } N = i; if(dobuild){ build(); } } template<class T> void segtree_pg<T>::build(void){ for(int i=N-1;i;i--){ segtree_pg_func(val[i], val[2*i], val[2*i+1]); } } template<class T> inline void segtree_pg<T>::build(int a){ while(a > 1){ a /= 2; segtree_pg_func(val[a], val[2*a], val[2*a+1]); } } template<class T> inline void segtree_pg<T>::change(int a, T v){ val[a+N] = v; build(a+N); } template<class T> inline T segtree_pg<T>::get(int a, int b){ T res; T tmp; int fga = 0; int fgb = 0; a += N; b += N; while(a < b){ if(a%2){ if(fga){ segtree_pg_func(res, res, val[a]); } else{ res = val[a]; fga = 1; } a++; } if(b%2){ b--; if(fgb){ segtree_pg_func(tmp, val[b], tmp); } else{ tmp = val[b]; fgb = 1; } } a /= 2; b /= 2; } if(fga==1 && fgb==0){ return res; } if(fga==0 && fgb==1){ return tmp; } if(fga==1 && fgb==1){ segtree_pg_func(res, res, tmp); return res; } return res; } // cLay varsion 20200412-1 // --- original code --- // int N, K, Q, C[1d5], A[1d5], E[1d5], F[1d5], T[3]; // // int cmx[1d5]; // graph g; // HLD hld; // segtree_pg<int> t; // // void dfs(int n, int b = -1){ // cmx[n] >?= C[n]; // rep[g.edge[n]](i,g.es[n]) if(i!=b){ // cmx[i] >?= cmx[n]; // dfs(i, n); // } // } // // void segtree_pg_func(int &res, int &a, int &b){ // res = hld.lca(a,b); // } // // { // int res; // rd(N,K,Q,C(N),(A--)(K),(E--,F--)(N-1)); // // g.setEdge(N,N-1,E,F); // hld.init(g); // dfs(0); // // t.malloc(K); // t.setN(K); // rep(i,K) t[i] = A[i]; // t.build(); // // rep(Q){ // rd(T[0],T[1]--,T[2]--); // if(T[0]==1){ // t.change(T[1], T[2]); // } else { // res = t.get(T[1], T[2]+1); // wt(cmx[res]); // } // } // }