第三次求调(玄关)
查看原帖
第三次求调(玄关)
377842
liuxy1234楼主2023/8/29 20:30

RT,炸了一个暑假了/kk

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

inline int read()
{
    register int x = 0, f = 1;
    register char c = getchar();
    while(c < '0' || c > '9')
    {
        if(c == '-')f = -1;
        c = getchar();
    }
    while(c <= '9' && c >= '0')
    {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}

inline void write(int x)
{
    if(!x)return;
    write(x / 10);
    putchar(x % 10 + '0');
    return; 
}

struct node
{
    int val, ch[2], rnd, size, lsum, rsum, sum, num, lazy, set;
}t[500010];

struct pr
{
    int start, len;
};

bool deleted[500010];

queue <pr> qu;

int newnode(int val)
{
    pr a = qu.front();
    qu.pop();
    int cnt = a.start;
    deleted[cnt] = 0;
    t[cnt].ch[0] = t[cnt].ch[1] = 0;
    t[cnt].lsum = t[cnt].rsum = t[cnt].sum = t[cnt].num = val;
    t[cnt].rnd = rand();
    t[cnt].size = 1;
    t[cnt].val = val;
    t[cnt].lazy = t[cnt].set = 0;
    if(a.len > 1)a.len--, a.start++, qu.push(a);
    return cnt;
}

void pushdown(int q)
{
    if(t[q].set)
    {
        t[t[q].ch[0]].set = t[t[q].ch[1]].set = t[q].set;
        t[q].lsum = t[q].rsum = t[q].sum = t[q].num = (t[q].set * t[q].size);
        t[q].val = t[q].set;
        t[q].set = 0;
    }
    if(t[q].lazy)
    {
        t[t[q].ch[0]].lazy ^= 1;
        t[t[q].ch[1]].lazy ^= 1;
        swap(t[q].ch[0], t[q].ch[1]);
        swap(t[q].lsum, t[q].rsum);
        t[q].lazy = 0;
    }
    return;
}

void update(int q)
{
    pushdown(q);
    pushdown(t[q].ch[0]);
    pushdown(t[q].ch[1]);
    t[q].size = t[t[q].ch[0]].size + t[t[q].ch[1]].size + 1;
    t[q].lsum = max(t[t[q].ch[0]].sum + t[q].val + t[t[q].ch[1]].lsum, t[t[q].ch[0]].lsum);
    t[q].rsum = max(t[t[q].ch[1]].sum + t[q].val + t[t[q].ch[0]].rsum, t[t[q].ch[1]].rsum);
    t[q].sum = max(t[t[q].ch[1]].sum, t[t[q].ch[0]].rsum + t[q].val + t[t[q].ch[1]].lsum);
    t[q].sum = max(t[q].sum, t[t[q].ch[0]].sum);
    t[q].num = t[t[q].ch[0]].num + t[t[q].ch[1]].num + t[q].val;
    return;
}

void split(int id, int k, int &x, int &y)
{
    if(!id)
    {
        x = y = 0;
        return;
    }
    pushdown(id);
    int lsize = t[t[id].ch[0]].size;
    if(lsize < k)
    {
        x = id;
        split(t[x].ch[1], k - lsize - 1, t[x].ch[1], y);
    }
    else
    {
        y = id;
        split(t[y].ch[0], k, x, t[y].ch[0]);
    }
    update(id);
    return;
}

int merge(int x, int y)
{
    if(!x || !y)
    {
        return x + y;
    }
    pushdown(x);
    pushdown(y);
    if(t[x].rnd < t[y].rnd)
    {
        t[x].ch[1] = merge(t[x].ch[1], y);
        update(x);
        return x;
    }
    else
    {
        t[y].ch[0] = merge(x, t[y].ch[0]);
        update(y);
        return y;
    }
}

int n, m, rt;

void dlt(int q)
{
    if(q == 0)return;
    dlt(t[q].ch[0]);
    dlt(t[q].ch[1]);
    pr a;
    deleted[q] = 1;
    a.start = q, a.len = 1;
    qu.push(a);
    return;
}

void query(int x)
{
    if(!x)return;
//  pushdown(x);
    if(!deleted[t[x].ch[0]])query(t[x].ch[0]);
    cout << x << " " << t[x].val << " " << t[x].lsum << " " << t[x].rsum << " " << t[x].sum << " " << t[x].size << " " << t[x].lazy << " " << t[x].set << " " << t[x].num << " " << t[x].ch[0] << " " << t[x].ch[1] << "\n";
    if(!deleted[t[x].ch[1]])query(t[x].ch[1]);
//  update(x);
    return;
}

signed main()
{
    memset(deleted, 1, sizeof(deleted)); 
    cin >> n >> m;
    pr a;
    a.start = 1, a.len = 500000;
    qu.push(a);
    for(int i = 1;i <= n;i++)
    {
        int val;
        val = read();
        rt = merge(rt, newnode(val));
    }
    string s;
    while(m--)
    {
        cin >> s;
        if(s == "GET-SUM")
        {
            int pos, len;
            pos = read(), len = read();
            int x, y, z;
            split(rt, pos - 1, x, y);
            split(y, len, y, z);
            if(y)
            {
                cout << t[y].num << "\n";
            }
//          query(y);
            rt = merge(x, merge(y, z));
        }
        if(s == "INSERT")
        {
            int pos, tot, x, y;
            pos = read(), tot = read();
            split(rt, pos, x, y);
            for(int i = 1;i <= tot;i++)
            {
                int val = read();
                x = merge(x, newnode(val));
            }
            rt = merge(x, y);
        }
        if(s == "DELETE")
        {
            int pos, len;
            pos=  read(), len = read();
            int x, y, z;
            split(rt, pos - 1, x, y);
            split(y, len, y, z);
            dlt(y);
            rt = merge(x, z);
        }
        if(s == "MAKE-SAME")
        {
            int pos, len, x, y, st, z;
            pos = read(), len = read(), st = read();
            split(rt, pos - 1, x, y);
            split(y, len, y, z);
            if(y)
            {
                t[y].set = st;
                t[y].val = st;
            }
            rt = merge(x, merge(y, z));
        }
        if(s == "REVERSE")
        {
            int pos, len, x, y, z;
            pos = read(), len = read();
            split(rt, pos - 1, x, y);
            split(y, len, y, z);
            if(y)
            {
                t[y].lazy ^= 1;
            }
            rt = merge(x, merge(y, z));
        }
        if(s == "MAX-SUM")
        {
//          query(rt);
            update(rt);
            cout << t[rt].sum << "\n";
        }
//      query(rt);
    }
    return 0;
}
2023/8/29 20:30
加载中...