P4344 [SHOI2015]脑洞治疗仪
2019-11-15 10:58 一只弱鸡丶 阅读(125) 评论(0) 编辑 收藏 举报#include <bits/stdc++.h> using namespace std; #define ll long long #define re register #define pb push_back #define fi first #define se second void read(int &a) { a=0;int d=1;char ch; while(ch=getchar(),ch>'9'||ch<'0') if(ch=='-') d=-1; a=ch^48; while(ch=getchar(),ch>='0'&&ch<='9') a=(a<<3)+(a<<1)+(ch^48); a*=d; } struct note { int l,r; mutable int v; note(int L,int R=-1,int V=0){l=L,r=R,v=V;} bool operator < (const note &x) const { return l<x.l; } }; set <note> s; set <note> :: iterator split(int pos) { auto it=s.lower_bound(note(pos)); if(it!=s.end()&&it->l==pos) return it; it--; if(it->r<pos) return s.end(); int L=it->l,R=it->r,V=it->v; s.erase(it); s.insert(note(L,pos-1,V)); return s.insert(note(pos,R,V)).fi; } void modify(int l,int r,int w) { auto it2=split(r+1),it1=split(l); s.erase(it1,it2); s.insert(note(l,r,w)); } void change(int l,int r,int x,int y) { int sum=0; auto it2=split(r+1),it1=split(l),it=it1; for(;it1!=it2;it1++) sum+=it1->v?it1->r-it1->l+1:0; s.erase(it,it2); s.insert(note(l,r,0)); if(!sum) return; it2=split(y+1),it1=split(x); if(sum>=y-x+1) { s.erase(it1,it2); s.insert(note(x,y,1)); return; } for(;it1!=it2;it1++) { if(!it1->v) { sum-=it1->r-it1->l+1; if(sum<0) { modify(it1->l,it1->r+sum,1); break; } else it1->v=1; } } } int ask(int l,int r) { int sum=0,ans=0; auto it2=split(r+1),it1=split(l); for(;it1!=it2;it1++) { if(!it1->v) sum+=it1->r-it1->l+1; else ans=max(sum,ans),sum=0; } return max(ans,sum); } int main() { int n,m; read(n),read(m); s.insert(note(1,n,1)); s.insert(note(n+1,n+1,0)); for(re int i=1,op,l,r,x,y;i<=m;i++) { read(op); if(op==0) read(l),read(r),modify(l,r,0); else if(op==1) read(l),read(r),read(x),read(y),change(l,r,x,y); else read(l),read(r),printf("%d\n",ask(l,r)); } return 0; }