洛谷P2826[USACO08NOV]光开关Light Switching

题目描述

Farmer John tries to keep the cows sharp by letting them play with intellectual toys. One of the larger toys is the lights in the barn. Each of the N (2 <= N <= 100,000) cow stalls conveniently numbered 1..N has a colorful light above it.

At the beginning of the evening, all the lights are off. The cows control the lights with a set of N pushbutton switches that toggle the lights; pushing switch i changes the state of light i from off to on or from on to off.

The cows read and execute a list of M (1 <= M <= 100,000) operations expressed as one of two integers (0 <= operation <= 1).

The first kind of operation (denoted by a 0 command) includes two subsequent integers S_i and E_i (1 <= S_i <= E_i <= N) that indicate a starting switch and ending switch. They execute the operation by pushing each pushbutton from S_i through E_i inclusive exactly once.

The second kind of operation (denoted by a 1 command) asks the cows to count how many lights are on in the range given by two integers S_i and E_i (1 <= S_i <= E_i <= N) which specify the inclusive range in which the cows should count the number of lights that are on.

Help FJ ensure the cows are getting the correct answer by processing the list and producing the proper counts.

灯是由高科技——外星人鼠标操控的。你只要左击两个灯所连的鼠标,

这两个灯,以及之间的灯都会由暗变亮,或由亮变暗。右击两个灯所连的鼠

标,你就可以知道这两个灯,以及之间的灯有多少灯是亮的。起初所有灯都是暗的,你的任务是在LZ之前算出灯的亮灭。

输入输出格式

输入格式:

第1 行: 用空格隔开的两个整数N 和M,n 是灯数

第2..M+1 行: 每行表示一个操作, 有三个用空格分开的整数: 指令号, S_i 和E_i

第1 种指令(用0 表示)包含两个数字S_i 和E_i (1 <= S_i <= E_i <= N), 它们表示起

始开关和终止开关. 表示左击

第2 种指令(用1 表示)同样包含两个数字S_i 和E_i (1 <= S_i <= E_i <= N), 不过这

种指令是询问从S_i 到E_i 之间的灯有多少是亮着的.

输出格式:

输入输出样例

输入样例#1:
4 5
0 1 2
0 2 4
1 2 3
0 2 4
1 1 4
输出样例#1:
1
2

说明

线段树题。。。

附代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#define LSON rt<<1//左孩子
#define RSON rt<<1|1//右孩子
#define DATA(x) a[x].data//节点值
#define SIGN(x) a[x].c//懒惰标记
#define LSIDE(x) a[x].l
#define RSIDE(x) a[x].r//左右区间
#define WIDTH(x) (RSIDE(x)-LSIDE(x)+1)//区间范围
#define MAXN 100010
using namespace std;
int n,m;
struct node{
    int data,c;
    int l,r;
}a[MAXN<<2];//线段树 数组,切记,一定要开 4 倍空间,不然会炸。。。
inline int read(){//弱弱的读入优化。。。
    int date=0,w=1;char c=0;
    while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
    while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
    return date*w;
}
void pushup(int rt){//数值上传
    DATA(rt)=DATA(LSON)+DATA(RSON);//每个节点(叶节点除外)的值 = 左孩子的值 + 右孩子旳值
}
void pushdown(int rt){//标记下传
    if(!SIGN(rt)||LSIDE(rt)==RSIDE(rt))
    return;
    SIGN(LSON)^=SIGN(rt);处理左孩子。取反,从1变为0 或 从0变为1
    DATA(LSON)=WIDTH(LSON)-DATA(LSON);//区间范围 = 区间内0的个数 + 区间内1的个数,移项即可。。。
    SIGN(RSON)^=SIGN(rt);//处理右孩子
    DATA(RSON)=WIDTH(RSON)-DATA(RSON);
    SIGN(rt)=0;//标记清空
}
void buildtree(int l,int r,int rt){//建树
    int mid;
    LSIDE(rt)=l;
    RSIDE(rt)=r;//这两句是建树的核心之一
    if(l==r){
        DATA(rt)=0;
        return;
    }
    mid=l+r>>1;
    buildtree(l,mid,LSON);
    buildtree(mid+1,r,RSON);//这是核心之二
    pushup(rt);
}
void update(int l,int r,int rt){//修改
    int mid;
    if(l<=LSIDE(rt)&&RSIDE(rt)<=r){//如果该节点在范围内,则 处理 并 返回 
        SIGN(rt)^=1;
        DATA(rt)=WIDTH(rt)-DATA(rt);//已解释过。。。见 标记下传
        return;
    }
    pushdown(rt);//一定要先下传,再递归
    mid=LSIDE(rt)+RSIDE(rt)>>1;//分为 左孩子 与 右孩子,分别处理
    if(l<=mid)update(l,r,LSON);
    if(mid<r)update(l,r,RSON);
    pushup(rt);//一定要上传
}
long long query(int l,int r,int rt){
    int mid;
    long long ans=0;
    if(l<=LSIDE(rt)&&RSIDE(rt)<=r)//如果该节点在范围内
    return DATA(rt);//返回节点值
    pushdown(rt);//下传标记
    mid=LSIDE(rt)+RSIDE(rt)>>1;//分为 左孩子 与 右孩子,分别求和
    if(l<=mid)ans+=query(l,r,LSON);
    if(mid<r)ans+=query(l,r,RSON);
    return ans;//不要忘记返回值。。。
}
int main(){
    int f,x,y;
    n=read();m=read();
    buildtree(1,n,1);
    while(m--){
        f=read();x=read();y=read();
        if(f==0)update(x,y,1);//分类
        if(f==1)printf("%lld\n",query(x,y,1));
    }
    return 0;//终于结束。。。
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值