题意简述
维护序列,支持以下操作:
- 区间赋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); } }