QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#883857 | #10016. Divine Tree | rotcar07 | WA | 248ms | 25472kb | C++23 | 2.4kb | 2025-02-05 19:26:56 | 2025-02-05 19:26:58 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
constexpr int N=1e5+5;
int n,m,q;
string s;vector<int> e[N];
int _u[N],_v[N],_w[N],fa[N];
int S[N],sz[N],dfn[N],toki;
void dfs(int p,int f){
fa[p]=f;sz[p]=1;dfn[p]=++toki;
for(int x:e[p])if(x!=f)dfs(x,p),sz[p]+=sz[x],S[p]+=S[x];
}
typedef long long ll;
constexpr ll inf=1e6;
struct seg{
ll t[N<<2],g[N<<2];
#define ls p<<1
#define rs p<<1|1
inline void pu(int p){t[p]=min(t[ls],t[rs]);}
inline void app(int p,ll k){t[p]+=k,g[p]+=k;}
inline void pd(int p){if(g[p])app(ls,g[p]),app(rs,g[p]),g[p]=0;}
void build(int p,int l,int r){
t[p]=inf;g[p]=0;
if(l==r) return;
int mid=l+r>>1;
build(ls,l,mid),build(rs,mid+1,r);
}
bool sb;
void modify(int p,int l,int r,int ql,int qr,ll x){
if(ql<=l&&r<=qr) return app(p,x);
int mid=l+r>>1;pd(p);
if(ql<=mid) modify(ls,l,mid,ql,qr,x);
if(qr>mid) modify(rs,mid+1,r,ql,qr,x);
pu(p);
}
}A,B;
int ff[N][2];
void dfs2(int p,int f,int xx){
ff[p][xx]=f;
for(int x:e[p])if(x!=fa[p]) dfs2(x,f,xx);
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>s;
for(int i=1;i<=n;i++) m+=S[i]=s[i-1]=='G';
for(int i=1;i<n;i++) cin>>_u[i]>>_v[i]>>_w[i],e[_u[i]].emplace_back(_v[i]),e[_v[i]].emplace_back(_u[i]);
cin>>q;
if(m==n||m==0){while(q--)puts("0");return 0;}
dfs(1,0);
for(int i=1;i<n;i++) if(fa[_v[i]]==_u[i]) _u[i]=_v[i];
auto upd=[&](int x,ll w){
int L=dfn[x],R=dfn[x]+sz[x]-1;
A.modify(1,1,n,L,R,(m-S[x])*w);B.modify(1,1,n,L,R,(n-sz[x]-m+S[x])*w);
if(1<L) A.modify(1,1,n,1,L-1,S[x]*w),B.modify(1,1,n,1,L-1,(sz[x]-S[x])*w);
if(R<n) A.modify(1,1,n,R+1,n,S[x]*w),B.modify(1,1,n,R+1,n,(sz[x]-S[x])*w);
if(ff[x][0]) A.modify(1,1,n,dfn[ff[x][0]],dfn[ff[x][0]],(sz[x]-S[x]-S[x])*w);
if(ff[x][1]) B.modify(1,1,n,dfn[ff[x][1]],dfn[ff[x][1]],-(sz[x]-S[x]-S[x])*w);
};B.sb=1;
A.build(1,1,n),B.build(1,1,n);
for(int i=1;i<=n;i++){
if(sz[i]==m) A.modify(1,1,n,dfn[i],dfn[i],-inf),dfs2(i,i,0),ff[i][0]=0;
if(sz[i]==n-m) B.modify(1,1,n,dfn[i],dfn[i],-inf),dfs2(i,i,1),ff[i][1]=0;
}
for(int i=1;i<n;i++) upd(_u[i],_w[i]);
while(q--){
int x,y;cin>>x>>y;
upd(_u[x],y);
cout<<min(A.t[1],B.t[1])<<'\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 15968kb
input:
5 BGGGG 1 3 0 2 1 0 5 2 0 2 4 0 5 2 1 1 3 4 4 3 10 1 2
output:
0 1 1 3 5
result:
ok 5 number(s): "0 1 1 3 5"
Test #2:
score: 0
Accepted
time: 0ms
memory: 15972kb
input:
5 GBBGB 3 2 0 2 1 0 1 4 0 1 5 1000 4 4 1 3 1 2 1 1 1
output:
0 1 3 4
result:
ok 4 number(s): "0 1 3 4"
Test #3:
score: 0
Accepted
time: 0ms
memory: 15904kb
input:
7 GGBBBBG 1 5 101 2 5 101 3 5 100 3 6 100 4 6 100 7 6 100 6 6 1 6 1 6 1 5 3 3 3 6 12345
output:
301 302 303 303 306 711
result:
ok 6 numbers
Test #4:
score: 0
Accepted
time: 0ms
memory: 7780kb
input:
5 BBBBB 1 2 0 1 3 0 2 4 0 2 5 0 5 1 1 2 3 3 3 4 10 2 2
output:
0 0 0 0 0
result:
ok 5 number(s): "0 0 0 0 0"
Test #5:
score: 0
Accepted
time: 0ms
memory: 7776kb
input:
5 GGGGG 1 2 0 1 3 0 2 4 0 2 5 0 5 1 1 2 3 3 3 4 10 2 2
output:
0 0 0 0 0
result:
ok 5 number(s): "0 0 0 0 0"
Test #6:
score: 0
Accepted
time: 0ms
memory: 12024kb
input:
3 BGG 1 2 0 1 3 0 5 1 1 2 1 1 1 2 1 1 1
output:
0 1 1 2 2
result:
ok 5 number(s): "0 1 1 2 2"
Test #7:
score: 0
Accepted
time: 248ms
memory: 25472kb
input:
99999 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...
output:
2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701915 2701...
result:
ok 100000 numbers
Test #8:
score: 0
Accepted
time: 227ms
memory: 24092kb
input:
99999 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...
output:
2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497774 2497...
result:
ok 100000 numbers
Test #9:
score: -100
Wrong Answer
time: 218ms
memory: 25152kb
input:
99999 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...
output:
4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959826 4959...
result:
wrong answer 20147th numbers differ - expected: '5675517', found: '5666682'