結果
問題 | No.650 行列木クエリ |
ユーザー | uwi |
提出日時 | 2018-02-09 22:48:52 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 15,113 bytes |
コンパイル時間 | 4,435 ms |
コンパイル使用メモリ | 90,064 KB |
実行使用メモリ | 93,496 KB |
最終ジャッジ日時 | 2024-10-08 17:00:55 |
合計ジャッジ時間 | 9,514 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 89 ms
39,860 KB |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | AC | 89 ms
39,776 KB |
testcase_07 | AC | 104 ms
39,612 KB |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | AC | 87 ms
39,968 KB |
ソースコード
package contest180209; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class E { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(); int[] from = new int[n - 1]; int[] to = new int[n - 1]; for (int i = 0; i < n - 1; i++) { from[i] = ni(); to[i] = ni(); } int[][] g = packU(n, from, to); int[][] pars = parents3(g, 0); int[] par = pars[0], ord = pars[1], dep = pars[2]; HeavyLightDecomposition hld = new HeavyLightDecomposition(g, par, ord, dep); SegmentTreeMatrix[] sts = new SegmentTreeMatrix[n]; for(int i = 0;i < hld.m;i++){ sts[i] = new SegmentTreeMatrix(hld.cluspath[i].length); } for(int Q = ni();Q > 0;Q--){ char c = nc(); if(c == 'x'){ int v = ni(); if(par[from[v]] == to[v]){ v = from[v]; }else{ v = to[v]; } // 1 3 7 2 // 0 2 4 9 sts[hld.clus[v]].update(hld.clusiind[v], ni(), ni(), ni(), ni()); }else{ int anc = ni(), u = ni(); int[][] M = {{1, 0}, {0, 1}}; if(anc != u){ anc = hld.ancestor(u, dep[u] - dep[anc] - 1); for(int x = u;;){ int cx = hld.clus[x]; int l = cx == hld.clus[anc] ? hld.clusiind[anc] : 0; int r = hld.clusiind[x]; M = sts[cx].apply(l, r+1, M); if(cx == hld.clus[anc])break; x = par[hld.cluspath[cx][0]]; } } out.println(M[0][0] + " " + M[0][1] + " " + M[1][0] + " " + M[1][1]); } } } public static class SegmentTreeMatrix { public int M, H, N; public int[][][] node; public static int mod = 1000000007; public static long BIG = 8L*mod*mod; public static int S = 2; public SegmentTreeMatrix(int n) { N = n; M = Integer.highestOneBit(Math.max(N-1, 1))<<2; H = M>>>1; node = new int[M][][]; for(int i = 0;i < N;i++){ node[H+i] = new int[S][S]; node[H+i][0][0] = 1; node[H+i][1][1] = 1; } for(int i = H-1;i >= 1;i--)propagate(i); } private void propagate(int cur) { node[cur] = prop2(node[2*cur], node[2*cur+1], node[cur]); } private int[][] prop2(int[][] L, int[][] R, int[][] C) { if(L != null && R != null){ C = mul(R, L, C, mod); return C; }else if(L != null){ return prop1(L, C); }else if(R != null){ return prop1(R, C); }else{ return null; } } private int[][] prop1(int[][] L, int[][] C) { if(C == null){ // C = L; // read only C = new int[S][]; for(int i = 0;i < S;i++){ C[i] = Arrays.copyOf(L[i], S); } }else{ for(int i = 0;i < S;i++){ C[i] = Arrays.copyOf(L[i], S); } } return C; } public void update(int pos, int x0, int x1, int x2, int x3) { node[H+pos][0][0] = x0; node[H+pos][0][1] = x1; node[H+pos][1][0] = x2; node[H+pos][1][1] = x3; for(int i = H+pos>>>1;i >= 1;i>>>=1)propagate(i); } public int[][] apply(int l, int r, int[][] m){ return apply(l, r, 0, H, 1, m); } protected int[][] apply(int l, int r, int cl, int cr, int cur, int[][] m) { if(l <= cl && cr <= r){ return mul(node[cur], m, null, mod); }else{ int mid = cl+cr>>>1; if(mid < r && l < cr){ m = apply(l, r, mid, cr, 2*cur+1, m); } if(cl < r && l < mid){ m = apply(l, r, cl, mid, 2*cur, m); } return m; } } public static int[] mul(int[][] A, int[] v, int mod) { int m = A.length; int n = v.length; int[] w = new int[m]; for(int i = 0;i < m;i++){ long sum = 0; for(int k = 0;k < n;k++){ sum += (long)A[i][k] * v[k]; if(sum >= BIG)sum -= BIG; } w[i] = (int)(sum % mod); } return w; } public static int[][] mul(int[][] A, int[][] B, int[][] C, int mod) { assert A[0].length == B.length; int m = A.length; int n = A[0].length; int o = B[0].length; if(C == null)C = new int[m][o]; for(int i = 0;i < m;i++){ for(int j = 0;j < o;j++){ long sum = 0; for(int k = 0;k < n;k++){ sum += (long)A[i][k] * B[k][j]; if(sum >= BIG)sum -= BIG; } sum %= mod; C[i][j] = (int)sum; } } return C; } } public static class HeavyLightDecomposition { public int[][] g; public int[] clus; public int[][] cluspath; public int[] clusiind; public int[] par, dep; public int m; public HeavyLightDecomposition(int[][] g, int[] par, int[] ord, int[] dep) { init(g, par, ord, dep); } public void init(int[][] g, int[] par, int[] ord, int[] dep) { clus = decomposeToHeavyLight(g, par, ord); cluspath = clusPaths(clus, ord); clusiind = clusIInd(cluspath, g.length); this.m = cluspath.length; this.par = par; this.dep = dep; this.g = g; } public static int[] decomposeToHeavyLight(int[][] g, int[] par, int[] ord) { int n = g.length; int[] size = new int[n]; Arrays.fill(size, 1); for(int i = n-1;i > 0;i--)size[par[ord[i]]] += size[ord[i]]; int[] clus = new int[n]; Arrays.fill(clus, -1); int p = 0; for(int i = 0;i < n;i++){ int u = ord[i]; if(clus[u] == -1)clus[u] = p++; // centroid path (not heavy path) int argmax = -1; for(int v : g[u]){ if(par[u] != v && (argmax == -1 || size[v] > size[argmax]))argmax = v; } if(argmax != -1)clus[argmax] = clus[u]; } return clus; } public static int[][] clusPaths(int[] clus, int[] ord) { int n = clus.length; int[] rp = new int[n]; int sup = 0; for(int i = 0;i < n;i++){ rp[clus[i]]++; sup = Math.max(sup, clus[i]); } sup++; int[][] row = new int[sup][]; for(int i = 0;i < sup;i++)row[i] = new int[rp[i]]; for(int i = n-1;i >= 0;i--){ row[clus[ord[i]]][--rp[clus[ord[i]]]] = ord[i]; } return row; } public static int[] clusIInd(int[][] clusPath, int n) { int[] iind = new int[n]; for(int[] path : clusPath){ for(int i = 0;i < path.length;i++){ iind[path[i]] = i; } } return iind; } public int lca(int x, int y) { int rx = cluspath[clus[x]][0]; int ry = cluspath[clus[y]][0]; while(clus[x] != clus[y]){ if(dep[rx] > dep[ry]){ x = par[rx]; rx = cluspath[clus[x]][0]; }else{ y = par[ry]; ry = cluspath[clus[y]][0]; } } return clusiind[x] > clusiind[y] ? y : x; } public int ancestor(int x, int v) { while(x != -1){ if(v <= clusiind[x])return cluspath[clus[x]][clusiind[x]-v]; v -= clusiind[x]+1; x = par[cluspath[clus[x]][0]]; } return x; } // NOT VERIFIED // [iord[x], right[x]) public int[][] makeRights() { int root = -1; int n = g.length; for(int i = 0;i < n;i++)if(par[i] == -1)root = i; int[] ord = new int[n]; int[] right = new int[n]; int[] curs = new int[n]; int[] inds = new int[n]; int sp = 0, p = 0; curs[sp++] = root; while(sp > 0){ int cur = curs[sp-1]; int ind = inds[sp-1]; inds[sp-1]++; if(ind == 0){ ord[p++] = cur; for(int e : g[cur]){ if(par[cur] == e)continue; if(clus[cur] == clus[e]){ curs[sp] = e; inds[sp] = 0; sp++; break; } } }else if(ind-1 < g[cur].length){ int e = g[cur][ind-1]; if(e == par[cur])continue; if(clus[cur] == clus[e])continue; curs[sp] = e; inds[sp] = 0; sp++; }else{ right[cur] = p; sp--; } } int[] iord = new int[n]; for(int i = 0;i < n;i++)iord[ord[i]] = i; return new int[][]{ord, iord, right}; } ///// templates /** * xとその先祖ancについて経路x-ancでのクエリを処理するテンプレ * @param x * @param anc */ // public void query(int x, int anc) // { // int cx = hld.clus[x]; // cluster // int ind = hld.clusiind[x]; // pos in cluster // while(cx != hld.clus[anc]){ // // TODO クラスタcxの0~indについて逆向きにクエリ処理 // // st[cx].add(0, ind+1, v); // int con = par[hld.cluspath[cx][0]]; // ind = hld.clusiind[con]; // cx = hld.clus[con]; // } // // TODO clusiind[anc]~indについて逆向きにクエリ処理 // // st[cx].add(hld.clusiind[anc], ind+1, v); // } /** * 処理順を返す。[clus,from,to]でclusの[from,to]を処理する。from>toの場合もあるので注意。 * @param x * @param y * @return */ public int[][] query2(int x, int y) { int lca = lca(x, y); int[][] stack = new int[60][]; int sp = 0; int cx = clus[x]; // cluster int indx = clusiind[x]; // pos in cluster while(cx != clus[lca]){ stack[sp++] = new int[]{cx, indx, 0}; int con = par[cluspath[cx][0]]; indx = clusiind[con]; cx = clus[con]; } stack[sp++] = new int[]{cx, indx, clusiind[lca]}; // when considering edge query and you want to remove lca. // if(indx > clusiind[lca])stack[sp++] = new int[] { cx, indx, clusiind[lca]+1 }; int top = sp; int cy = clus[y]; // cluster int indy = clusiind[y]; // pos in cluster while(cy != clus[lca]){ stack[sp++] = new int[]{cy, 0, indy}; int con = par[cluspath[cy][0]]; indy = clusiind[con]; cy = clus[con]; } if(clusiind[lca] < indy){ stack[sp++] = new int[]{cy, clusiind[lca]+1, indy}; } for(int p = top, q = sp-1;p < q;p++,q--){ int[] dum = stack[p]; stack[p] = stack[q]; stack[q] = dum; } return Arrays.copyOf(stack, sp); } /* * query of coefficient * Epic Tree * 頂点に係数c_iが割り当てられ、x->lca->yのパス上に一律に+vと、パスの値の和を求めるテンプレ for(int xx : new int[]{x, y}){ int cx = clus[xx]; int ind = clusiind[xx]; while (cx != clus[lca]) { addFenwick(fts1[cx], 0, v); addFenwick(fts1[cx], ind+1, -v); addFenwick(fts0[cx], ind+1, cums[cx][ind+1]*v); int con = par[cluspath[cx][0]]; ind = clusiind[con]; cx = clus[con]; } addFenwick(fts0[cx], clusiind[lca], -v*cums[cx][clusiind[lca]]); addFenwick(fts0[cx], ind+1, v*cums[cx][ind+1]); addFenwick(fts1[cx], clusiind[lca], v); addFenwick(fts1[cx], ind+1, -v); } addFenwick(fts0[clus[lca]], clusiind[lca], v*cums[clus[lca]][clusiind[lca]]); addFenwick(fts0[clus[lca]], clusiind[lca]+1, -v*cums[clus[lca]][clusiind[lca]+1]); addFenwick(fts1[clus[lca]], clusiind[lca], -v); addFenwick(fts1[clus[lca]], clusiind[lca]+1, v); ========================================================================== long ret = 0; for(int xx : new int[]{x, y}){ int cx = clus[xx]; int ind = clusiind[xx]; while (cx != clus[lca]) { ret += sumFenwick(fts0[cx], ind); ret += sumFenwick(fts1[cx], ind)*cums[cx][ind+1]; ret %= mod; int con = par[cluspath[cx][0]]; ind = clusiind[con]; cx = clus[con]; } ret += sumFenwick(fts0[cx], ind); ret -= sumFenwick(fts0[cx], clusiind[lca]-1); ret += sumFenwick(fts1[cx], ind) * cums[cx][ind+1]; ret -= sumFenwick(fts1[cx], clusiind[lca]-1)*cums[cx][clusiind[lca]]; ret %= mod; } ret -= sumFenwick(fts0[clus[lca]], clusiind[lca]); ret += sumFenwick(fts0[clus[lca]], clusiind[lca]-1); ret -= sumFenwick(fts1[clus[lca]], clusiind[lca]) * cums[clus[lca]][clusiind[lca]+1]; ret += sumFenwick(fts1[clus[lca]], clusiind[lca]-1)*cums[clus[lca]][clusiind[lca]]; ret %= mod; if(ret < 0)ret += mod; */ } public static int[][] parents3(int[][] g, int root) { int n = g.length; int[] par = new int[n]; Arrays.fill(par, -1); int[] depth = new int[n]; depth[0] = 0; int[] q = new int[n]; q[0] = root; for (int p = 0, r = 1; p < r; p++) { int cur = q[p]; for (int nex : g[cur]) { if (par[cur] != nex) { q[r++] = nex; par[nex] = cur; depth[nex] = depth[cur] + 1; } } } return new int[][] { par, q, depth }; } static int[][] packU(int n, int[] from, int[] to) { int[][] g = new int[n][]; int[] p = new int[n]; for (int f : from) p[f]++; for (int t : to) p[t]++; for (int i = 0; i < n; i++) g[i] = new int[p[i]]; for (int i = 0; i < from.length; i++) { g[from[i]][--p[from[i]]] = to[i]; g[to[i]][--p[to[i]]] = from[i]; } return g; } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); // Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){ // @Override // public void run() { // long s = System.currentTimeMillis(); // solve(); // out.flush(); // if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); // } // }; // t.start(); // t.join(); } public static void main(String[] args) throws Exception { new E().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private long[] nal(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); return a; } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[][] nmi(int n, int m) { int[][] map = new int[n][]; for(int i = 0;i < n;i++)map[i] = na(m); return map; } private int ni() { return (int)nl(); } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }