y+1标记为-1或1为什么都对?
查看原帖
y+1标记为-1或1为什么都对?
602632
_7Mr楼主2023/3/4 11:09
#include<bits/stdc++.h>
using namespace std;
const int maxn=5*1e5+5;
int n,m;
int a[maxn],c[maxn];
int lowbit(int x) {
	return x&(-x);
}
void updata(int i,int k) {
	for(; i <= n; i += lowbit(i)) c[i] += k;
}
int getsum(int i) {
	int res = 0;
	for(; i>0; i -= lowbit(i)) res += c[i];
	return res;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie();
	cout.tie();
	cin>>n>>m;
	int op,x,y;
	for(int i=1;i<=m;i++){
		cin>>op;
		if(op==1) {
			cin>>x>>y;
			updata(x,1);
			updata(y+1,-1);//这里1或-1为什么都对???  

		} else {
			cin>>x;
			cout<<getsum(x)%2<<endl;
		}
	}
	return 0;
}
2023/3/4 11:09
加载中...