[CQOI 2006]线段树之简单题

Description

有一个n个元素的数组,每个元素初始均为0。有m条指令,要么让其中一段连续序列数字反转--0变1,1变0(操作1),要么询问某个元素的值(操作2)。例如当n=20时,10条指令如下:

Input

第一行包含两个整数n,m,表示数组的长度和指令的条数,以下m行,每行的第一个数t表示操作的种类。若t=1,
则接下来有两个数L, R (L<=R),表示区间[L, R]的每个数均反转;若t=2,则接下来只有一个数I,表示询问的下
标。1<=n<=100,000,1<=m<=500,000

Output

每个操作2输出一行(非0即1),表示每次操作2的回答

Sample Input
20 10
1 1 10
2 6
2 12
1 5 12
2 6
2 15
1 6 16
1 11 17
2 12
2 6

Sample Output
1
0
0
0
1
1


这题是个线段树裸题,只需要维护一个标记即可。0和1的转变如何记录?标记++就好,输出答案只要(land) 1 即可

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
	int x=0,f=1;char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')    f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
inline void print(int x){
	if (x>=10)     print(x/10);
	putchar(x%10+'0');
}
const int N=1e5;
struct Segment{
	#define ls (p<<1)
	#define rs (p<<1|1)
	int tree[N*4+10];
	void pushdown(int p){
		if (!tree[p])	return;
		tree[ls]+=tree[p];
		tree[rs]+=tree[p];
		tree[p]=0;
	}
	void change(int p,int l,int r,int x,int y){
		if (x<=l&&r<=y){tree[p]++;return;}
		int mid=(l+r)>>1;
		if (x<=mid)	change(ls,l,mid,x,y);
		if (y>mid)	change(rs,mid+1,r,x,y);
	}
	int query(int p,int l,int r,int x){
		if (l==r)	return tree[p];
		pushdown(p);
		int mid=(l+r)>>1;
		return x<=mid?query(ls,l,mid,x):query(rs,mid+1,r,x);
	}
}T;
int main(){
	int n=read(),m=read();
	for (int i=1;i<=m;i++){
		int t=read(),x,y;
		if (t==1)	x=read(),y=read(),T.change(1,1,n,x,y);
		if (t==2)	x=read(),printf("%d
",T.query(1,1,n,x)&1);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/Wolfycz/p/8414571.html