设为首页 加入收藏

TOP

洛谷P2572 [SCOI2010]序列操作(ODT)
2019-02-09 12:08:23 】 浏览:24
Tags:洛谷 P2572 SCOI2010 序列 操作 ODT

题解

题意

题目链接

Sol

ODT板子题.....

// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define LL long long 
#define Fin(x) freopen(#x".in", "r", stdin);
#define Fout(x) freopen(#x".out", "w", stdout); 
#define fi first
#define se second 
#define int long long 
using namespace std;
const int MAXN = 1e5 + 10;
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});
    if(pos != s.end() && 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, int v) {
    sit ed = split(r + 1), bg = split(l);
    s.erase(bg, ed);
    s.insert({l, r, v});
}
void Rev(int l, int r) {
    sit ed = split(r + 1), bg = split(l);
    for(sit i = bg; i != ed; i++) i->v ^= 1;
}
int QueryNum(int l, int r) {
    int ans = 0; sit ed = split(r + 1), bg = split(l);
    for(sit i = bg; i != ed; i++)
        if(i->v == 1) ans += i->r - i->l + 1;
    return ans;
}
int QuerySuc(int l, int r) {
    int ans = 0, pre = 0; sit ed = split(r + 1), bg = split(l);
    for(sit i = bg; i != ed; i++)
        if(i->v == 1) ans = max(ans, i->r - i->l + 1 + pre), pre += i->r - i->l + 1;
        else pre = 0;
    return ans;
}
signed main() {
    N = read(); M = read();
    for(int i = 1; i <= N; i++) s.insert({i, i, read()});
    s.insert({N + 1, N + 1, 0});
    for(int i = 1; i <= M; i++) {
        int op = read(), a = read() + 1, b = read() + 1;
        if(op == 0 || op == 1) Mem(a, b, op);
        else if(op == 2) Rev(a, b);
        else if(op == 3) printf("%d\n", QueryNum(a, b));
        else printf("%d\n", QuerySuc(a, b));
    }
    return 0;
}

编程开发网
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇codechef QCHEF(不删除莫队) 下一篇洛谷P4344 [SHOI2015]脑洞治疗仪(..

评论

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

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