P5057 [CQOI2006]简单题
原题链接
考察:树状数组
思路:
类似于树状数组的扩展,利用差分数组将区间修改转为单点修改,单点查询改为区间查询.最后输出单点值%2即可.
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100010;
int n,m,tr[N];
int lowbit(int x)
{
return x&-x;
}
void add(int k,int x)
{
for(int i=k;i<=n;i+=lowbit(i)) tr[i]+=x;
}
int ask(int x)
{
int res = 0;
for(int i=x;i;i-=lowbit(i)) res+=tr[i];
return res;
}
int main()
{
scanf("%d%d",&n,&m);
while(m--)
{
int op,x,y;
scanf("%d%d",&op,&x);
if(op==1)
{
scanf("%d",&y);
add(x,1); add(y+1,-1);
}else printf("%d\n",(ask(x)%2+2)%2);
}
return 0;
}