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,表示詢問的下標。
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
Hint
50%的數據滿足:1<=n<=1,000,1<=m<=10,000
100%的數據滿足:1<=n<=100,000,1<=m<=500,000
對於一個01序列來說,將其全部取反等價於整個序列異或1
區間修改單點查詢,BIT維護異或差分即可
#include <bits/stdc++.h>
#define Lowbit(x) x & -x
using namespace std;
const int MAXN = 100100;
const int INF = 0x3f3f3f3f;
template <typename T> inline void read(T &x) {
int ch = getchar();
bool fg = false;
for (x = 0; !isdigit(ch); ch = getchar()) {
if (ch == '-') {
fg = true;
}
}
for (; isdigit(ch); ch = getchar()) {
x = x * 10 + ch - '0';
}
if (fg) {
x = -x;
}
}
int n, Q;
int tre[MAXN];
void update(int x, int y) {
for(int i = x; i <= MAXN; i += Lowbit(i)) tre[i] ^= y;
}
int query(int x) {
int ret = 0;
for(int i = x; i; i -= Lowbit(i)) ret ^= tre[i];
return ret;
}
signed main() {
read(n); read(Q);
while(Q--) {
int com, l, r;
read(com);
if(com == 1) {
read(l), read(r);
update(l, 1);
update(r + 1, 1);
}
else {
read(l);
printf("%d\n", query(l));
}
}
return 0;
}