洛谷P4344 [SHOI2015]腦洞治療儀(ODT)

自為風月馬前卒發表於2019-02-09

題意

題目連結

Sol

ODT板子題。

操作1直接拆區間就行。

#include<bits/stdc++.h>
#define fi first
#define se second 
const int MAXN = 2e5 + 10;
using namespace std;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < `0` || c > `9`) {if(c == `-`) f = -1; c = getchar();}
    while(c >= `0` && c <= `9`) x = x * 10 + c - `0`, c = getchar();
    return x * f;
}
int N, M;
#define sit set<Node>::iterator 
struct Node {
    int l, r;
    mutable int v;
    bool operator < (const Node &rhs) const {
        return l < rhs.l;
    }
};
set<Node> s;
sit split(int p) {
    sit pos = s.lower_bound({p, 0, 0});
    if(pos->l == p) return pos;
    pos--;
    int L = pos->l, R = pos->r, V = pos->v;
    s.erase(pos);
    s.insert({L, p - 1, V});
    return s.insert({p, R, V}).fi;
}
void Mem(int l, int r) {
    sit ed = split(r + 1), bg = split(l);
    s.erase(bg, ed);
    s.insert({l, r, 0});
}
void Fix(int l0, int r0, int l, int r) {
    int num = 0;
    sit ed = split(r + 1), bg = split(l);
    for(sit i = bg; i != ed; i++) if(i->v == 1) num += i->r - i->l + 1;
    s.erase(bg, ed);
    s.insert({l, r, 0});
    ed = split(r0 + 1), bg = split(l0);
    sit gg; int RR = -1;
    for(sit i = bg; i != ed; i++) {
        if(i -> v == 1) continue;
        if(num <= 0) return ;
        int len = i->r - i->l + 1;
        if(len <= num) {i -> v = 1, num -= len; gg = i; RR = i->r; continue;}
        int L = i->l, R = i->r;
        s.erase(i);
        s.insert({L, L + num - 1, 1});
        s.insert({L + num, R, 0});
        return ;
    }
    if(RR > l0) {//Ò»¸ö²¢Ã»ÓÐʲôÂÑÓõÄÓÅ»¯ 
        gg++;
        s.erase(bg, gg);
        s.insert({l0, RR, 1});  
    }
}
int Query(int l, int r) {
    sit ed = split(r + 1), bg = split(l);
    int pre = 0, ans = 0;
    for(sit i = bg; i != ed; i++) {
        if(i->v == 0) ans = max(ans, i->r - i->l + 1 + pre), pre += i->r - i->l + 1;
        else pre = 0;
    }
    return ans;
}
int main() {
    //freopen("a.in", "r", stdin);
    N = read(); M = read();
    for(int i = 1; i <= N; i++) s.insert({i, i, 1});
    s.insert({N + 1, N + 1, 1});
    for(int i = 1; i <= M; i++) {
        int opt = read(), l = read(), r = read();
        if(opt == 0) Mem(l, r);
        else if(opt == 1) {
            int l1 = read(), r1 = read();
            Fix(l1, r1, l, r);
        } else printf("%d
", Query(l, r));
    }
    return 0;
}