P5057 [CQOI2006]简单题

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 2e5 + 10;
int n, m;
int w[N];
struct node {
    int l, r;
    int tag;
}tr[N << 2];
void pushdown(int u)
{
    if (tr[u].tag) {
        tr[u << 1].tag = !tr[u << 1].tag;
        tr[u << 1 | 1].tag = !tr[u << 1 | 1].tag;
        tr[u].tag = !tr[u].tag;    
    }
}
void build(int u, int l, int r)
{
    if (l == r) {tr[u] = {l, r}; return;}
    tr[u] = {l, r};
    int mid = l + r >> 1;
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
}
int query(int u, int x)
{
    if (tr[u].l == x && tr[u].r == x)   return tr[u].tag;
    pushdown(u);
    int mid = tr[u].l + tr[u].r >> 1;
    if (x <= mid)   return query(u << 1, x);
    else    return query(u << 1 | 1, x);
}
void modify(int u, int l, int r)
{
    if (tr[u].l >= l  && tr[u].r <= r) {
        tr[u].tag = !tr[u].tag;
        return;
    }
    pushdown(u);
    int mid = tr[u].l + tr[u].r >> 1;
    if (l <= mid)   modify(u << 1, l, r);
    if (r > mid)    modify(u << 1 | 1, l, r);
}
int main() {
    scanf("%d%d", &n, &m);
    build(1, 1, n);
    while (m -- ) {
        int t;
        scanf("%d", &t);
        if (t == 1) {
            int l, r;
            scanf("%d%d", &l, &r);
            modify(1, l, r);
        }
        else {
            int x;
            scanf("%d", &x);
            printf("%d\n", query(1, x));
        }
    }
    return 0;
}
上一篇:随机向量:各向同性


下一篇:cf739 C. Alyona and towers(线段树)