设为首页 加入收藏

TOP

洛谷P4344 [SHOI2015]脑洞治疗仪(ODT)
2019-02-09 12:08:21 】 浏览:27
Tags:洛谷 P4344 SHOI2015 治疗仪 ODT

题意

题目链接

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\n", Query(l, r));
    }
    return 0;
}

编程开发网
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇洛谷P2572 [SCOI2010]序列操作(OD.. 下一篇01分数规划

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

array(4) { ["type"]=> int(8) ["message"]=> string(24) "Undefined variable: jobs" ["file"]=> string(32) "/mnt/wp/cppentry/do/bencandy.php" ["line"]=> int(214) }