85分tle求助
查看原帖
85分tle求助
264840
johntime楼主2024/8/22 21:31

rt,尝试过文件快读但是全wa了(不太会使),如果直接用快读70分tle,求指点

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll N = 1e5 + 10;
ll n, m, r;

struct t {
    ll fa[N], dep[N], siz[N], son[N], top[N], dfs[N];

    void dfs1(const vector<vector<ll> > &g, const ll u, const ll f) {
        fa[u] = f;
        dep[u] = dep[f] + 1;
        siz[u] = 1;
        for (auto v: g[u]) {
            if (v == f)
                continue;
            dfs1(g, v, u);
            siz[u] += siz[v];
            if (siz[v] > siz[son[u]])
                son[u] = v;
        }
    }

    void dfs2(const vector<vector<ll> > &g, const ll u, const ll t) {
        top[u] = t;
        dfs[u] = ++dfs[n];
        if (son[u])
            dfs2(g, son[u], t);
        for (auto v: g[u]) {
            if (v == fa[u] || v == son[u])
                continue;
            dfs2(g, v, v);
        }
    }
} tree;

using LL = long long;

struct node {
    int l, r;
    LL val, addtg, settg = -1;
};

struct segmentTree {
#define ls(x) (x << 1)
#define rs(x) (x << 1 | 1)
#define p tr[u]
#define pl tr[ls(u)]
#define pr tr[rs(u)]

    std::vector<node> tr;

    segmentTree(int n) : tr(n << 2) {
    }

    void pushup(int u) {
        p.val = pl.val + pr.val;
    }

    void pushdown(int u) {
        if (p.addtg) {
            pl.val += p.addtg * (pl.r - pl.l + 1);
            pr.val += p.addtg * (pr.r - pr.l + 1);
            pl.addtg += p.addtg;
            pr.addtg += p.addtg;
            p.addtg = 0;
        }
        if (p.settg != -1) {
            pl.val = p.settg * (pl.r - pl.l + 1);
            pr.val = p.settg * (pr.r - pr.l + 1);
            pl.settg = p.settg;
            pr.settg = p.settg;
            p.settg = -1;
        }
    }

    void build(int u, int l, int r, std::vector<LL> &a) {
        p.l = l, p.r = r;
        if (l == r) {
            p.val = a[l];
            return;
        }
        int mid = (l + r) >> 1;
        build(ls(u), l, mid, a);
        build(rs(u), mid + 1, r, a);
        pushup(u);
    }

    void modify(int u, int l, int r, LL d) {
        if (p.l >= l && p.r <= r) {
            p.val += d * (p.r - p.l + 1);
            p.addtg += d;
            return;
        }
        pushdown(u);
        int mid = (p.l + p.r) >> 1;
        if (l <= mid)
            modify(ls(u), l, r, d);
        if (r > mid)
            modify(rs(u), l, r, d);
        pushup(u);
    }

    void set(int u, int l, int r, LL d) {
        if (p.l >= l && p.r <= r) {
            p.val = d * (p.r - p.l + 1);
            p.settg = d;
            return;
        }
        int mid = (p.l + p.r) >> 1;
        pushdown(u);
        if (l <= mid)
            set(ls(u), l, r, d);
        if (r > mid)
            set(rs(u), l, r, d);
        pushup(u);
    }

    LL query(int u, int l, int r) {
        if (p.l >= l && p.r <= r)
            return p.val;
        pushdown(u);
        int mid = (p.l + p.r) >> 1;
        LL res = 0;
        if (l <= mid)
            res += query(ls(u), l, r);
        if (r > mid)
            res += query(rs(u), l, r);
        return res;
    }

#undef ls
#undef rs
#undef p
#undef pl
#undef pr
};

// // #define DEBUG 1  // 调试开关
// struct IO {
// #define MAXSIZE (1 << 20)
// #define isdigit(x) (x >= '0' && x <= '9')
//     char buf[MAXSIZE], *p1, *p2;
//     char pbuf[MAXSIZE], *pp;
// #if DEBUG
// #else
//     IO() : p1(buf), p2(buf), pp(pbuf) {}
//
//     ~IO() { fwrite(pbuf, 1, pp - pbuf, stdout); }
// #endif
//     char gc() {
// #if DEBUG  // 调试,可显示字符
//         return getchar();
// #endif
//         if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
//         return p1 == p2 ? ' ' : *p1++;
//     }
//
//     bool blank(char ch) {
//         return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
//     }
//
//     template <class T>
//     void read(T &x) {
//         double tmp = 1;
//         bool sign = 0;
//         x = 0;
//         char ch = gc();
//         for (; !isdigit(ch); ch = gc())
//             if (ch == '-') sign = 1;
//         for (; isdigit(ch); ch = gc()) x = x * 10 + (ch - '0');
//         if (ch == '.')
//             for (ch = gc(); isdigit(ch); ch = gc())
//                 tmp /= 10.0, x += tmp * (ch - '0');
//         if (sign) x = -x;
//     }
//
//     void read(char *s) {
//         char ch = gc();
//         for (; blank(ch); ch = gc());
//         for (; !blank(ch); ch = gc()) *s++ = ch;
//         *s = 0;
//     }
//
//     void read(char &c) { for (c = gc(); blank(c); c = gc()); }
//
//     void push(const char &c) {
// #if DEBUG  // 调试,可显示字符
//         putchar(c);
// #else
//         if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
//         *pp++ = c;
// #endif
//     }
//
//     template <class T>
//     void write(T x) {
//         if (x < 0) x = -x, push('-');  // 负数输出
//         static T sta[35];
//         T top = 0;
//         do {
//             sta[top++] = x % 10, x /= 10;
//         } while (x);
//         while (top) push(sta[--top] + '0');
//     }
//
//     template <class T>
//     void write(T x, char lastChar) {
//         write(x), push(lastChar);
//     }
// } io;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    // freopen("P2146_1.in", "r", stdin);
    // freopen("P2146.out", "w", stdout);
    cin >> n;
    // IO io;
    // io.read(n);
    vector<vector<ll> > g(n + 1);
    for (int i = 1; i < n; i++) {
        ll req;
        cin >> req;
        // io.read(req);
        g[req].push_back(i);
    }
    tree.dfs1(g, 0, 0);
    tree.dfs2(g, 0, 0);
    vector<ll> w(n + 1, 0);
    segmentTree root(n);
    root.build(1, 1, n, w);
    cin >> m;
    // io.read(m);
    while (m--) {
        string op;
        cin >> op;
        // io.read(op);
        if (op[0] == 'i') {
            ll x;
            cin >> x;
            // io.read(x);
            const ll t1 = root.query(1, 1, n);
            while (tree.top[x] != 0) {
                root.set(1, tree.dfs[tree.top[x]], tree.dfs[x], 1);
                x = tree.fa[tree.top[x]];
            }
            root.set(1, 1, tree.dfs[x], 1);
            const ll t2 = root.query(1, 1, n);
            cout << std::abs(t2 - t1) << '\n';
            // io.write(std::abs(t2 - t1), '\n');
        } else if (op[0] == 'u') {
            ll x;
            cin >> x;
            // io.read(x);
            const ll t1 = root.query(1, 1, n);
            root.set(1, tree.dfs[x], tree.dfs[x] + tree.siz[x] - 1, 0);
            const ll t2 = root.query(1, 1, n);
            cout << std::abs(t2 - t1) << '\n';
            // io.write(std::abs(t2 - t1), '\n');
        }
    }
}

//
// Created by johntimeson on 24-8-22.
//
2024/8/22 21:31
加载中...