QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#187671#6404. Shuttle TourUESTC_Guest_WiFiWA 88ms16636kbC++203.8kb2023-09-24 20:22:012023-09-24 20:22:01

Judging History

你现在查看的是最新测评结果

  • [2023-09-24 20:22:01]
  • 评测
  • 测评结果:WA
  • 用时:88ms
  • 内存:16636kb
  • [2023-09-24 20:22:01]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=2e5;
int n,q;
char s[N+5];
struct E{
    int v,w;
    E(int v=0,int w=0):v(v),w(w){}
};
vector<E> e[N+5];
int fa[N+5],dep[N+5];
ll dw[N+5];
int sz[N+5],son[N+5],top[N+5];
int tot,mnd[55],bl[N+5];
void dfs1(int u,int tf){
    fa[u]=tf; sz[u]=1;
    dep[u]=dep[tf]+1;
    for(auto it:e[u]){
        int v=it.v;
        if(v==tf) continue;
        dw[v]=dw[u]+it.w;
        dfs1(v,u);
        if(sz[v]>sz[son[u]]) son[u]=v;
        sz[u]+=sz[v];
    }
}
void dfs2(int u,int tf){
    top[u]=tf;
    if(!son[u]) return;
    dfs2(son[u],tf);
    for(auto it:e[u])
        if(it.v!=son[u]&&it.v!=fa[u])
            dfs2(it.v,it.v);
}
int LCA(int u,int v){
    if(!u||!v) return u+v;
    // printf("%d %d\n",u,v);
    while(top[u]!=top[v]){
        if(dep[u]<dep[v]) swap(u,v);
        u=fa[top[u]];
    }
    return dep[u]<dep[v]?u:v;
}
namespace Segtr{
    #define rc id<<1|1
    #define lc id<<1
    int mx[N*4+5][55],lca[N*4+5];
    int cmp(int x,int y){ return !x||!y?x+y:dep[x]<dep[y]?y:x; }
    void push_up(int id){
        // printf("%d %d ",lca[lc],lca[rc]);
        lca[id]=LCA(lca[lc],lca[rc]);
        // printf("%d\n",lca[id]);
        for(int i=1;i<=tot;i++)
            mx[id][i]=cmp(mx[lc][i],mx[rc][i]);
    }
    void build(int l,int r,int id){
        if(l==r){
            // printf("%d %d\n",l,s[l]);
            if(s[l]) lca[id]=mx[id][bl[l]]=l;
            return;
        }
        int mid=l+r>>1;
        build(l,mid,lc); build(mid+1,r,rc);
        push_up(id);
    }
    void upd(int l,int r,int p,int id){
        if(l==r){
            s[l]^=1;
            if(s[l]) lca[id]=mx[id][bl[l]]=l;
            else lca[id]=mx[id][bl[l]]=0;
            return;
        }
        int mid=l+r>>1;
        if(p<=mid) upd(l,mid,p,lc);
        else upd(mid+1,r,p,rc);
        push_up(id);
    }
    int res[55];
    void update(int val[],int vlca){
        // printf("%d %d ",res[0],vlca);
        res[0]=LCA(res[0],vlca);
        // printf("%d\n",res[0]);
        for(int i=1;i<=tot;i++)
            res[i]=cmp(res[i],val[i]);
    }
    void query(int l,int r,int st,int en,int id){
        if(st<=l&&en>=r){
            // printf("%d %d\n",l,r);
            update(mx[id],lca[id]); return;
        }
        int mid=l+r>>1;
        if(st<=mid) query(l,mid,st,en,lc);
        if(en>mid) query(mid+1,r,st,en,rc);
    }
}
using namespace Segtr;
bool used[N+5];
void prep(){
    dfs1(1,0); dfs2(1,1);
    for(int i=2;i<=n;i++)
        if(e[i].size()==1){
            tot++;
            for(int j=i;j&&!used[j];j=fa[j]){
                bl[j]=tot; mnd[tot]=j;
                used[j]=1;
            }
            mnd[tot]=fa[mnd[tot]];
        }
    build(1,n,1);
}
int main(){
    scanf("%d %d",&n,&q);
    scanf("%s",s+1);
    for(int i=1;i<=n;i++) s[i]-='0';
    for(int i=1,u,v,w;i<n;i++){
        scanf("%d %d %d",&u,&v,&w);
        e[u].push_back(E(v,w));
        e[v].push_back(E(u,w));
    }
    prep();
    while(q--){
        int tp,l,r;
        scanf("%d %d",&tp,&l);
        if(tp==1) upd(1,n,l,1);
        else{
            scanf("%d",&r);
            memset(res,0,sizeof res);
            query(1,n,l,r,1);
            ll ans=0; int rt=res[0];
            // printf("%d %d %d\n",l,r,rt);
            if(!rt) puts("-1");
            else{
                for(int i=1;i<=tot;i++){
                    if(!res[i]) continue;
                    // printf("%d %d\n",res[i],rt);
                    if(dep[rt]>=dep[mnd[i]])
                        ans+=dw[res[i]]-dw[rt];
                    else
                        ans+=dw[res[i]]-dw[mnd[i]];
                }
                printf("%lld\n",ans<<1);
            }
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 16636kb

input:

5 6
10110
1 2 1
1 3 10
2 4 100
3 5 1000
2 1 5
1 3
2 1 5
2 2 4
2 5 5
2 1 1

output:

222
202
0
-1
0

result:

ok 5 number(s): "222 202 0 -1 0"

Test #2:

score: -100
Wrong Answer
time: 88ms
memory: 14704kb

input:

50 200000
00100100100001101010110100000111100011101110011010
14 47 940241413
11 43 331483591
37 38 496247070
47 46 832459694
7 15 16479291
1 30 402388666
30 8 513064064
46 50 739311480
5 4 761894947
32 41 631669659
17 24 715786564
35 20 763151642
32 33 492466005
39 11 186023146
9 7 805081251
3 42 25...

output:

10387160262
-1
17897570196
23766236352
15460492156
18360273242
10175601204
16452142896
18531713524
18401090236
29727468956
6379800596
6222696122
20887061930
11270256470
2856655228
13102105330
0
-1
5601877922
24313120040
15420813374
20130966978
14529089056
8533875940
7695830712
16779481598
0
23861130...

result:

wrong answer 1st numbers differ - expected: '17149847378', found: '10387160262'