help

P3586 [POI2015] LOG

tidongCrazy @ 2020-11-01 20:30:01

A了1个点 WA了8个点

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define int long long

const int N = 3e6 + 5;

inline int read() {

    int w = 1, s = 0; char c;
    while(!isdigit(c = getchar())) if(c == '-') w = -1;
    while(isdigit(c)) s = (s << 1) + (s << 3) + (c & 15), c = getchar();
    return s * w;
}

struct Node { int l, r, v, rand, siz, ans; } tr[N];

inline void push_up(int x) {

    tr[x].siz = tr[tr[x].l].siz + tr[tr[x].r].siz + 1;
    tr[x].ans = tr[tr[x].l].ans + tr[tr[x].r].ans + tr[x].v;
}

int rt, siz;

inline int insert(int v) {

    tr[++ siz].siz = 1;
    tr[siz].v = tr[siz].ans = v;
    tr[siz].rand = rand();
    return siz;
}

inline void split(int now, int v, int &x, int &y) {

    if(!now) x = y = 0;
    else {
        if(tr[now].v <= v) x = now, split(tr[now].r, v, tr[now].r, y);
        else y = now, split(tr[now].l, v, x, tr[now].l);
        push_up(now);
    }   
}

inline int merge(int x, int y) {

    if(!x || !y) return x | y;
    if(tr[x].rand < tr[y].rand) {
        tr[x].r = merge(tr[x].r, y);
        push_up(x); return x;
    } else {
        tr[y].l = merge(x, tr[y].l);
        push_up(y); return y;
    }
}

#include <ctime>
int arr[N];

signed main() {

    srand(time(NULL));
    int n = read(), q = read(), x, y;
    for(int i = 1; i <= n; i ++ ) rt = merge(rt, insert(0));
    while(q -- ) {
        char opt; int a, b;
        cin >> opt;
        a = read(); b = read();
        if(opt == 'U') {
            split(rt, arr[a], rt, x);
            split(rt, arr[a] - 1, rt, y);
            rt = merge(rt, merge(x, insert(b)));
            arr[a] = b;
        } if(opt == 'Z') {
            split(rt, b - 1, rt, x);
            int sum = tr[rt].ans;
            if(sum >= (a - tr[x].siz) * b) printf("TAK\n");
            else printf("NIE\n");
            rt = merge(rt, x);
        }
    }
}

调了好久


|