求助!这个指针版的fhq为何过不了第二个样例呢?

CF702F T-Shirts

cjwdyzxfblzs @ 2023-05-05 10:41:35

#include <bits/stdc++.h>

using namespace std;

#define int long long

int n, m;
const int N = 1e6 + 7;
mt19937 rnd(233);

inline int read()
{
    int o = 1, p = 0;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            o = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        p = p * 10 + c - '0',
        c = getchar();
    return o * p;
}
struct node
{
    node *ls;
    node *rs;
    int val;
    int rand;
    int size;
    int sum;
    int tag;
    int sub;
    int id;
    explicit node(int x) : size(1)
    {
        ls = rs = nullptr;
        rand = rnd();
        val = x;
        sum = sub = tag = 0;
        id = 0;
    }
} *root;
void push_up(node *u)
{
    u->size = (u->ls ? u->ls->size : 0) + (u->rs ? u->rs->size : 0) + 1;
}
void push_down(node *u)
{
    if (u->tag)
    {
        if (u->ls)
            u->ls->tag += u->tag, u->ls->sum += u->tag;
        if (u->rs)
            u->rs->tag += u->tag, u->rs->sum += u->tag;
        u->tag = 0;
    }
    if (u->sub)
    {
        if (u->ls)
            u->ls->sub += u->sub, u->ls->val += u->sub;
        if (u->rs)
            u->rs->sub += u->sub, u->rs->val += u->sub;
        u->sub = 0;
    }
}
node *merge(node *x, node *y)
{
    if (!x || !y)
        return x ? x : y;
    if (x->rand < y->rand)
    {
        push_down(x);
        x->rs = merge(x->rs, y);
        push_up(x);
        return x;
    }
    else
    {
        push_down(y);
        y->ls = merge(x, y->ls);
        push_up(y);
        return y;
    }
}
void split(node *u, int val, node *&L, node *&R)
{
    if (!u)
        return L = R = nullptr, void();
    push_down(u);
    if (u->val <= val)
    {
        L = u;
        split(u->rs, val, u->rs, R);
    }
    else
    {
        R = u;
        split(u->ls, val, L, u->ls);
    }
    push_up(u);
}
node *L, *R, *Z;
void insert(int val, int i)
{
    split(root, val, L, R);
    node *xx = new node(val);
    xx->id = i;
    root = merge(merge(L, xx), R);
}
void ins(node *&rt, node *p)
{
    if (!p) p = new node(0);
    split(rt, p->val, L, R);
    root = merge(merge(L, p), R);
}
void dfs(node *u, node *&y)
{
    if (!u)
        return;
    push_down(u);
    if (u->ls)
        dfs(u->ls, y);
    if (u->rs)
        dfs(u->rs, y);
    u->ls = u->rs = nullptr;
    ins(y, u);
}
node *merge_one(node *x, node *y)
{
    if (!x) x = new node(0);
    if (!y) y = new node(0);
    if (x->size > y->size)
        swap(x, y);
    dfs(x, y);
    return y;
}
void dfs(node *u)
{
    if (!u)
        return;
    push_down(u);
    if (u->ls)
        dfs(u->ls);
    if (u->rs)
        dfs(u->rs);
}
int ans[N];
void dfs1(node *u)
{
    if (!u)
        return;
    ans[u->id] = u->sum;
    if (u->ls)
        dfs1(u->ls);
    if (u->rs)
        dfs1(u->rs);
}
struct nodee
{
    int c, q;
    bool operator<(const nodee &a) const
    {
        if (q == a.q)
            return c > a.c;
        return q < a.q;
    }
} p[N];

signed main()
{
    n = read();
    for (int i = 1; i <= n; i++)
        p[i].c = read(),
        p[i].q = read();
    sort(p + 1, p + n + 1);
    reverse(p + 1, p + n + 1);

    m = read();
    for (int i = 1; i <= m; i++)
        insert(read(), i);

    node *x, *y, *z;
    for (int i = 1; i <= n; i++)
    {
        int cost = p[i].c;
        split(root, cost - 1, L, R);
        R->sum++;
        R->tag++;
        R->sub += -cost;
        R->val -= cost;
        split(R, cost - 1, R, Z);
        L = merge_one(L, R);
        root = merge(L, Z);
    }

    dfs(root);
    dfs1(root);

    for (int i = 1; i <= m; i++)
        printf("%d ", ans[i]);
    puts("");

    return 0;
}

by cjwdyzxfblzs @ 2023-05-05 10:42:01

已经调了3h 了,真的不理解了


by cjwdyzxfblzs @ 2023-05-05 10:47:11

原因是我把reverse没注释,抱歉,此帖终


|