QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#787411#6404. Shuttle ToursongszhCompile Error//C++145.1kb2024-11-27 11:35:212024-11-27 11:35:25

Judging History

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

  • [2024-11-27 11:35:25]
  • 评测
  • [2024-11-27 11:35:21]
  • 提交

answer

#include <bits/stdc++.h>
#define re register
#define ll long long
#define il inline

using namespace std;

const int N = 2e5 + 10,M = N * 2;
const int inf = 1e9 + 10;
int n,q;
char s[N]; bool st[N];
int idx,h[N],ne[M],e[M],w[M];
int lf = 1,tim,id[N],dfn[N],pid[N]; ll dep[N];

template<class T> il void read(T &x) {
    x = 0; T f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); }
    x *= f;
}
template<class T,class... Args> il void read(T &x,Args &...x_) { read(x); read(x_...); }
template<class T> il void print(T x) {
    if (x < 0) ptc('-'), x = -x; 
    if (x > 9) print(x / 10); ptc(x % 10 + '0');
}
template<class T,class T_> il void write(T x,T_ ch) { print(x); ptc(ch); }

inline void add(int a,int b,int c){
    ne[idx] = h[a];
    e[idx] = b; w[idx] = c;
    h[a] = idx++;
}

inline void dfs(int u,int fa){
    id[u] = lf; pid[dfn[u] = ++tim] = u;
    bool falg = true;
    for (re int i = h[u];~i;i = ne[i]){
        int v = e[i];
        if (v == fa) continue;
        dep[v] = dep[u] + w[i];
        dfs(v,u); falg = false;
    }
    if (falg) lf++;
}

namespace LCA{
    #define pot(x) (1 << (x))

    const int M = N * 5;
    int tim,lg[M],dfn[M],dp[M][24];

    inline void dfs(int u,int fa){
        dp[dfn[u] = ++tim][0] = u;
        for (re int i = h[u];~i;i = ne[i]){
            int v = e[i];
            if (v == fa) continue;
            dfs(v,u); dp[++tim][0] = u;
        }
    }

    inline void init(){
        dfs(1,0);
        for (re int i = 2;i <= tim;i++) lg[i] = lg[i >> 1] + 1;
        for (re int j = 1;j <= lg[tim];j++){
            for (re int i = 1;i + pot(j) - 1 <= tim;i++){
                int a = dp[i][j - 1],b = dp[i + pot(j - 1)][j - 1];
                if (dep[a] < dep[b]) dp[i][j] = a;
                else dp[i][j] = b;
            }
        }
    }

    inline int lca(int x,int y){
        int sx = dfn[x],sy = dfn[y];
        if (sx > sy) swap(sx,sy);
        int t = lg[sy - sx + 1];
        int a = dp[sx][t],b = dp[sy - pot(t) + 1][t];
        if (dep[a] < dep[b]) return a;
        else return b;
    }

    inline ll dist(int x,int y){
        return (dep[x] + dep[y] - 2ll * dep[lca(x,y)]);
    }

    #undef pot
}
using LCA::dist;

struct{
    #define ls(u) (u << 1)
    #define rs(u) (u << 1 | 1)
    #define val(u) (tr[u].val)

    int typ;

    struct value{
        int Max,Min;

        value friend operator +(const value &a,const value &b){
            return {max(a.Max,b.Max),min(a.Min,b.Min)};
        }
    };

    struct node{
        int l,r;
        value val;
    }tr[N << 2];

    inline void pushup(int u){
        val(u) = val(ls(u)) + val(rs(u));
    }

    inline void build(int u,int l,int r){
        tr[u] = {l,r,-inf,inf};
        if (l == r){
            if (st[l] && id[l] == typ) val(u) = {dfn[l],dfn[l]};
            return;
        }
        int mid = l + r >> 1;
        build(ls(u),l,mid); build(rs(u),mid + 1,r);
        pushup(u);
    }

    inline void update(int u,int x){
        if (tr[u].l == tr[u].r){
            if (st[x]) val(u) = {dfn[x],dfn[x]};
            else val(u) = {-inf,inf};
            return;
        }
        int mid = tr[u].l + tr[u].r >> 1;
        if (x <= mid) update(ls(u),x);
        else update(rs(u),x);
        pushup(u);
    }

    inline value query(int u,int l,int r){
        if (l <= tr[u].l && tr[u].r <= r) return val(u);
        int mid = tr[u].l + tr[u].r >> 1;
        if (l <= mid && r > mid) return query(ls(u),l,r) + query(rs(u),l,r);
        else if (l <= mid) return query(ls(u),l,r);
        else return query(rs(u),l,r);
    }

    #undef ls
    #undef rs
    #undef val
}T[51];

signed main(){
    read(n,q);
    for (re int i = 0;i <= n;i++) h[i] = -1;
    scanf("%s",s + 1);
    for (re int i = 1;i <= n;i++) st[i] = s[i] - '0';
    for (re int i = 1,a,b,c;i < n;i++){
        read(a,b,c);
        add(a,b,c),add(b,a,c);
    }
    dfs(1,0),LCA::init();
    for (re int i = 1;i < lf;i++) T[i].typ = i,T[i].build(1,1,n);
    // for (re int i = 1;i <= n;i++) cerr << dep[i] << " "; cerr << "\n";
    // for (re int i = 1;i <= n;i++){
    //     for (re int j = i + 1;j <= n;j++) cerr << i << " " << j << " " << lca(i,j) << " ???\n";
    // }
    while (q--){
        int op; read(op);
        if (op == 1){
            int x; read(x);
            st[x] ^= 1;
            T[id[x]].update(1,x);
        }
        else{
            int l,r; read(l,r);
            int be = 0,ed = 0; ll ans = 0;
            for (re int i = 1;i < lf;i++){
                auto val = T[i].query(1,l,r);
                if (val.Max != -inf){
                    if (!be) be = val.Min;
                    if (ed) ans += dist(pid[ed],pid[val.Min]);
                    ed = val.Max; ans += dist(pid[val.Max],pid[val.Min]);
                }
            }
            if (!be) puts("-1");
            else write(ans + dist(pid[be],pid[ed]),'\n');
        }
    }
    return 0;
}

详细

answer.code: In function ‘void print(T)’:
answer.code:23:16: error: there are no arguments to ‘ptc’ that depend on a template parameter, so a declaration of ‘ptc’ must be available [-fpermissive]
   23 |     if (x < 0) ptc('-'), x = -x;
      |                ^~~
answer.code:23:16: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
answer.code: In instantiation of ‘void write(T, T_) [with T = long long int; T_ = char]’:
answer.code:184:23:   required from here
answer.code:26:68: error: ‘ptc’ was not declared in this scope; did you mean ‘putc’?
   26 | template<class T,class T_> il void write(T x,T_ ch) { print(x); ptc(ch); }
      |                                                                 ~~~^~~~
      |                                                                 putc
answer.code: In instantiation of ‘void print(T) [with T = long long int]’:
answer.code:26:60:   required from ‘void write(T, T_) [with T = long long int; T_ = char]’
answer.code:184:23:   required from here
answer.code:23:19: error: ‘ptc’ was not declared in this scope; did you mean ‘putc’?
   23 |     if (x < 0) ptc('-'), x = -x;
      |                ~~~^~~~~
      |                putc
answer.code:24:34: error: ‘ptc’ was not declared in this scope; did you mean ‘putc’?
   24 |     if (x > 9) print(x / 10); ptc(x % 10 + '0');
      |                               ~~~^~~~~~~~~~~~~~
      |                               putc
answer.code: In function ‘int main()’:
answer.code:153:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  153 |     scanf("%s",s + 1);
      |     ~~~~~^~~~~~~~~~~~