QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#687775 | #9492. 树上简单求和 | xlwang | 5 | 79ms | 78168kb | C++14 | 8.1kb | 2024-10-29 21:09:54 | 2024-10-29 21:09:54 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define fr(i,j,k) for(register int i=j;i<=k;++i)
#define rf(i,j,k) for(register int i=j;i>=k;--i)
#define foredge(i,j) for(register int i=head[j];i;i=e[i].nxt)
#define pb push_back
#define Times printf("Time:%.3lf\n",clock()/CLOCKS_PER_SEC)
#define pii pair<int,int>
#define mk make_pair
using namespace std;
inline int read(){
int x=0;
bool f=0;
char c=getchar();
while(!isdigit(c)) f|=(c=='-'),c=getchar();
while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
return f?-x:x;
}
inline void write(int x){
if(x<0){putchar('-');x=-x;}
if(x>9)write(x/10);
putchar(x%10+'0');
}
inline void writeln(int x){write(x); puts("");}
inline void writepl(int x){write(x); putchar(' ');}
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline int randfind(int l,int r){return rnd()%(r-l+1)+l;}
//inline void init(){
// int t=read();
// while(t--) work();
//}
const int Maxn=2e5+10,B=500;
int n,q;
ull a[Maxn];
int toid[Maxn];
struct sgt{
ull s[Maxn<<2],tag[Maxn<<2];
inline int ls(int x){return x<<1;}
inline int rs(int x){return x<<1|1;}
inline void pushup(int x){s[x]=s[ls(x)]+s[rs(x)];}
inline void change(int x,int l,int r,ull k){s[x]+=(r-l+1)*k;tag[x]+=k;}
inline void pushdown(int x,int l,int r){
if(tag[x]){
int mid;mid=(l+r)>>1;
change(ls(x),l,mid,tag[x]);
change(rs(x),mid+1,r,tag[x]);
tag[x]=0;
}
}
inline void update(int ql,int qr,int l,int r,int x,ull k){
// cout<<ql<<' '<<qr<<' '<<l<<' '<<r<<' '<<x<<' '<<k<<endl;
if(ql<=l && r<=qr) {change(x,l,r,k);return;}
pushdown(x,l,r);int mid;mid=(l+r)>>1;
if(ql<=mid) update(ql,qr,l,mid,ls(x),k);
if(mid<qr) update(ql,qr,mid+1,r,rs(x),k);
pushup(x);
}
inline ull query(int ql,int qr,int l,int r,int x){
if(ql<=l && r<=qr) return s[x];
int mid;mid=(l+r)>>1;pushdown(x,l,r);ull ans=0;
if(ql<=mid) ans+=query(ql,qr,l,mid,ls(x));
if(mid<qr) ans+=query(ql,qr,mid+1,r,rs(x));
return ans;
}
inline void clear(int x,int l,int r){
s[x]=tag[x]=0;
if(l==r) return;
int mid;mid=(l+r)>>1;
clear(ls(x),l,mid);clear(rs(x),mid+1,r);
}
}SGT;
struct BLOCK{
ull S[Maxn],s[Maxn];
int id[Maxn];
int L[Maxn],R[Maxn];
inline void into(){
fr(i,1,n){
id[i]=(i-1)/B+1;
if(!L[id[i]]) L[id[i]]=i;
R[id[i]]=i;
}
// fr(i,1,n){
// s[i]=a[toid[i]];
// if(i!=L[id[i]]) s[i]+=s[i-1];
// }
// fr(i,1,n) if(id[i]!=id[n]) S[id[i]+1]+=a[toid[i]];
// fr(i,1,id[n]) S[i]+=S[i-1];
}
inline void update(int x,ull k){
// cout<<x<<' '<<k<<endl;
fr(i,x,R[id[x]]) s[i]+=k;
fr(i,id[x]+1,id[n]) S[i]+=k;
}
inline ull getans(int x){return s[x]+S[id[x]];}
inline ull query(int l,int r){if(l>r) return 0;return getans(r)-getans(l-1);}
}BL;
struct Tree{
vector<int> vc[Maxn];
int f[20][Maxn<<1],dep[Maxn];
int dfn[Maxn],idx;
int num,id[Maxn];
int lg[Maxn<<1];
int father[Maxn];
int siz[Maxn];
inline void dfs(int x,int fa){
dep[x]=dep[fa]+1;f[0][++idx]=x;dfn[x]=idx;father[x]=fa;id[x]=++num;siz[x]=1;
for(auto y:vc[x]) if(y!=fa){
dfs(y,x);
f[0][++idx]=x;siz[x]+=siz[y];
}
}
inline int getmin(int x,int y){if(dep[x]<dep[y]) return x;return y;}
inline int getlca(int x,int y){
x=dfn[x],y=dfn[y];if(x>y) swap(x,y);
int ln=lg[y-x+1];
// cout<<ln<<' '<<x<<' '<<y<<' '<<f[ln][x]<<' '<<f[ln][y-(1<<ln)+1]<<endl;
return getmin(f[ln][x],f[ln][y-(1<<ln)+1]);
}
inline void Read(){
fr(i,1,n-1){
int x,y;
cin>>x>>y;
vc[x].pb(y);vc[y].pb(x);
}
dfs(1,0);
lg[0]=-1;fr(i,1,idx) lg[i]=lg[i/2]+1;
fr(j,1,lg[idx]) fr(i,1,idx){
if(i+(1<<j)-1>idx) break;
f[j][i]=getmin(f[j-1][i],f[j-1][i+(1<<(j-1))]);
// cout<<j<<' '<<i<<' '<<f[j][i]<<endl;
}
}
}T1,T2;
int vis[Maxn];
vector<int> ID;
struct node{int x,y;ull z;}que[Maxn];
ull ans[Maxn];
inline bool cmp(int a,int b){return T2.id[a]<T2.id[b];}
inline void init(){
cin>>n>>q;
fr(i,1,n) cin>>a[i];
T1.Read();T2.Read();
// fr(i,1,n) vis[i]=1;
fr(i,1,n) SGT.update(T2.id[i],T2.id[i]+T2.siz[i]-1,1,n,1,a[i]);
fr(i,1,n) ID.pb(i);
shuffle(ID.begin(),ID.end(),rnd);
// fr(i,0,min(n-1,B)) vis[ID[i]]=1;vis[1]=1;
ID.clear();
fr(i,1,n) vis[i]=1;
fr(i,1,n) if(vis[i]) ID.pb(i);
sort(ID.begin(),ID.end(),cmp);
int ln=ID.size();
fr(i,0,ln-2) ID.pb(T2.getlca(ID[i],ID[i+1]));
sort(ID.begin(),ID.end(),cmp);
ID.erase(unique(ID.begin(),ID.end()),ID.end());
for(auto x:ID) vis[x]=1;
fr(i,1,q) cin>>que[i].x>>que[i].y>>que[i].z;
BL.into();
}
vector<pii> vc[Maxn];
inline void update(int x,int y,ull z){
int llca=T1.getlca(x,y);
// cout<<llca<<endl;
BL.update(T1.id[x],z);BL.update(T1.id[y],z);
BL.update(T1.id[llca],-2*z);
// cout<<"**"<<endl;
SGT.update(T2.id[llca],T2.id[llca]+T2.siz[llca]-1,1,n,1,z);
a[llca]+=z;
}
inline ull query(int x){return BL.query(T1.id[x],T1.id[x]+T1.siz[x]-1);}
inline ull getval(int x,int y){
int llca=T2.getlca(x,y);
ull ans=0;
ans=SGT.query(T2.id[x],T2.id[x],1,n,1);
ans+=SGT.query(T2.id[y],T2.id[y],1,n,1);
ans-=2*SGT.query(T2.id[llca],T2.id[llca],1,n,1);
ans+=a[llca];
return ans;
}
int id1[Maxn];
inline void query(int x,int y,int id){
// cout<<"query:"<<x<<' '<<y<<' '<<id<<endl;
ans[id]=getval(x,y);
// cout<<"ans:"<<ans[id]<<endl;
while(1){
// cout<<x<<' '<<y<<endl;
if(vis[x] && vis[y]) break;
if(x==y){
ans[id]+=query(x),x=T2.father[x];
return;
}
if(vis[y]) ans[id]+=query(x),x=T2.father[x];
else if(vis[x]) ans[id]+=query(y),y=T2.father[y];
else {
if(T2.dep[x]<T2.dep[y]) swap(x,y);
// cout<<x<<endl;
ans[id]+=query(x),x=T2.father[x];
// cout<<"ans:"<<ans[id]<<endl;
}
}
int llca=T2.getlca(x,y);
// cout<<"llca:"<<x<<' '<<y<<' '<<llca<<endl;
vc[x].pb(mk(id,1));vc[y].pb(mk(id,1));vc[llca].pb(mk(id,-2));
id1[id]=llca;
}
inline void Update(int x,int y,ull z){
// cerr<<x<<' '<<y<<' '<<z<<endl;
SGT.update(T1.id[x],T1.id[x],1,n,1,z);
SGT.update(T1.id[y],T1.id[y],1,n,1,z);
int llca=T1.getlca(x,y);
SGT.update(T1.id[llca],T1.id[llca],1,n,1,-2*z);
// cerr<<"end\n";
}
inline ull getpoint(int x){return SGT.query(T1.id[x],T1.id[x]+T1.siz[x]-1,1,n,1)+a[x];}
int fl[Maxn],s[Maxn];
inline void dfs(int x,int fa){
s[x]=s[fa]+fl[x];
for(auto y:T1.vc[x]) if(y!=fa) dfs(y,x);
}
ull Sum=0;
inline void Add(int x,int y,ull z){
int sum=0,llca=T1.getlca(x,y);
sum=s[x]+s[y]-2*s[llca];
Sum+=z*sum;
}
inline void into(int X){
// cout<<"into:"<<X<<endl;
int pre=X;
fr(i,1,n) fl[i]=0;Sum=0;
while(X) fl[X]=1,X=T2.father[X];
dfs(1,0);
int tim=0;
for(auto p:vc[pre]){
fr(i,tim+1,p.first) Add(que[i].x,que[i].y,que[i].z);
ans[p.first]+=Sum*p.second;
tim=p.first;
}
}
inline void work(){
// cout<<"dfn:\n";
// fr(i,1,n) cout<<i<<' '<<T1.id[i]<<endl;
fr(i,1,q) update(que[i].x,que[i].y,que[i].z),query(que[i].x,que[i].y,i);
// cerr<<"**"<<endl;
SGT.clear(1,1,n);fr(i,1,n) a[i]=0;
// cerr<<"**"<<endl;
// cout<<ans[1]<<endl;
fr(i,1,q) Update(que[i].x,que[i].y,que[i].z),ans[i]+=getpoint(id1[i]);
// cout<<ans[1]<<endl;
for(auto x:ID) into(x);
fr(i,1,q) cout<<ans[i]<<'\n';
}
signed main(){
// freopen("input.in","r",stdin);
// freopen("output.out","w",stdout);
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
init();work();
// printf("\nTIME:%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}
详细
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 79ms
memory: 78012kb
input:
3000 3000 7236742292501328495 17973811477309806363 16075782662531676171 17971236571771878676 11392080645527132110 3685563455925680459 9773593720088356683 8313828403245053795 7736401634567449043 1634817828009987181 6951124933529719486 12775126714635387213 15460977209223753216 397573676785925632 31372...
output:
12105153858659381124 18367442707572066757 11668241962484097878 11288238120352358472 1742468310074622166 9942835997686093671 3305677510569607477 17741602000425004088 14984128302052618266 1075081718074605786 6509217537832509095 16750513627843273113 8569443169249732820 14475184194298579044 156111071108...
result:
ok 3000 lines
Test #2:
score: 5
Accepted
time: 77ms
memory: 75984kb
input:
3000 3000 1612333876155866602 8538417838700679227 6080765231437578796 17905224638340228394 12270907925903144224 17944105326358594564 17302041033966840611 1006351124625222126 496336153231744288 9393087977687876980 9553975238547373621 9361882717200384390 15051881329169144319 9757999873162420435 882725...
output:
11133131376095771981 7909873024850695144 16250639243139481926 14562550655578101207 8274205996508264973 178549413271904466 2368406276743327913 7464009386554813982 9439464815411774627 1471756740732097060 15201641099137019227 6774030298556871576 18156105511913219667 1553508745644446823 4225137078364117...
result:
ok 3000 lines
Test #3:
score: 5
Accepted
time: 74ms
memory: 76080kb
input:
3000 3000 9709246061666095435 1861649101703072889 10620139893353930613 17635186539135419482 710209455559527146 6075101384669982511 1120305006358459674 9703156967435388252 1397046737759839382 5259056712870179169 8253156305433022999 710199693203327302 15130650033641744675 10720111924616886955 15543351...
output:
7834604406305153073 5037061270969117785 16481572776620825702 15177894197606565804 3120320619896892806 18008650876379132344 7417108723176816402 13515164814425439399 3299769942258542105 15897528270699011770 11642805469843844638 16764682282380318054 4824039114054405772 4859834102876213962 1234210473247...
result:
ok 3000 lines
Test #4:
score: 5
Accepted
time: 77ms
memory: 78156kb
input:
3000 3000 16538965545220923528 18062192327708400751 10422465150728338588 3471522151129113073 1236650672072793692 1942240200040301168 13090729759591037952 15335798523677372669 9912100622761466753 11177948788405690381 3710859061697501523 4984944638666762977 17278589713462878008 6371292801024547050 868...
output:
8182453933067329108 13535217473847106938 17067385337010269798 3806121648880466130 11322569288575153037 11079197311131660121 9670138330007803226 6554062218199796758 965954569567598779 18055887214749050688 6142620503089407421 8690117812667761187 9547139298346295115 8890987597519353054 1755036654049586...
result:
ok 3000 lines
Test #5:
score: 5
Accepted
time: 78ms
memory: 78168kb
input:
3000 3000 17759588706587888497 10550000524636484378 11601004513528075994 7150322911283804521 4459707248078569712 10692395730842402625 8940418793863522991 12967068928670540447 9954278250450015940 13702413838608801301 10598390500439869870 15110245227553613794 490862872212325709 15164980555660957366 94...
output:
9743736929788175512 16812303667256960040 14694223512340829897 550204232580650311 1175342872438242313 17622261358285047637 7413682703975031220 12643066512274062227 1868985217436232595 5471830334855681322 8070132260376389587 3970361922096052085 218281824643752746 991917103472727104 2960248244218479023...
result:
ok 3000 lines
Subtask #2:
score: 0
Time Limit Exceeded
Dependency #1:
100%
Accepted
Test #6:
score: 12
Accepted
time: 3ms
memory: 48856kb
input:
5 7 0 3 2 6 4 1 2 2 4 1 5 5 3 3 4 4 2 2 5 5 1 5 3 0 3 2 5 4 4 4 4 4 3 5 2 0 3 4 3 5 5 6
output:
15 21 10 13 17 26 18
result:
ok 7 lines
Test #7:
score: 0
Time Limit Exceeded
input:
70000 70000 3805295436278888199 9842309351516174725 1566744796319231180 2206519284152256579 2715928675931950447 6346821976624501261 16020972671480798719 14702021753902144915 17127828773798978442 15779168055669690475 4964561323934614661 9395102787554964450 6377076753365184543 15167378195767668817 288...
output:
result:
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Time Limit Exceeded
Test #21:
score: 0
Time Limit Exceeded
input:
200000 200000 622783158027686223 2242697872372232537 8481648430436878777 10092474834140799044 15403999682625301609 12614289513474949582 9180944589267018841 7823784919308285798 8257785171198951273 5134508521895120821 8041682272181381093 3835432206618893170 2653803171409877650 5589823419153460372 1007...
output:
result:
Subtask #5:
score: 0
Time Limit Exceeded
Test #27:
score: 0
Time Limit Exceeded
input:
200000 200000 1958469220619413759 14991498002015735322 6054491201406941902 18206143187746582567 15082377615826460430 2936248617457291604 10073577150351675920 16534472678586906457 2207599132486246393 10301540360769075442 1492580560381080472 551692353431379140 13238280352539145808 8462626987240986565 ...
output:
result:
Subtask #6:
score: 0
Time Limit Exceeded
Test #34:
score: 0
Time Limit Exceeded
input:
200000 200000 6794776813641982926 1561596256197101737 10910039723053043515 7892247858295192798 12233819960547881004 17695389034783066733 9173201689566865598 17626618141377486739 7358781671024283919 6787559733384974662 3884392438269280436 14872846228351316833 9037842441501571648 14299818404271084016 ...
output:
result:
Subtask #7:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%