bzoj1230: [Usaco2008 Nov]lites 开关灯(线段树)

14 篇文章 0 订阅

题目传送门
…..

解法:
裸线段树。。
打个lazy。。

代码实现:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
struct node {int l,r,lc,rc,c,n,lazy;}tr[210000];int len;
void bt(int l,int r) {
    int now=++len;tr[now].l=l;tr[now].r=r;tr[now].lc=tr[now].rc=-1;tr[now].c=0;tr[now].n=r-l+1;tr[now].lazy=0;
    if(l<r) {int mid=(l+r)/2;tr[now].lc=len+1;bt(l,mid);tr[now].rc=len+1;bt(mid+1,r);}
}
void update(int now) {
    int lc=tr[now].lc,rc=tr[now].rc;
    tr[now].lazy=0;tr[lc].lazy^=1;tr[rc].lazy^=1;
    tr[lc].c=tr[lc].n-tr[lc].c;tr[rc].c=tr[rc].n-tr[rc].c;
}
void change(int now,int l,int r) {
    if(tr[now].lazy)update(now);
    if(tr[now].l==l&&tr[now].r==r) {tr[now].c=(tr[now].n-tr[now].c);tr[now].lazy^=1;return ;}
    int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;
    if(r<=mid)change(lc,l,r);else if(l>mid)change(rc,l,r);
    else {change(lc,l,mid);change(rc,mid+1,r);}
    tr[now].c=tr[lc].c+tr[rc].c;
}
int find_sum(int now,int l,int r) {
    if(tr[now].lazy)update(now);
    if(tr[now].l==l&&tr[now].r==r)return tr[now].c;
    int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;
    if(r<=mid)return find_sum(lc,l,r);else if(l>mid)return find_sum(rc,l,r);
    else return find_sum(lc,l,mid)+find_sum(rc,mid+1,r);
}
int main() {
    int n,m;scanf("%d%d",&n,&m);len=0;bt(1,n);
    for(int i=1;i<=m;i++) {
        int t,x,y;scanf("%d%d%d",&t,&x,&y);
        if(x>y)swap(x,y);
        if(t==0)change(1,x,y);
        else printf("%d\n",find_sum(1,x,y));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
2018/07/20 周五 12:53 354 01求平均年龄.cpp 2018/07/20 周五 12:53 327 02财务管理.cpp 2018/07/20 周五 12:53 357 03均值.cpp 2018/07/20 周五 12:53 375 04求整数的和与平均值.cpp 2018/07/20 周五 12:54 375 05最高的分数.cpp 2018/07/20 周五 12:54 371 06整数序列的元素最大跨度值.cpp 2018/07/20 周五 12:55 430 07奥运奖牌计数.cpp 2018/07/20 周五 12:55 281 08多边形内角和.cpp 2018/07/20 周五 12:55 237 09奇数求和.cpp 2018/07/20 周五 12:56 239 10满足条件的数累加.cpp 2018/07/20 周五 12:56 403 11整数的个数.cpp 2018/07/20 周五 12:56 257 12与指定数字相同的数个数.cpp 2018/07/20 周五 12:56 217 13乘方计算.cpp 2018/07/20 周五 12:57 324 14人口增长问题.cpp 2018/07/20 周五 12:57 307 15银行利息.cpp 2018/07/20 周五 12:57 433 16买房子.cpp 2018/07/20 周五 12:57 260 17斐波那契数列.cpp 2018/07/20 周五 12:58 461 18鸡尾酒疗法.cpp 2018/07/20 周五 12:58 392 19救援.cpp 2018/07/20 周五 12:58 280 20球弹跳高度的计算.cpp 2018/07/20 周五 12:58 450 21角谷猜想.cpp 2018/07/20 周五 12:59 520 22津津的储蓄计划.cpp 2018/07/20 周五 12:59 400 23药房管理.cpp 2018/07/20 周五 12:59 642 24正常血压.cpp 2018/07/20 周五 13:00 453 25求特殊自然数.cpp 2018/07/20 周五 13:00 387 26统计满足条件的4位数个数.cpp 2018/07/20 周五 13:00 244 27级数求和.cpp 2018/07/20 周五 13:00 296 28分离整数的各个数位.cpp 2018/07/20 周五 13:01 443 29数字反转.cpp 2018/07/20 周五 13:01 341 30含k个3的数.cpp 2018/06/10 周日 14:07 545 31开关.cpp 2018/07/20 周五 13:01 360 32求分数序列和.cpp 2018/07/20 周五 13:01 324 33计算分数加减表达式的值.cpp 2018/07/20 周五 13:02 220 34求阶乘和.cpp 2018/07/20 周五 13:02 304 35求出e的值.cpp 2018/07/20 周五 13:02 302 36计算多项式的值.cpp 2018/07/20 周五 13:03 378 37雇佣兵.cpp 2018/07/20 周五 13:03 540 38计算多项式导函数.cpp 2018/07/20 周五 13:03 377 39与7无关的数.cpp 2018/07/20 周五 13:03 331 40数1的个数.cpp 2018/07/20 周五 13:04 485 41数字统计.cpp 2018/07/20 周五 13:04 704 42画矩形.cpp 2018/07/20 周五 13:04 341 43质因数分解.cpp 2018/07/20 周五 13:04 531 44第n小的质数.cpp 2018/07/20 周五 13:05 341 45金币.cpp

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值