【BZOJ 1230】开关灯

3 篇文章 0 订阅
2 篇文章 0 订阅

题意:有一排 N(N100000) 盏灯,一开始全都是关闭状态。现有两种操作,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;
}

最近因为一些事耽搁了不少时间,刷的题好像有点少啊。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值