水题AT1814题解

· · 题解

这题不就跟 P5057 差不多么。。。

由于区间异或满足区间加法原则,可以使用树状数组,把两个函数改动一下,就可以 AC 了。在代码里有标出改动处。

Code

#include <bits/stdc++.h>
using namespace std;
int n;
bool c[500005];
inline int lowbit(int n){
    return n&(-n);
}
void update(int x){
    while(x<=n){
        c[x]^=1;//①
        x+=lowbit(x);
    }
}
bool getsum(int x){
    bool ans=0;
    while(x){
        ans^=c[x];//②
        x-=lowbit(x);
    }
    return ans;
}
int main(){
    int m,op,x,y;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&x,&y);
        update(x);
        update(y+1);
    }
    for(int i=1;i<=n;i++)printf("%d",getsum(i));
    putchar('\n');
    return 0;
}

然后,就: