結果
問題 | No.900 aδδitivee |
ユーザー | fgwiebfaoish |
提出日時 | 2020-03-03 09:20:56 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 587 ms / 2,000 ms |
コード長 | 7,193 bytes |
コンパイル時間 | 944 ms |
コンパイル使用メモリ | 109,056 KB |
実行使用メモリ | 57,468 KB |
最終ジャッジ日時 | 2024-10-13 21:44:42 |
合計ジャッジ時間 | 16,316 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 27 ms
18,176 KB |
testcase_01 | AC | 27 ms
18,304 KB |
testcase_02 | AC | 26 ms
18,176 KB |
testcase_03 | AC | 26 ms
18,176 KB |
testcase_04 | AC | 26 ms
18,304 KB |
testcase_05 | AC | 27 ms
18,304 KB |
testcase_06 | AC | 26 ms
18,432 KB |
testcase_07 | AC | 563 ms
57,084 KB |
testcase_08 | AC | 587 ms
57,324 KB |
testcase_09 | AC | 575 ms
57,328 KB |
testcase_10 | AC | 572 ms
57,460 KB |
testcase_11 | AC | 572 ms
57,316 KB |
testcase_12 | AC | 577 ms
57,204 KB |
testcase_13 | AC | 566 ms
57,468 KB |
testcase_14 | AC | 584 ms
57,208 KB |
testcase_15 | AC | 566 ms
57,208 KB |
testcase_16 | AC | 579 ms
57,204 KB |
testcase_17 | AC | 576 ms
57,212 KB |
testcase_18 | AC | 579 ms
57,200 KB |
testcase_19 | AC | 570 ms
57,460 KB |
testcase_20 | AC | 580 ms
57,332 KB |
testcase_21 | AC | 584 ms
57,316 KB |
testcase_22 | AC | 413 ms
52,092 KB |
testcase_23 | AC | 417 ms
52,476 KB |
testcase_24 | AC | 415 ms
52,336 KB |
testcase_25 | AC | 425 ms
52,332 KB |
testcase_26 | AC | 408 ms
52,208 KB |
testcase_27 | AC | 413 ms
52,348 KB |
testcase_28 | AC | 417 ms
52,352 KB |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System; using System.Collections.Generic; using System.Collections; using System.Collections.Specialized; using System.Linq; using System.Text; using System.IO; using System.Reflection; using static System.Math; using System.Numerics; static class Program{ const int mod=(int)1e9+7; static List<int>[] li; static bool[] b; static void Main(){ Sc sc=new Sc(); var s=sc.Ia; int n=s[0],m=n-1; li=new List<int>[n+1]; b=new bool[n+1]; var h=new long[n+1]; for(int i=1;i<=n;i++){li[i]=new List<int>();} for(int i=0;i<m;i++){ var e=sc.Ia; li[e[0]+1].Add(e[1]+1); li[e[1]+1].Add(e[0]+1); h[e[1]+1]+=e[2]; } Hld hl=new Hld(li,1,false); for(int i = 1;i<=n;i++) {hl.Ud(i,h[i]);} int q=sc.I; StringBuilder sb=new StringBuilder(); for(int i = 0;i<q;i++) { var e=sc.Ia; if(e[0]==1){hl.Ur(e[1]+1,e[2]);} else{sb.Append(hl.Get(1,e[1]+1)+"\n");} } Console.Write(sb); } } public class Hld{ private List<int>[] li; private List<int> lir; private List<List<int>> lia=new List<List<int>>(),lib=new List<List<int>>(); private int[] da,ea,fa,ia,va; private St[] st; private St stt; private int n,m=0,o,r,f=0,hc=0; public Hld(List<int>[] li,int o,bool bo){ if(!bo){hc=1;} this.li=li; n=li.Length; this.o=o; fa=new int[n]; da=new int[n]; ea=new int[n]; va=new int[n]; lir=new List<int>(); Fu1(o); r=da[o]; fa=new int[m]; ia=new int[m]; st=new St[m]; stt=new St(m); for(int i = 0;i<m;i++) { st[i]=new St(lia[i].Count); } Fu2(r,0); } public void Ur(int a,long d){ if(ea[a]-hc>=0){st[da[a]].Ud2(0,ea[a]-hc,d);} if(va[a]!=-1){stt.Ud2(ia[da[a]]+1,va[a],d);} } public void Ud(int a,long d){ st[da[a]].Ud(ea[a],d); } public long Get(int a,int b){ long p=0; int af=fa[da[a]],bf=fa[da[b]]; while(af>bf){ p=Fg(p,da[a],ea[a],lia[da[a]].Count-1); p=Fgt(p,ia[da[a]],lia[da[a]].Count-ea[a]); a=lir[da[a]]; af--; } while(af<bf){ p=Fg(p,da[b],ea[b],lia[da[b]].Count-1); p=Fgt(p,ia[da[b]],lia[da[b]].Count-ea[b]); b=lir[da[b]]; bf--; } while(da[a]!=da[b]){ p=Fg(p,da[a],ea[a],lia[da[a]].Count-1); p=Fg(p,da[b],ea[b],lia[da[b]].Count-1); p=Fgt(p,ia[da[a]],lia[da[a]].Count-ea[a]); p=Fgt(p,ia[da[b]],lia[da[b]].Count-ea[b]); a=lir[da[a]]; b=lir[da[b]]; } if(ea[a]<ea[b]){ p=Fg(p,da[a],ea[a],ea[b]-hc); p=Fgt(p,ia[da[a]],ea[b]-ea[a]+1-hc); } else{ p=Fg(p,da[a],ea[b],ea[a]-hc); p=Fgt(p,ia[da[a]],ea[a]-ea[b]+1-hc); } return p; } private long Fg(long p,int x,int a,int b){ return p+st[x].Get(a,b); } private long Fgt(long p,int a,int c){ return p+stt.Get(a,a)*c; } public void Ud2(int a,int b,long d){ int af=fa[da[a]],bf=fa[da[b]]; while(af>bf){ st[da[a]].Ud2(ea[a],lia[da[a]].Count-1,d); a=lir[da[a]]; af--; } while(af<bf){ st[da[b]].Ud2(ea[b],lia[da[b]].Count-1,d); b=lir[da[b]]; bf--; } while(da[a]!=da[b]){ st[da[a]].Ud2(ea[a],lia[da[a]].Count-1,d); st[da[b]].Ud2(ea[b],lia[da[b]].Count-1,d); a=lir[da[a]]; b=lir[da[b]]; } if(ea[a]<ea[b]){st[da[a]].Ud2(ea[a],ea[b]-hc,d);} else{st[da[a]].Ud2(ea[b],ea[a]-hc,d);} } private void Fu2(int a,int g){ fa[a]=g; ia[a]=f; f++; int j=0,p=-1; for(int i=0;i<lib[a].Count;i++){ Fu2(lib[a][i],g+1); while(lia[a][j]!=lir[lib[a][i]]){va[lia[a][j]]=p;j++;} p=f-1; va[lia[a][j]]=p; } while(j<lia[a].Count){va[lia[a][j]]=p;j++;} } private void Fu1(int a){ fa[a]=1; da[a]=-1; int p=0; for(int i=0;i<li[a].Count;i++){ if(fa[li[a][i]]==0){ Fu1(li[a][i]); if(fa[p]<fa[li[a][i]]){p=li[a][i];} fa[a]+=fa[li[a][i]]; } } if(fa[a]==1){ lia.Add(new List<int>()); lib.Add(new List<int>()); lir.Add(0); lia[m].Add(a); da[a]=m; m++; } else{ ea[a]=lia[da[p]].Count; lia[da[p]].Add(a); da[a]=da[p]; if(li[a].Count>2||o==a){ for(int i=0;i<li[a].Count;i++){ if(da[li[a][i]]!=-1&&p!=li[a][i]){ lir[da[li[a][i]]]=a; lib[da[a]].Add(da[li[a][i]]); } } } } } } public class St{ private long[] dat,dat2; private int m=1,cnt=0; public St(int n){ while(m<n){m<<=1;} cnt=m+n-1; dat=new long[cnt]; dat2=new long[cnt+1]; } public void Ud(int q,long b){ q+=m-1; dat[q]+=b; while(q>0){dat[(q-1)>>1]=dat[(q-1)>>1]+b;q=(q-1)>>1;} } private long Fdg(int a,int b,int k,int l,int r){ if(dat2[k]!=0){ dat[k]+=dat2[k]; if(2*k+1<cnt){dat2[2*k+1]+=dat2[k]>>1;} if(2*k+2<cnt){dat2[2*k+2]+=dat2[k]>>1;} dat2[k]=0; } if(r<a||b<l){return 0;} if(a<=l&&r<=b){return dat[k];} else{return Fdg(a,b,k*2+1,l,(l+r)>>1)+Fdg(a,b,k*2+2,(l+r+1)>>1,r);} } public long Get(int a,int b){return Fdg(a,b,0,0,m-1);} public void Ud2(int a,int b,long d){Hn(a,b,0,0,m-1,d*m);} private long Hn(int a,int b,int k,int l,int r,long d){ if(a<=l&&r<=b){dat2[k]+=d;return d;} else if(r>=a&&b>=l){ long q=Hn(a,b,k*2+1,l,(l+r)>>1,d>>1)+Hn(a,b,k*2+2,(l+r+1)>>1,r,d>>1); dat[k]+=q; return q; } return 0; } } public class Sc{ public int I{get{return int.Parse(Console.ReadLine());}} public long L{get{return long.Parse(Console.ReadLine());}} public double D{get{return double.Parse(Console.ReadLine());}} public string S{get{return Console.ReadLine();}} public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}} public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}} public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}} public string[] Sa{get{return Console.ReadLine().Split();}} public object[] Oa{get{return Console.ReadLine().Split();}} public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}} public int[] Ia3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),int.Parse);} public int[] Ia3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),int.Parse);} public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}} public long[] La3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),long.Parse);} public long[] La3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),long.Parse);} public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}} public double[] Da3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),double.Parse);} public double[] Da3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),double.Parse);} public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;} public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;} public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;} public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;} }