QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#187777#6404. Shuttle TourUESTC_Guest_WiFiCompile Error//C++204.1kb2023-09-24 22:11:502023-09-24 22:11:50

Judging History

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

  • [2023-09-24 22:11:50]
  • 评测
  • [2023-09-24 22:11:50]
  • 提交

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 dfn[N+5],dfns;
int tot,bl[N+5];
void dfs1(int u,int tf){
    dfn[u]=++dfns;
    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]){
        int v=it.v;
        if(v!=son[u]&&v!=fa[u])
            dfs2(v,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[top[u]]<dep[top[v]]) swap(u,v);
        u=fa[top[u]];
    }
    return dep[u]<dep[v]?u:v;
}
namespace Segtr{
    #define lc id<<1
    #define rc id<<1|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){
            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){
        res[0]=LCA(res[0],vlca);
        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; used[j]=1;
            }
        }
    build(1,n,1);
}
bool cmpv(int x,int y){ return dfn[x]<dfn[y]?x:y; }
ll calc(vector<int> ver){
    if(!ver.size()) return -1;
    sort(ver.begin(),ver.end(),cmpv);
    static int st[105];
    int s=0; ll ans=0;
    st[++s]=ver[0];
    for(int i=1;i<ver.size();i++){
        int u=ver[i],lca=LCA(u,st[s]);
        // printf("%d\n",lca);
        // printf("%d ",u);
        while(s>1&&dep[st[s-1]]>dep[lca]){
            ans+=dw[st[s]]-dw[st[s-1]];
            s--;
        }
        if(dep[st[s]]>dep[lca]){
            ans+=dw[st[s]]-dw[lca];
            s--;
        }
        if(s<0) fprintf("------");
        assert(s>=0);
        if(st[s]!=lca) st[++s]=lca;
        st[++s]=u;
    }
    // puts("");
    while(s>1){
        ans+=dw[st[s]]-dw[st[s-1]];
        s--;
    }
    return ans<<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);
            vector<int> ver;
            for(int i=0;i<=tot;i++)
                if(res[i]) ver.push_back(res[i]);
            printf("%lld\n",calc(ver));
        }
    }
    return 0;
}

Details

answer.code: In function ‘ll calc(std::vector<int>)’:
answer.code:130:25: error: cannot convert ‘const char*’ to ‘FILE*’
  130 |         if(s<0) fprintf("------");
      |                         ^~~~~~~~
      |                         |
      |                         const char*
In file included from /usr/include/stdio.h:867,
                 from /usr/include/c++/11/cstdio:42,
                 from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:46,
                 from answer.code:1:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:98:27: note:   initializing argument 1 of ‘int fprintf(FILE*, const char*, ...)’
   98 | fprintf (FILE *__restrict __stream, const char *__restrict __fmt, ...)
      |          ~~~~~~~~~~~~~~~~~^~~~~~~~
answer.code: In function ‘int main()’:
answer.code:143:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  143 |     scanf("%d %d",&n,&q);
      |     ~~~~~^~~~~~~~~~~~~~~
answer.code:144:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  144 |     scanf("%s",s+1);
      |     ~~~~~^~~~~~~~~~
answer.code:147:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  147 |         scanf("%d %d %d",&u,&v,&w);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
answer.code:154:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  154 |         scanf("%d %d",&tp,&l);
      |         ~~~~~^~~~~~~~~~~~~~~~
answer.code:157:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  157 |             scanf("%d",&r);
      |             ~~~~~^~~~~~~~~