Codeforces 915E Physical Education Lessons

题意简述

维护序列,支持以下操作:

  • 区间赋1/0
  • 求整个区间之和

题解思路

珂朵莉树暴力赋值,查询

代码

#include <set>
#include <cstdio>
#include <cctype>
#define IT std::set<Node>::iterator
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char ch,buf[1<<21],*p1=buf,*p2=buf;
inline void read(int& x) {
    for (ch=getc();!isdigit(ch);ch=getc());
    for (x=ch-'0';isdigit(ch=getc());x=x*10+ch-'0');
}
int q,l,r,opt,sum;
struct Node 
{
    int l,r; bool v;
    Node(const int& L,const int& R,const bool& V):l(L),r(R),v(V) {}
    bool operator <(const Node& x) const { return l<x.l; }
};
std::set<Node> s; 
inline IT split(const int& x) {
    IT it=s.lower_bound(Node(x,0,0));
    if (it!=s.end()&&it->l==x) return it;
    --it; int L=it->l,R=it->r; bool V=it->v; s.erase(it);
    s.insert(Node(L,x-1,V)); return s.insert(Node(x,R,V)).first;
}
int main() {
    read(sum);read(q);
    s.insert((Node){1,sum,1});
    for (;q;--q) {
        read(l);read(r);read(opt);
        IT itr=split(r+1),itl=split(l);
        for (register IT it=itl;it!=itr;++it) if (it->v) sum-=it->r-it->l+1;
        s.erase(itl,itr); s.insert(Node(l,r,--opt)); if (opt) sum+=r-l+1;
        printf("%d\n",sum);
    }
}

转载于:https://www.cnblogs.com/xuyixuan/p/11360829.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值