NOIP模拟 行星通道计划(二维树状数组)
QAQ
【题目分析】
考虑树状数组维护状态,然后。。。。。就没有然后了。。。。。没想出来二维如何统计。
感觉正解很毒瘤啊,各种压位操作。。。。所以正常打个二维树状数组还是能过啊。。。。
考虑两个连接点x,y,如果以后有两个点x',y'经过他,那么一定满足x<x'<y<y'或x'<x<y'<y,所以相当于1~x-1,x+1~y-1的点相连的代价增加1,x+1~y-1,y+1~n的代价加1,树状数组记录一下即可。
【代码~】
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e3+10;
const int MAXM=5e5+10;
int tr[MAXN][MAXN];
int ll[MAXM],rr[MAXM];
int n,q;
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int y,int v)
{
for(int i=x;i<=n;i+=lowbit(i))
for(int j=y;j<=n;j+=lowbit(j))
tr[i][j]+=v;
}
int query(int x,int y)
{
int ret=0;
for(int i=x;i;i-=lowbit(i))
for(int j=y;j;j-=lowbit(j))
ret+=tr[i][j];
return ret;
}
int Read()
{
int i=0,f=1;
char c;
for(c=getchar();(c>'9'||c<'0')&&c!='-';c=getchar());
if(c=='-')
f=-1,c=getchar();
for(;c>='0'&&c<='9';c=getchar())
i=(i<<3)+(i<<1)+c-'0';
return i*f;
}
int main()
{
n=Read(),q=Read();
for(int i=1;i<=q;++i)
{
int cz=Read();
if(!cz)
{
int x=Read(),y=Read();
if(x>y)
swap(x,y);
printf("%d\n",query(x,y));
ll[i]=x,rr[i]=y;
if(x!=1)
{
update(1,x+1,1);
update(1,y,-1);
update(x,x+1,-1);
update(x,y,1);
}
if(y!=n)
{
update(x+1,y+1,1);
update(x+1,n+1,-1);
update(y,y+1,-1);
update(y,n+1,1);
}
}
else
{
int id=Read();
int x=ll[id],y=rr[id];
if(x!=1)
{
update(1,x+1,-1);
update(1,y,1);
update(x,x+1,1);
update(x,y,-1);
}
if(y!=n)
{
update(x+1,y+1,-1);
update(x+1,n+1,1);
update(y,y+1,1);
update(y,n+1,-1);
}
}
}
return 0;
}