fhq 求助,TLE on test 7,9

CF702F T-Shirts

__ycx2010__ @ 2023-09-27 21:43:57

为什么我的代码常数这么大呢?/yiw

#include <bits/stdc++.h>
#define re register
using namespace std;

typedef long long LL;
const int N = 2e5 + 10;
int n, m, root;

inline int read() {
    int f = 1, w = 0;
    char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {w = (w << 3) + (w << 1) + ch - '0'; ch = getchar();}
    return f * w;
}

struct Node {
    int s[2], key, v;
    int val, add, cost;
} tr[N];

inline void pushdown(int u) {
    if (tr[u].add) {
        tr[tr[u].s[0]].val += tr[u].add, tr[tr[u].s[1]].val += tr[u].add;
        tr[tr[u].s[0]].add += tr[u].add, tr[tr[u].s[1]].add += tr[u].add;
        tr[u].add = 0;
    } if (tr[u].cost) {
        tr[tr[u].s[0]].key -= tr[u].cost, tr[tr[u].s[1]].key -= tr[u].cost;
        tr[tr[u].s[0]].cost += tr[u].cost, tr[tr[u].s[1]].cost += tr[u].cost;
        tr[u].cost = 0;
    }
}

void split(int u, int k, int &l, int &r) {
    if (!u) {
        l = r = 0;
        return;
    } if (tr[u].key <= k) {
        l = u;
        pushdown(u);
        split(tr[u].s[1], k, tr[u].s[1], r);
    } else {
        r = u;
        pushdown(u);
        split(tr[u].s[0], k, l, tr[u].s[0]);
    }
}

int merge(int x, int y) {
    if (!x || !y) return x | y;
    if (tr[x].val < tr[y].val) {
        pushdown(x);
        tr[x].s[1] = merge(tr[x].s[1], y);
        return x;
    } else {
        pushdown(y);
        tr[y].s[0] = merge(x, tr[y].s[0]);
        return y;
    }
}

void brute_force(int x, int &y) {
    if (!x) return;
    pushdown(x);
    brute_force(tr[x].s[0], y);
    brute_force(tr[x].s[1], y);
    int a, b;
    tr[x].s[0] = tr[x].s[1] = 0;
    split(y, tr[x].key, a, b);
    y = merge(merge(a, x), b);
}

void update(int u) {
    if (!u) return;
    pushdown(u);
    update(tr[u].s[0]);
    update(tr[u].s[1]);
    return;
}

struct GREAT {
    int w, q;
} c[N];

inline bool cmp(GREAT a, GREAT b) {
    if (a.q != b.q) return a.q > b.q;
    return a.w < b.w;
}

int main() {
    n = read();
    for (re int i = 1; i <= n; i ++ ) c[i].w = read(), c[i].q = read();
    sort(c + 1, c + n + 1, cmp);
    m = read();
    for (re int i = 1; i <= m; i ++ ) {
        int v = read();
        tr[i].key = v, tr[i].v = rand();
        int a, b;
        split(root, v, a, b);
        root = merge(merge(a, i), b);
    }
    for (re int i = 1; i <= n; i ++ ) {
        int ww = c[i].w, a, b, c;
        split(root, ww - 1, a, b);
        split(b, 2 * ww, b, c);
        tr[b].val ++ , tr[c].val ++ ;
        tr[b].add ++ , tr[c].add ++ ;
        tr[b].key -= ww, tr[c].key -= ww;
        tr[b].cost += ww, tr[c].cost += ww;
        brute_force(b, a);
        root = merge(a, c);
    }
    update(root);
    for (re int i = 1; i <= m; i ++ ) printf("%d ", tr[i].val);
    return 0;
}

by __ycx2010__ @ 2023-09-28 08:46:35


|