[CQOI2006] 簡單題 - 線段樹/樹狀數組


題目描述

有一個n個元素的數組,每個元素初始均爲0。有m條指令,要麼讓其中一段連續序列數字反轉——0變1,1變0(操作1),要麼詢問某個元素的值(操作2)。例如當n=20時,10條指令如下:
這裏寫圖片描述


輸入格式

第一行包含兩個整數n,m,表示數組的長度和指令的條數,以下m行,每行的第一個數t表示操作的種類。若t=1,則接下來有兩個數L, R (L<=R),表示區間[L, R]的每個數均反轉;若t=2,則接下來只有一個數I,表示詢問的下標。


輸出格式

每個操作2輸出一行(非0即1),表示每次操作2的回答


樣例數據

樣例輸入

20 10
1 1 10
2 6
2 12
1 5 12
2 6
2 15
1 6 16
1 11 17
2 12
2 6

樣例輸出

1
0
0
0
1
1


說明

50%的數據滿足:1<=n<=1,000,1<=m<=10,000
100%的數據滿足:1<=n<=100,000,1<=m<=500,000


題目分析

線段樹 區間修改查詢點

樹狀數組
模板題+1


源代碼

線段樹

#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
inline const int Get_Int() {
    int num=0,bj=1;
    char x=getchar();
    while(x<'0'||x>'9') {
        if(x=='-')bj=-1;
        x=getchar();
    }
    while(x>='0'&&x<='9') {
        num=num*10+x-'0';
        x=getchar();
    }
    return num*bj;
}
const int maxn=100000;
struct Tree { //修改區間 查詢點
    int left,right,data;
};
struct Segment_Tree { //執行加法mod2操作
    Tree tree[maxn*4];
    int sum;
    void build(int index,int Left,int Right) {
        tree[index].left=Left;
        tree[index].right=Right;
        tree[index].data=0;
        if(Left==Right)return;
        int mid=(Left+Right)/2;
        build(2*index,Left,mid);
        build(2*index+1,mid+1,Right);
    }
    void increase(int index,int Left,int Right,int delta) { //增加區間的值
        if(Right<tree[index].left||Left>tree[index].right)return; //不相交
        if(Left<=tree[index].left&&Right>=tree[index].right) { //完全包含
            tree[index].data+=delta;
            return;
        }
        increase(index*2,Left,Right,delta);
        increase(index*2+1,Left,Right,delta);
    }
    void init() { //注意多次詢問一定要調用
        sum=0;
    }
    void Query(int index,int target) { //查詢點值
        if(target<tree[index].left||target>tree[index].right)return; //不相交
        if(target>=tree[index].left&&target<=tree[index].right)sum+=tree[index].data; //完全包含
        if(tree[index].left==tree[index].right)return;
        Query(index*2,target);
        Query(index*2+1,target);
    }
};
Segment_Tree st;
int n,m;
int main() {
    n=Get_Int();
    m=Get_Int();
    st.build(1,1,n);
    for(int i=1; i<=m; i++) {
        int order=Get_Int();
        if(order==1) {
            int Left=Get_Int(),Right=Get_Int();
            st.increase(1,Left,Right,1);
        } else {
            int target=Get_Int();
            st.init();
            st.Query(1,target);
            printf("%d\n",st.sum%2);
        }
    }
    return 0;
}

樹狀數組

#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
int n,m,f[100005];
inline const int Get_Int() {
    int n=0,bj=1;
    char x=getchar();
    while(x<'0'||x>'9') {
        if(x=='-')bj=-1;
        x=getchar();
    }
    while(x>='0'&&x<='9') {
        n=n*10+x-'0';
        x=getchar();
    }
    return n*bj;
}
int Lowbit(int x) {
    return x&-x;
}
void Add(int x,int d) {
    for(int i=x; i<=n; i+=Lowbit(i))f[i]+=d;
}
int Ask(int r) {
    int sum=0;
    for(int i=r; i>=1; i-=Lowbit(i))sum+=f[i];
    return sum;
}
int main() {
    n=Get_Int();
    m=Get_Int();
    for(int i=1; i<=m; i++) {
        int order=Get_Int();
        if(order==1) {
            int x=Get_Int(),y=Get_Int();
            Add(x,1);
            Add(y+1,-1);
        } else {
            int x=Get_Int();
            printf("%d\n",Ask(x)%2);
        }
    }
    return 0;
}

發佈了169 篇原創文章 · 獲贊 31 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章