题意:有一排 N(N≤100000) 盏灯,一开始全都是关闭状态。现有两种操作,A.改变 [l,r] 区间所有灯的状态;B.询问 [l,r] 区间打开灯的盏数。
线段树裸题,每个节点用一个布尔型变量保存tag,每次修改时取反。
很水的题,然而我WA了3发。
我以为c++有&=,有|=,还有^=,一定会有!=对不对!对不对!对不对!
宛如一个智障23333333
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string.h>
#include<cmath>
#include<cstdlib>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int INF = 2147483647;
int n,m,x,y;
int tree[400010];
bool tag[400010];
inline void Init(int &x){
char y;while ((y = getchar()) < '0' || y > '9');
x = y - '0';
while ((y = getchar()) >= '0' && y <= '9') x = (x << 3) + (x << 1) + y - '0';
}
inline void Pushdown(int l,int r,int now){
int mid = l + r >> 1;
tree[now << 1] = mid - l + 1 - tree[now << 1];
tree[(now << 1) + 1] = r - mid - tree[(now << 1) + 1];
tag[now << 1] ^= 1;
tag[(now << 1) + 1] ^= 1;
tag[now] = 0;
}
void work(int l,int r,int now){
if (l >= x && r <= y){
tree[now] = r - l + 1 - tree[now];
tag[now] ^= 1;
return;
}
if (tag[now]) Pushdown(l,r,now);
int mid = l + r >> 1;
if (mid >= x) work(l,mid,now << 1);
if (mid < y) work(mid + 1,r,(now << 1) + 1);
tree[now] = tree[now << 1] + tree[(now << 1) + 1];
}
int sum(int l,int r,int now){
if (l >= x && r <= y) return tree[now];
if (tag[now]) Pushdown(l,r,now);
int s = 0,mid = l + r >> 1;
if (mid >= x) s += sum(l,mid,now << 1);
if (mid < y) s += sum(mid + 1,r,(now << 1) + 1);
return s;
}
int main(){
scanf("%d%d",&n,&m);
memset(tree,0,sizeof(tree));
memset(tag,0,sizeof(tag));
for (int i = 1;i <= m;++i){
int op;
scanf("%d%d%d",&op,&x,&y);
if (op) printf("%d\n",sum(1,n,1));
else work(1,n,1);
}
return 0;
}
最近因为一些事耽搁了不少时间,刷的题好像有点少啊。