1. 程式人生 > >洛谷P5057 [CQOI2006]簡單題(線段樹)

洛谷P5057 [CQOI2006]簡單題(線段樹)

題意

題目連結

Sol

紫色的線段樹板子題??。。。

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int MAXN = 1e6 + 10;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while (c < '0' || c > '9') {if (c == '-')f = -1; c = getchar();}
    while (c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    return x * f;
}
int N, M;
#define ls k << 1
#define rs k << 1 | 1
struct Node {
    int l, r, w, f;
}T[MAXN];
void Build(int k, int ll, int rr) {
    T[k].l = ll; T[k].r = rr; T[k].w = -1; T[k].f = 0;
    if(ll == rr)  {T[k].w = 0; return ;}
    int mid = (T[k].l + T[k].r) >> 1;
    Build(ls, ll, mid); Build(rs, mid + 1, rr);
}
void Reverse(int k, int ll, int rr) {
    if(ll <= T[k].l && T[k].r <= rr) {T[k].f ^= 1; return ;}
    int mid = (T[k].l + T[k].r) >> 1;
    if(ll <= mid) Reverse(ls, ll, rr);
    if(rr >  mid) Reverse(rs, ll, rr);
}
int Query(int k, int pos) {
    int ans = T[k].f;
    if(T[k].l == T[k].r) return ans;
    int mid = (T[k].l + T[k].r) >> 1;
    if(pos <= mid) ans ^= Query(ls, pos);
    if(pos  > mid) ans ^= Query(rs, pos);
    return ans;
}
int main() {
    N = read(); M = read();
    Build(1, 1, N);
    while(M--) {
        int opt = read();
        if(opt == 1) {
            int l = read(), r = read();
            Reverse(1, l, r);
        } else {
            printf("%d\n", Query(1, read()));
        }
    }
    return 0;
}