QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#187667#6404. Shuttle TourUESTC_Guest_WiFiWA 0ms16656kbC++203.8kb2023-09-24 20:20:362023-09-24 20:20:36

Judging History

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

  • [2023-09-24 20:20:36]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:16656kb
  • [2023-09-24 20:20:36]
  • 提交

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: 0
Wrong Answer
time: 0ms
memory: 16656kb

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
0

result:

wrong answer Output contains longer sequence [length = 6], but answer contains 5 elements