线段树模板题,开始用scanf("%c")一直错。。。
#include <iostream>
#include <cstdio>
#include <cstring>
const int MAX = 1e5 + 5;
char arr[MAX];
int res[3 * MAX];
void update(int idx, int st, int ed, int q_st, int q_ed)
{
if (st == q_st && q_ed == ed)
{
res[idx]++;
return;
}
int m = (st + ed) >> 1;
int l = idx << 1, r = l | 1;
if (q_ed <= m)
{
update(l, st, m, q_st, q_ed);
}
else if (q_st > m)
{
update(r,m + 1, ed, q_st, q_ed);
}
else
{
update(l, st, m, q_st, m);
update(r, m + 1, ed, m + 1, q_ed);
}
}
int query(int idx, int st, int ed, int pos)
{
if (st == pos && ed == pos)
{
return res[idx];
}
int m = (st + ed) >> 1;
int l = idx << 1, r = l | 1;
if (pos <= m)
{
return query(l, st, m, pos) + res[idx];
}
else
{
return query(r, m + 1, ed, pos) + res[idx];
}
}
int main()
{
int t, cas = 0;
scanf("%d", &t);
while (t--)
{
memset(res, 0, sizeof(res));
int q;
scanf("%s", arr + 1);
int lenth = strlen(arr + 1);
scanf("%d", &q);
char type[2];
int q_st, q_ed, pos;
printf("Case %d:\n", ++cas);
for (int i = 1; arr[i]; ++i)
{
if (arr[i] == '1')
update(1, 1, lenth, i, i);
}
for (int i = 0; i != q; ++i)
{
scanf("%s", type);
if (type[0] == 'I')
{
scanf("%d%d", &q_st, &q_ed);
update(1, 1, lenth, q_st, q_ed);
}
else if (type[0] == 'Q')
{
scanf("%d", &pos);
printf("%d\n",query(1, 1, lenth, pos) & 1);
}
}
}
return 0;
}