ZR3773 开场

自评难度 \(2\)。

ZR3773 开场

设 \(f_i\) 为第 \(i\) 首歌结束的时刻,只要 \(f_i \le d_i\) 则 \(i\) 有贡献。

扫描 \(i=1\to n\) 维护 \(f_i-d_i\) 的序列,指针移动需要全局减,单点插入和删除。不带脑子的话,拍平衡树维护即可,复杂度一只 log。

const int N = 2e5 + 5;
int n; i64 x[N], d[N], a[N];

struct node {
    u32 pri; int siz;
    i64 val, tag;
    node *l, *r;
    node(i64 v = 0) : pri(rand32()), siz(1), val(v), tag(0), l(0), r(0) {}
    node* pushup() {
        siz = 1 + (l ? l->siz : 0) + (r ? r->siz : 0);
        return this;
    }
    void apply(i64 c) {val += c, tag += c;}
    void pushdown() {
        if (l) l->apply(tag);
        if (r) r->apply(tag);
        tag = 0;
    }
};
void split(node* u, i64 k, node*& x, node*& y) {
    if (!u) return x = y = nullptr, void();
    u->pushdown();
    if (u->val <= k) x = u, split(u->r, k, x->r, y), x->pushup();
    else y = u, split(u->l, k, x, y->l), y->pushup();
}
node* merge(node* u, node* v) {
    if (!u || !v) return u ? u : v;
    u->pushdown(), v->pushdown();
    return (u->pri < v->pri) ? 
        (u->r = merge(u->r, v), u->pushup()) :
        (v->l = merge(u, v->l), v->pushup()) ;
}


void _main() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> x[i];
    for (int i = 1; i <= n; i++) cin >> d[i];
    for (int i = 1; i <= n; i++) a[i] = a[i-1] + x[i];
    i64 sum = a[n]; node *T = nullptr;
    auto ins = [&](i64 c) -> void {
        node *x, *y; split(T, c, x, y);
        T = merge(merge(x, new node(c)), y);
    };
    auto del = [&](i64 c) -> void {
        node *x, *y, *z; split(T, c, y, z), split(y, c-1, x, y);
        T = merge(merge(x, y->l), merge(y->r, z));
    };
    for (int i = 1; i <= n; i++) a[i] -= d[i], ins(a[i]);
    int res = 0, num = 1;
    for (int i = 1; i <= n; i++) {
        node *u, *v; split(T, 0, u, v);
        int cnt = u ? u->siz : 0;
        if (cnt > res) res = cnt, num = i;
        T = merge(u, v), T->apply(-x[i]);
        del(-d[i]), ins(sum - d[i]);
    }
    cout << res << ' ' << num;
} 
posted @ 2026-11-18 15:22  stripe_python  阅读(6)  评论(0)    收藏  举报