题目链接 珂朵莉树是一个可以维护区间x次方和查询的高效数据结构,原理十分暴力。
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
typedef pair<int,ll> pil;
struct ChthollyTree:map<int,pil>
{
iterator split(int pos)
{
iterator it=lower_bound(pos);
if(it!=end()&&it->first==pos)return it;
--it;
if(pos>it->second.first)return end();
pair<int,pil> p=*it;
erase(it);
insert(make_pair(p.first,pil(pos-1,p.second.second)));
return insert(make_pair(pos,p.second)).first;
}
void set(int l,int r,ll val)
{
erase(split(l),split(r+1)),insert(make_pair(l,pil(r,val)));
}
void scure(int l,int r,int x,int y)
{
iterator e=split(r+1),b=split(l),it=b;
int sum=0;
for(; b!=e; ++b)
if(b->second.second)
sum+=b->second.first-b->first+1;
erase(it,e);
insert(make_pair(l,pil(r,0)));
if(!sum)return;
e=split(y+1),b=split(x),it=b;
if(sum>=y-x+1)
{
erase(b,e);
insert(make_pair(x,pil(y,1)));
return;
}
for( ; b!=e; ++b)
if(!b->second.second)
{
sum-=b->second.first-b->first+1;
if(sum<0)return set(b->first,b->second.first+sum,1);
b->second.second=1;
}
}
ll MAX(int l,int r)
{
iterator e=split(r+1),b=split(l);
ll res=0,now=0;
for(; b!=e; ++b)
if(!b->second.second)
now+=b->second.first-b->first+1;
else if(now)res=max(res,now),now=0;
return max(res,now);
}
} t;
int main()
{
int n,m;
scanf("%d%d",&n,&m);
t.insert(make_pair(1,pil(n,1)));
for(int op,l,r,x,y; m--;)
{
scanf("%d%d%d",&op,&l,&r);
if(op==0)t.set(l,r,0);
else if(op==1)scanf("%d%d",&x,&y),t.scure(l,r,x,y);
else printf("%d\n",t.MAX(l,r));
}
}
![]() |
![]() |