B. Light bulbs
B. Light bulbs
There are NNN light bulbs indexed from 000 to N−1N-1N−1. Initially, all of them are off.
A FLIP operation switches the state of a contiguous subset of bulbs. FLIP(L,R)FLIP(L, R)FLIP(L,R) means to flip all bulbs xxx such that L≤x≤RL \leq x \leq RL≤x≤R. So for example, FLIP(3,5)FLIP(3, 5)FLIP(3,5) means to flip bulbs 333 , 444 and 555, and FLIP(5,5)FLIP(5, 5)FLIP(5,5) means to flip bulb 555.
Given the value of NNN and a sequence of MMM flips, count the number of light bulbs that will be on at the end state.
InputFile
The first line of the input gives the number of test cases, TTT. TTT test cases follow. Each test case starts with a line containing two integers NNN and MMM, the number of light bulbs and the number of operations, respectively. Then, there are MMM more lines, the iii-th of which contains the two integers LiL_iLi and RiR_iRi, indicating that the iii-th operation would like to flip all the bulbs from LiL_iLi to RiR_iRi , inclusive.
1≤T≤10001 \leq T \leq 10001≤T≤1000
1≤N≤1061 \leq N \leq 10^61≤N≤106
1≤M≤10001 \leq M \leq 10001≤M≤1000
0≤Li≤Ri≤N−10 \leq L_i \leq R_i \leq N-10≤Li≤Ri≤N−1
OutputFile
For each test case, output one line containing Case #x: y, where xxx is the test case number (starting from 111) and yyy is the number of light bulbs that will be on at the end state, as described above.
样例输入
2
10 2
2 6
4 8
6 3
1 1
2 3
3 4
样例输出
Case #1: 4
Case #2: 3
题意:
N 个 灯泡(编号 0 ~ N-1) M 次操作(初始灯都是关的)
每次操作 给 2个数 L, R,把[L, R]区间内的开关翻转
求 M次操作后 有多少灯开着 题解:
把操作区间的左右位置le,ri离散化到一维数组b中,,记录每个区间被操作的次数,最后前缀和判断奇偶即可
//B. Light bulbs
#include<iostream>
#include<algorithm>
#include<string.h>
#define ll long long
using namespace std;
int a[],b[];//a是记录操作次数,b是记录区间离散化到数组中的位置
int main()
{
int t;
scanf("%d",&t);
for(int k=;k<=t;k++)
{
int n,m,cnt=;
scanf("%d%d",&n,&m);
memset(a,,sizeof(a));
for(int i=;i<=m;i++)
{
int le,ri;
scanf("%d%d",&le,&ri);
if(a[le]==)
b[cnt++]=le;
if(a[++ri]==)
b[cnt++]=ri;
a[le]++;
a[ri]++;
}
sort(b,b+cnt);
int ans=;
ll num=a[b[]];
for(int i=;i<cnt;i++)
{
if(num%==)
ans=ans+b[i]-b[i-];//区间长度就是亮灯个数
num=num+a[b[i]];//求区间[0,b[i]]所有灯泡操作次数
}
printf("Case #%d: %d\n",k,ans); }
return ;
}
B. Light bulbs的更多相关文章
- zoj 2976 Light Bulbs(暴力枚举)
Light Bulbs Time Limit: 2 Seconds Memory Limit: 65536 KB Wildleopard had fallen in love with hi ...
- 哈理工2015 暑假训练赛 zoj 2976 Light Bulbs
MS Memory Limit:65536KB 64bit IO Format:%lld & %llu SubmitStatusid=14946">Practice ...
- 2019 ACM-ICPC 上海网络赛 B. Light bulbs (差分)
题目链接:Light bulbs 比赛链接:The Preliminary Contest for ICPC Asia Shanghai 2019 题意 给定 \(N\) 个灯泡 (编号从 \(0\) ...
- B. Light bulbs(2019 ICPC上海站)
There are NN light bulbs indexed from 00 to N-1N−1. Initially, all of them are off. A FLIP operation ...
- The Preliminary Contest for ICPC Asia Shanghai 2019 B. Light bulbs
题目:https://nanti.jisuanke.com/t/41399 思路:差分数组 区间内操作次数为奇数次则灯为打开状态 #include<bits/stdc++.h> using ...
- The Preliminary Contest for ICPC Asia Shanghai 2019 B Light bulbs (离散的差分)
复杂度分析,询问一千次,区间长1e6,O(1e9)超时. 那么我们知道对于差分来说,没必要一个一个求,只需要知道区间长就可以了,所以我们定义结构体差分节点,一个头结点,一个尾节点. 这样tail.lo ...
- 2019年icpc上海网络赛 B Light bulbs (分块、差分)
https://nanti.jisuanke.com/t/41399 题目大意: 有n个灯,m次操作,每次修改[l,r]内的灯,(off - on ,on - off),问最后有几盏灯亮着. 换种说法 ...
- nRF5 SDK for Mesh(八) Exploring Mesh APIs using light switch example,使用 灯开关 案例探索BLE mesh 的APIS
Exploring Mesh APIs using light switch example The light switch example is meant to showcase the API ...
- Unity Lighting - Light Types 灯光类型(八)
Light Types 灯光类型 We have now covered some of the project settings which need to be considered befo ...
随机推荐
- iOS 实现 摇一摇
摇一摇功能:调用了系统自带加速器,当设备摇动时,系统会 计算出加速器的加速值,然后告诉设备是否发生摇动手势,系统只会运动开始和结束时通知你,并不会在运动发生的整个过程中始终向你报告每一次运动.例如,你 ...
- UE4高级运动系统(Advanced Locomotion System V3)插件分析
Advanced Locomotion System V3是虚幻商城的一款第三方插件.它相比UE4的基础走跑跳表现,实现了更多动作游戏里常用的运动特性,虽然价格定价不菲,依然备受关注.笔者试用了这款插 ...
- python中os.path.abspath与os.path.realpath 区别
python中os.path.abspath与os.path.realpath 区别cd /homemkdir amkdir btouch a/1.txtln -s /home/a/1.txt /ho ...
- python splash scrapy
python splash scrapy 1. 前言 slpash是一个渲染引擎,它有自己的api,可以直接访问splash服务的http接口,但也有对应的包python-splash方便调 ...
- 【剑指Offer面试编程题】题目1508:把字符串转换成整数--九度OJ
题目描述: 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 输入: 输入可能包含多个测试样例. 对于每个测试案例,输入为一个合法或者非法的字符串,代表一个整数n(1<= n&l ...
- Python数据分析之Pandas操作大全
从头到尾都是手码的,文中的所有示例也都是在Pycharm中运行过的,自己整理笔记的最大好处在于可以按照自己的思路来构建矿建,等到将来在需要的时候能够以最快的速度看懂并应用=_= 注:为方便表述,本章设 ...
- 刚下载好的 vscode 不能运行,一片黑 以及终端不能输入 解决办法
1.鼠标右键vscode快捷方式点击属性,选择兼容性,勾选以兼容模式运行,下拉列表调整为windows vista (service pack 1)即可解决. 2.如果打开终端不能输入命令,首先点击属 ...
- 「CF197B Limit」
题目撞名 题目大意: 给出两个函数 \(P(x),Q(x)\). \(P(x)=a_0 \times x^N+a_1 \times x^{N-1}+a_2 \times x^{N-2} \cdots ...
- python列表操作方法详解
列表 列表是Python中最基本的数据结构,列表是最常用的Python数据类型,列表是一个数据的集合,集合内可以放任何数据类型,可对集合方便的增删改查操作.Python已经内置确定序列的长度以及确 ...
- 「AHOI2014/JSOI2014」奇怪的计算器
「AHOI2014/JSOI2014」奇怪的计算器 传送门 我拿到这题首先是懵b的,因为感觉没有任何性质... 后来经过同机房dalao的指导发现可以把所有的 \(X\) 放到一起排序,然后我们可以发 ...