[洛谷P5057][CQOI2006]简单题

题目大意:有一个长度为$n$的$01$串,两个操作:

  1. $1\;l\;r:$把区间$[l,r]$翻转($0->1,1->0$)
  2. $2\;p:$求第$p$位是什么

题解:维护前缀异或和,树状数组即可

卡点:

C++ Code:

#include <cstdio>
#include <cctype>

namespace std {
	struct istream {
#define M (1 << 24 | 3)
		char buf[M], *ch = buf - 1;
		inline istream() {
#ifndef ONLINE_JUDGE
			freopen("input.txt", "r", stdin);
#endif
			fread(buf, 1, M, stdin);
		}
		inline istream& operator >> (int &x) {
			while (isspace(*++ch));
			for (x = *ch & 15; isdigit(*++ch); ) x = x * 10 + (*ch & 15);
			return *this;
		}
#undef M
	} cin;
	struct ostream {
#define M (1 << 24 | 3)
		char buf[M], *ch = buf - 1;
		int w;
		inline ostream& operator << (int x) {
			if (!x) {
				*++ch = '0';
				return *this;
			}
			for (w = 1; w <= x; w *= 10);
			for (w /= 10; w; w /= 10) *++ch = (x / w) ^ 48, x %= w;
			return *this;
		}
		inline ostream& operator << (const char x) {*++ch = x; return *this;}
		inline ostream& operator << (const char *x) {
			while (*x) *this << *x++;
			return *this;
		}
		inline ~ostream() {
#ifndef ONLINE_JUDGE
			freopen("output.txt", "w", stdout);
#endif
			fwrite(buf, 1, ch - buf + 1, stdout);
		}
#undef M
	} cout;
}

#define maxn 100010

int n, m;
namespace BIT {
	int Tr[maxn], res;
	inline void add(int p) {for (; p <= n; p += p & -p) Tr[p] ^= 1;}
	inline int ask(int p) {for (res = 0; p; p &= p - 1) res ^= Tr[p]; return res;}
}

int main() {
	std::cin >> n >> m;
	while (m --> 0) {
		int op, l, r;
		std::cin >> op >> l;
		if (op == 1) {
			std::cin >> r;
			BIT::add(l), BIT::add(r + 1);
		} else std::cout << BIT::ask(l) << '\n';
	}
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/Memory-of-winter/p/10127483.html
今日推荐