Light OJ 1080 - Binary Simulation - (线段树区间更新 单点查询)

Description

Given a binary number, we are about to do some operations on the number. Two types of operations can be here.

‘I i j‘    which means invert the bit from
i to j (inclusive)

‘Q i‘    answer whether the ith bit is 0 or 1

The MSB (most significant bit) is the first bit (i.e. i=1). The binary number can contain leading zeroes.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case starts with a line containing a binary integer having length
n(1 ≤ n ≤ 105). The next line will contain an integer
q (1 ≤ q ≤ 50000) denoting the number of queries. Each query will be either in the form
‘I i j‘ where i, j are integers and 1 ≤ i ≤ j ≤ n. Or the query will be in the form
‘Q i‘ where i is an integer and 1 ≤ i ≤ n.

Output

For each case, print the case number in a single line. Then for each query
‘Q i‘ you have to print 1 or 0 depending on the ith bit.

Sample Input

2

0011001100

6

I 1 10

I 2 7

Q 2

Q 1

Q 7

Q 5

1011110111

6

I 1 10

I 2 7

Q 2

Q 1

Q 7

Q 5

Sample Output

Case 1:

0

1

1

0

Case 2:

0

0

0

1

题意:一串01序列,两种操作,一种是从i到j取反,一种是问第i个是0还是1.

把需要翻转的区间标记,记录这个区间的翻转次数,然后向下传标记,查询时只需看看这个点翻转奇数次还是偶数次。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <ctype.h>
#include <iostream>
#define lson o << 1, l, m
#define rson o << 1|1, m+1, r
using namespace std;
typedef long long LL;
const int MAX = 0x3f3f3f3f;
const int maxn = 111111;
int n, q, t, a, b, ans;
int cnt[maxn<<2];
char s[maxn], op[3];
void down(int o) {
    if(cnt[o]) {
        cnt[o<<1] += cnt[o];
        cnt[o<<1|1] += cnt[o];
        cnt[o] = 0;
    }
}
void update(int o, int l, int r) {
    if(a <= l && r <= b) {
        cnt[o] ++;
        return ;
    }
    down(o);
    int m = (l+r) >> 1;
    if(a <= m) update(lson);
    if(m < b ) update(rson);
}
void query(int o, int l, int r) {
    if(l == r) {
        ans = s[l]-'0';
        if(cnt[o]%2) ans = !ans;
        return ;
    }
    down(o);
    int m = (l+r) >> 1;
    if(a <= m) query(lson);
    else query(rson);
}
int main()
{
    cin >> t; for(int ca=1;ca<=t;ca++) {
        scanf("%s", s+1);
        n = strlen(s+1);
        memset(cnt, 0, sizeof(cnt));
        printf("Case %d:\n", ca);
        scanf("%d", &q); while(q--) {
            scanf("%s", op);
            if(op[0] == 'I') {
                scanf("%d%d", &a, &b);
                update(1, 1, n);
            } else {
                scanf("%d", &a);
                query(1, 1, n);
                printf("%d\n", ans);
            }
        }
    }
    return 0;
}



Light OJ 1080 - Binary Simulation - (线段树区间更新 单点查询)

时间: 2024-10-11 12:45:17

Light OJ 1080 - Binary Simulation - (线段树区间更新 单点查询)的相关文章

ZOJ 1610 Count the Colors【题意+线段树区间更新&amp;&amp;单点查询】

任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent

POJ 2777 Count Color (线段树区间更新加查询)

Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. There is a very long board with length L centimeter, L is a positive integer, so we can evenly d

Wikilo 1191线段树区间修改单点查询

这题也算比较容易的了. 如果哪个区间已经没有黑色的话,就不用update了,就是因为这个原因WA了2发,唉-- #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #incl

Light OJ 1080 - Binary Simulation

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1080 1080 - Binary Simulation PDF (English) problem=1080" style="color:rgb(79,107,114)">Statistics problem=1080" style="color:rgb(79,107,114)">Forum Time Limit:

HDU 5861 Road(线段树 区间修改 单点查询)

Road Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1132    Accepted Submission(s): 309 Problem Description There are n villages along a high way, and divided the high way into n-1 segments. E

POJ - 2155 Matrix (二维树状数组 + 区间改动 + 单点求值 或者 二维线段树 + 区间更新 + 单点求值)

POJ - 2155 Matrix Time Limit: 3000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we ha

ZOJ 1610 Count the Colors(线段树,区间覆盖,单点查询)

Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task is counting the segments of different colors you can s

POJ 2528 Mayor&#39;s posters (离散化+线段树区间更新)

Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for

codevs 1081 线段树练习 2 区间更新 单点查询 无lazy

题目描述 Description 给你N个数,有两种操作 1:给区间[a,b]的所有数都增加X 2:询问第i个数是什么? 输入描述 Input Description 第一行一个正整数n,接下来n行n个整数,再接下来一个正整数Q,表示操作的个数. 接下来Q行每行若干个整数.如果第一个数是1,后接3个正整数a,b,X,表示在区间[a,b]内每个数增加X,如果是2,后面跟1个整数i, 表示询问第i个位置的数是多少. 输出描述 Output Description 对于每个询问输出一行一个答案 样例输