結果
問題 | No.235 めぐるはめぐる (5) |
ユーザー | threepipes_s |
提出日時 | 2016-07-21 22:49:21 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 12,313 bytes |
コンパイル時間 | 3,349 ms |
コンパイル使用メモリ | 100,360 KB |
実行使用メモリ | 155,304 KB |
最終ジャッジ日時 | 2024-11-06 07:53:08 |
合計ジャッジ時間 | 12,430 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | AC | 1,729 ms
155,304 KB |
testcase_02 | AC | 2,044 ms
151,172 KB |
ソースコード
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.Closeable; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.PriorityQueue; import java.util.TreeMap; public class Main { static ContestScanner in;static Writer out;public static void main(String[] args) {try{in=new ContestScanner();out=new Writer();Main solve=new Main();solve.solve(); in.close();out.flush();out.close();}catch(IOException e){e.printStackTrace();}} static void dump(int[]a){for(int i=0;i<a.length;i++)out.print(a[i]+" ");out.println();} static void dump(int[]a,int n){for(int i=0;i<a.length;i++)out.printf("%"+n+"d ",a[i]);out.println();} static void dump(long[]a){for(int i=0;i<a.length;i++)out.print(a[i]+" ");out.println();} static void dump(char[]a){for(int i=0;i<a.length;i++)out.print(a[i]);out.println();} static long pow(long a,int n){long r=1;while(n>0){if((n&1)==1)r*=a;a*=a;n>>=1;}return r;} static String itob(int a,int l){return String.format("%"+l+"s",Integer.toBinaryString(a)).replace(' ','0');} static void sort(int[]a){m_sort(a,0,a.length,new int[a.length]);} static void sort(int[]a,int l){m_sort(a,0,l,new int[l]);} static void sort(int[]a,int l,int[]buf){m_sort(a,0,l,buf);} static void sort(int[]a,int s,int l,int[]buf){m_sort(a,s,l,buf);} static void m_sort(int[]a,int s,int sz,int[]b) {if(sz<7){for(int i=s;i<s+sz;i++)for(int j=i;j>s&&a[j-1]>a[j];j--)swap(a, j, j-1);return;} m_sort(a,s,sz/2,b);m_sort(a,s+sz/2,sz-sz/2,b);int idx=s;int l=s,r=s+sz/2;final int le=s+sz/2,re=s+sz; while(l<le&&r<re){if(a[l]>a[r])b[idx++]=a[r++];else b[idx++]=a[l++];} while(r<re)b[idx++]=a[r++];while(l<le)b[idx++]=a[l++];for(int i=s;i<s+sz;i++)a[i]=b[i]; } /* qsort(3.5s)<<msort(9.5s)<<<shuffle+qsort(17s)<Arrays.sort(Integer)(20s) */ static void sort(long[]a){m_sort(a,0,a.length,new long[a.length]);} static void sort(long[]a,int l){m_sort(a,0,l,new long[l]);} static void sort(long[]a,int l,long[]buf){m_sort(a,0,l,buf);} static void sort(long[]a,int s,int l,long[]buf){m_sort(a,s,l,buf);} static void m_sort(long[]a,int s,int sz,long[]b) {if(sz<7){for(int i=s;i<s+sz;i++)for(int j=i;j>s&&a[j-1]>a[j];j--)swap(a, j, j-1);return;} m_sort(a,s,sz/2,b);m_sort(a,s+sz/2,sz-sz/2,b);int idx=s;int l=s,r=s+sz/2;final int le=s+sz/2,re=s+sz; while(l<le&&r<re){if(a[l]>a[r])b[idx++]=a[r++];else b[idx++]=a[l++];} while(r<re)b[idx++]=a[r++];while(l<le)b[idx++]=a[l++];for(int i=s;i<s+sz;i++)a[i]=b[i];} static void swap(long[] a,int i,int j){final long t=a[i];a[i]=a[j];a[j]=t;} static void swap(int[] a,int i,int j){final int t=a[i];a[i]=a[j];a[j]=t;} static int binarySearchSmallerMax(int[]a,int v)// get maximum index which a[idx]<=v {int l=-1,r=a.length-1,s=0;while(l<=r){int m=(l+r)/2;if(a[m]>v)r=m-1;else{l=m+1;s=m;}}return s;} static int binarySearchSmallerMax(int[]a,int v,int l,int r) {int s=-1;while(l<=r){int m=(l+r)/2;if(a[m]>v)r=m-1;else{l=m+1;s=m;}}return s;} @SuppressWarnings("unchecked") static List<Integer>[]createGraph(int n) {List<Integer>[]g=new List[n];for(int i=0;i<n;i++)g[i]=new ArrayList<>();return g;} void solve() throws NumberFormatException, IOException{ int n = in.nextInt(); int[] s = new int[n]; int[] c = new int[n]; for(int i=0; i<n; i++) s[i] = in.nextInt(); for(int i=0; i<n; i++) c[i] = in.nextInt(); HLD_light hld = new HLD_light(n, s, c); for(int i=0; i<n-1; i++){ int a = in.nextInt()-1; int b = in.nextInt()-1; hld.edge(a, b); } hld.decomp(0); hld.graph(0); int q = in.nextInt(); for(int i=0; i<q; i++){ final int query = in.nextInt(); final int x = in.nextInt()-1; final int y = in.nextInt()-1; if(query==0){ final int z = in.nextInt(); hld.add(x, y, z); }else{ out.println(hld.sum(x, y)); } } } } class SegTree{ final long mod = 1000000007; long[] tree; int size; int len; public SegTree(int n){ int beki = 1; while(beki < n) beki <<= 1; len = beki<<1; tree = new long[len]; size = beki; } public void add(int id, int x){ for(int i=id|size; i>0; i>>=1) tree[i] += x; } } class LazySegTree{ // benchmark: (n=10^5, q=2*10^5) * 100try --- 13772ms ( 137ms/try) // : (n=10^6, q=2*10^6) * 10try --- 18512ms (1851ms/try) SegTree st; final long mod = 1000000007; long[] value; long[] hold; int length, n, bits; public LazySegTree(int n) { for(length=1; length<n; length<<=1) bits++; length <<= 1; value = new long[length]; hold = new long[length]; st = new SegTree(n); } void setS(int idx, int s){ add(idx, idx, s); } void setC(int idx, int c){ st.add(idx, c); } void add(int l, int r, long v){ // r: inclusive add(l, r, v, bits, 0); } private void add(int l, int r, long v, int shift, int idx){ if(r<l) return; final int index = idx|1<<bits-shift; if(r-l+1<(1<<shift)){ final int nidx = idx<<1|1; add(l, Math.min(r, (nidx<<shift-1)-1), v, shift-1, nidx-1); add(Math.max(l, nidx<<shift-1), r, v, shift-1, nidx); value[index] = (value[index<<1]+value[index<<1|1] +hold[index]*st.tree[index]%mod)%mod; }else{ value[index] = (value[index]+v*st.tree[index]%mod)%mod; hold[index] = (hold[index]+v)%mod; } } long sum(int l, int r){ return sum(l, r, bits, 0, 0); } private long sum(int l, int r, int shift, int idx, long add){ final int index = idx|1<<bits-shift; if(r<l){ hold[index] = (hold[index]+add)%mod; value[index] = (value[index]+add*st.tree[index]%mod)%mod; return 0; } value[index] = (value[index]+add*st.tree[index]%mod)%mod; if(r-l+1<(1<<shift)){ final int nidx = idx<<1|1; final long next = (hold[index]+add)%mod; hold[index] = 0; return sum(l, Math.min(r, (nidx<<shift-1)-1), shift-1, nidx-1, next) + sum(Math.max(l, nidx<<shift-1), r, shift-1, nidx, next); }else{ hold[index] = (hold[index]+add)%mod; return value[index]; } } } class HLD_light{ List<Integer>[] node; int[] child; int[] len; int n; int size; int[] s, c; HLD_light(int n, int[] s, int[] c) { node = new List[n]; for(int i=0; i<n; i++) node[i] = new ArrayList<>(); init(n); this.s = s; this.c = c; } public HLD_light(List<Integer>[] node) { this.node = node; init(node.length); } void init(int n){ this.n = n; child = new int[n]; len = new int[n]; Arrays.fill(child, -1); st = new int[n]; pst = new int[n]; } void edge(int v, int u){ node[v].add(u); node[u].add(v); } int[] st, pst; void decomp(int root){ boolean[] back = new boolean[n]; int sp = 0; pst[0] = root; st[sp++] = root; size = node[root].size(); while(sp>0){ int p = st[--sp]; int par = pst[sp]; boolean b = back[sp]; if(b){ if(len[p]+1>len[par] && p!=root){ child[par] = p; len[par] = len[p]+1; } }else{ len[p] = 1; back[sp++] = true; if(node[p].size()>1 && p!=root) size += node[p].size()-2; for(int v: node[p]){ if(v==par) continue; st[sp] = v; pst[sp] = p; back[sp] = false; sp++; } } } } int[][] vertex; LazySegTree[] lst; int[] idx, pidx; int[] parent, dep; void graph(int root){ int[] isp = new int[size]; idx = new int[n]; pidx = new int[n]; int number = 0; vertex = new int[size][]; // lst = new LazySegTree[size]; parent = new int[size]; dep = new int[size]; int sp = 0; pst[0] = root; st[sp++] = root; vertex[0] = new int[len[root]]; isp[0]++; while(sp>0){ int p = st[--sp]; int par = pst[sp]; for(int v: node[p]){ if(v==par || v==child[p]) continue; idx[v] = ++number; pidx[v] = isp[number]; dep[number] = dep[idx[p]]+1; parent[number] = p; // child to parent vertex[number] = new int[len[v]]; vertex[number][isp[number]++] = v; st[sp] = v; pst[sp] = p; sp++; } if(child[p]>=0){ int cld = child[p]; idx[cld] = idx[p]; pidx[cld] = isp[idx[cld]]; vertex[idx[cld]][isp[idx[cld]]++] = cld; st[sp] = cld; pst[sp] = p; sp++; } } lst = new LazySegTree[size]; for(int i=0; i<size; i++){ final int len = vertex[i].length; lst[i] = new LazySegTree(len); Arrays.fill(lst[i].st.tree, 1); for(int j=0; j<len; j++){ lst[i].setS(j, s[vertex[i][j]]); } } for(int i=0; i<size; i++){ final int len = vertex[i].length; Arrays.fill(lst[i].st.tree, 0); for(int j=0; j<len; j++){ lst[i].setC(j, c[vertex[i][j]]); } } } void add(int h, int l, int z){ while(idx[h] != idx[l]){ if(dep[idx[h]]>dep[idx[l]]){ h = h^l; l = h^l; h = h^l; } lst[idx[l]].add(0, pidx[l], z); l = parent[idx[l]]; } if(pidx[h] > pidx[l]){ h = h^l; l = h^l; h = h^l; } lst[idx[h]].add(pidx[h], pidx[l], z); } final long mod = 1000000007; int sum(int h, int l){ long res = 0; while(idx[h] != idx[l]){ if(dep[idx[h]]>dep[idx[l]]){ h = h^l; l = h^l; h = h^l; } res = (res+lst[idx[l]].sum(0, pidx[l]))%mod; l = parent[idx[l]]; } if(pidx[h] > pidx[l]){ h = h^l; l = h^l; h = h^l; } return (int)((res + lst[idx[h]].sum(pidx[h], pidx[l]))%mod); } } @SuppressWarnings("serial") class MultiSet<T> extends HashMap<T, Integer>{ @Override public Integer get(Object key){return containsKey(key)?super.get(key):0;} public void add(T key,int v){put(key,get(key)+v);} public void add(T key){put(key,get(key)+1);} public void sub(T key){final int v=get(key);if(v==1)remove(key);else put(key,v-1);} public MultiSet<T> merge(MultiSet<T> set) {MultiSet<T>s,l;if(this.size()<set.size()){s=this;l=set;}else{s=set;l=this;} for(Entry<T,Integer>e:s.entrySet())l.add(e.getKey(),e.getValue());return l;} } @SuppressWarnings("serial") class OrderedMultiSet<T> extends TreeMap<T, Integer>{ @Override public Integer get(Object key){return containsKey(key)?super.get(key):0;} public void add(T key,int v){put(key,get(key)+v);} public void add(T key){put(key,get(key)+1);} public void sub(T key){final int v=get(key);if(v==1)remove(key);else put(key,v-1);} public OrderedMultiSet<T> merge(OrderedMultiSet<T> set) {OrderedMultiSet<T>s,l;if(this.size()<set.size()){s=this;l=set;}else{s=set;l=this;} while(!s.isEmpty()){l.add(s.firstEntry().getKey(),s.pollFirstEntry().getValue());}return l;} } class Pair implements Comparable<Pair>{ int a,b;final int hash;Pair(int a,int b){this.a=a;this.b=b;hash=(a<<16|a>>16)^b;} public boolean equals(Object obj){Pair o=(Pair)(obj);return a==o.a&&b==o.b;} public int hashCode(){return hash;} public int compareTo(Pair o){if(a!=o.a)return a<o.a?-1:1;else if(b!=o.b)return b<o.b?-1:1;return 0;} } class Timer{ long time;public void set(){time=System.currentTimeMillis();} public long stop(){return time=System.currentTimeMillis()-time;} public void print(){System.out.println("Time: "+(System.currentTimeMillis()-time)+"ms");} @Override public String toString(){return"Time: "+time+"ms";} } class Writer extends PrintWriter{ public Writer(String filename)throws IOException {super(new BufferedWriter(new FileWriter(filename)));} public Writer()throws IOException{super(System.out);} } class ContestScanner implements Closeable{ private BufferedReader in;private int c=-2; public ContestScanner()throws IOException {in=new BufferedReader(new InputStreamReader(System.in));} public ContestScanner(String filename)throws IOException {in=new BufferedReader(new InputStreamReader(new FileInputStream(filename)));} public String nextToken()throws IOException { StringBuilder sb=new StringBuilder(); while((c=in.read())!=-1&&Character.isWhitespace(c)); while(c!=-1&&!Character.isWhitespace(c)){sb.append((char)c);c=in.read();} return sb.toString(); } public String readLine()throws IOException{ StringBuilder sb=new StringBuilder();if(c==-2)c=in.read(); while(c!=-1&&c!='\n'&&c!='\r'){sb.append((char)c);c=in.read();} return sb.toString(); } public long nextLong()throws IOException,NumberFormatException {return Long.parseLong(nextToken());} public int nextInt()throws NumberFormatException,IOException {return(int)nextLong();} public double nextDouble()throws NumberFormatException,IOException {return Double.parseDouble(nextToken());} public void close() throws IOException {in.close();} }