Question Link
Question:
There is a string that contains only 0 and 1, and then the M group operation, I L R is to flip from L to R 0 to 1, 1 to 0, q X indicates the X character.
Ideas:
We only need to calculate how many times the query character has been flipped over. If it is an even number of times, this character is changed; otherwise, it is flipped over. We can use the line segment tree for Interval Update. However, for this question, it is only a point query, and each node has the same initial value, which is 0, therefore, we can directly use a tree array. The following is a clever practice that is easy to understand.
Use the Interval Update single-point lookup function of the tree array (generally the single-point Update Interval lookup)
For example, interval (2, 4) and 1
Then updata (2, 1) updata (4 + 1,-1)
Implemented the update (2, 4) value without changing other values
When sum is used, a certain value is obtained.
//LA 1080 - Binary Simulation//2013-04-12-18.52#include <stdio.h>#include <string.h>const int maxn = 100010;char str[maxn];int n;int sum[maxn];int lowbit(int x){ return x&(-x);}void change(int x, int v){ while (x <= n) { sum[x] += v; x += lowbit(x); }}int get(int x){ int s = 0; while (x) { s += sum[x]; x -= lowbit(x); } return s;}void update(int l, int r){ change(l, 1); change(r+1, -1);}int main(){ int t, m; scanf("%d",&t); for(int i = 1; i <= t; i++) { memset(sum, 0, sizeof(sum)); scanf("%s",&str[1]); scanf("%d",&m); n =strlen(&str[1]); char op; int l, r; printf("Case %d:\n",i); while (m--) { getchar(); scanf("%c",&op); if (op == 'I') { scanf("%d %d",&l, &r); update(l, r); } else { scanf("%d",&l); if (get(l)%2 == 1) { if (str[l] == '1') puts("0"); else puts("1"); } else printf("%c\n",str[l]); } } } return 0;}
You can also use the line segment tree to solve this problem. If you are interested, you can try it.