二维线段树,卡了一下午,求和时,sum会溢出,所以类型用__int64
#include<cstdio>
using namespace std;
const int MAXN = 811111;
struct tree{
int cnt[MAXN], lazy[MAXN];
inline void rev(int id, int l, int r)
{
cnt[id] = r-l+1-cnt[id];
lazy[id]^=1;
}
void update(int id, int l, int r, int st, int en)
{
if(l==st&&r==en)
{
rev(id,l,r);
return ;
}
int mid = (st+en)>>1;
if(lazy[id])
{
lazy[id]=0;
rev(id<<1,st,mid);
rev(id<<1|1, mid+1,en);
}
if(r<=mid) update(id<<1,l,r,st,mid);
else if(l>mid) update(id<<1|1,l,r,mid+1,en);
else
{
update(id<<1,l,mid,st,mid);
update(id<<1|1,mid+1,r,mid+1,en);
}