FHQ-Treap 全 WA 求助
查看原帖
FHQ-Treap 全 WA 求助
688783
SilverLi楼主2024/1/2 17:32
#include <iostream>
#include <random>
#include <ctime>
#define int long long
using namespace std;
inline int read()
{
    int ans = 0, f = 0;
    char c = getchar();
    while (!isdigit(c))
        f |= (c == '-'), c = getchar();
    while (isdigit(c))
        ans = (ans << 3) + (ans << 1) + c - 48, c = getchar();
    return f ? -ans : ans;
}
void write(int x)
{
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        write(x / 10);
    putchar(48 + x % 10);
}
const int N = 1e5 + 5;
int n, m, val[N], mx[N];
int cnt, root, ad[N], tag[N];
int si[N], pr[N], son[N][2];
// mt19937 RAND(time(0));
inline int _new(int k)
{
    si[++cnt] = 1, pr[cnt] = rand();
    val[cnt] = mx[cnt] = k;
    return cnt;
}
inline void updata(int p)
{
    si[p] = 1 + si[son[p][0]] + si[son[p][1]];
    mx[p] = val[p];
    if (son[p][0])
        mx[p] = max(mx[p], mx[son[p][0]]);
    if (son[p][1])
        mx[p] = max(mx[p], mx[son[p][1]]);
}
inline void down(int p)
{
    if (ad[p])
    {
        val[son[p][0]] += ad[p];
        val[son[p][1]] += ad[p];
        mx[son[p][0]] += ad[p];
        mx[son[p][1]] += ad[p];
        ad[son[p][0]] += ad[p];
        ad[son[p][1]] += ad[p];
        ad[p] = 0;
        updata(p);
    }
    if (tag[p])
    {
        swap(son[p][0], son[p][1]);
        tag[son[p][0]] ^= 1;
        tag[son[p][1]] ^= 1;
        tag[p] = 0;
    }
}
void split(int p, int v, int &x, int &y)
{
    if (!p)
    {
        x = y = 0;
        return;
    }
    down(p);
    if (si[son[p][0]] < v)
        x = p, split(son[p][1], v, son[p][1], y);
    else
        y = p, split(son[p][0], v - si[son[p][0]] - 1, x, son[p][0]);
    updata(p);
}
int merge(int x, int y)
{
    if (!x || !y)
        return x | y;
    down(x), down(y);
    if (pr[x] < pr[y])
    {
        son[x][1] = merge(son[x][1], y);
        updata(x);
        return x;
    }
    son[y][0] = merge(x, son[y][0]);
    updata(y);
    return y;
}
signed main()
{
    srand(time(0));
    int root = 0;
    n = read(), m = read();
    for (int i = 1; i <= n; ++i)
        root = merge(root, _new(0));
    while (m--)
    {
        int opt = read(), l = read(), r = read();
        if (opt == 1)
        {
            int v = read();
            int x, y, x1, y1;
            split(root, l - 1, x, y);
            split(y, r - l + 1, x1, y1);
            val[x1] += v, mx[x1] += v, ad[x1] += v;
            root = merge(x, merge(x1, y1));
        }
        if (opt == 2)
        {
            int x, y, x1, y1;
            split(root, l - 1, x, y);
            split(y, r - l + 1, x1, y1);
            tag[x1] ^= 1;
            root = merge(x, merge(x1, y1));
        }
        if (opt == 3)
        {
            int x, y, x1, y1;
            split(root, l - 1, x, y);
            split(y, r - l + 1, x1, y1);
            write(mx[x1]);
            putchar('\n');
            root = merge(x, merge(x1, y1));
        }
    }
    return 0;
}
2024/1/2 17:32
加载中...