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的更多相关文章

  1. zoj 2976 Light Bulbs(暴力枚举)

    Light Bulbs Time Limit: 2 Seconds      Memory Limit: 65536 KB Wildleopard had fallen in love with hi ...

  2. 哈理工2015 暑假训练赛 zoj 2976 Light Bulbs

    MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu SubmitStatusid=14946">Practice ...

  3. 2019 ACM-ICPC 上海网络赛 B. Light bulbs (差分)

    题目链接:Light bulbs 比赛链接:The Preliminary Contest for ICPC Asia Shanghai 2019 题意 给定 \(N\) 个灯泡 (编号从 \(0\) ...

  4. 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 ...

  5. The Preliminary Contest for ICPC Asia Shanghai 2019 B. Light bulbs

    题目:https://nanti.jisuanke.com/t/41399 思路:差分数组 区间内操作次数为奇数次则灯为打开状态 #include<bits/stdc++.h> using ...

  6. The Preliminary Contest for ICPC Asia Shanghai 2019 B Light bulbs (离散的差分)

    复杂度分析,询问一千次,区间长1e6,O(1e9)超时. 那么我们知道对于差分来说,没必要一个一个求,只需要知道区间长就可以了,所以我们定义结构体差分节点,一个头结点,一个尾节点. 这样tail.lo ...

  7. 2019年icpc上海网络赛 B Light bulbs (分块、差分)

    https://nanti.jisuanke.com/t/41399 题目大意: 有n个灯,m次操作,每次修改[l,r]内的灯,(off - on ,on - off),问最后有几盏灯亮着. 换种说法 ...

  8. 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 ...

  9. Unity Lighting - Light Types 灯光类型(八)

      Light Types 灯光类型 We have now covered some of the project settings which need to be considered befo ...

随机推荐

  1. 「JSOI2015」字符串树

    「JSOI2015」字符串树 传送门 显然可以树上差分. 我们对于树上每一条从根出发的路径都开一 棵 \(\text{Trie}\) 树,那么我们就只需要在 \(\text{Trie}\) 树中插入一 ...

  2. 安卓开发:初步了解布局文件layout

    了解完项目的目录结构,主要文件的作用之后. 了解完各常量文件的定义和使用之后,接下来的重头戏肯定是布局文件layout. 果然,网上关于“安卓布局文件layout”的各种介绍.解析.深入分析,等等资料 ...

  3. 最全的Java操作Redis的工具类,使用StringRedisTemplate实现,封装了对Redis五种基本类型的各种操作!

    转载自:https://github.com/whvcse/RedisUtil 代码 ProtoStuffSerializerUtil.java import java.io.ByteArrayInp ...

  4. 吴裕雄--天生自然Numpy库学习笔记:NumPy 数学函数

    NumPy 包含大量的各种数学运算的函数,包括三角函数,算术运算的函数,复数处理函数等. NumPy 提供了标准的三角函数:sin().cos().tan(). import numpy as np ...

  5. Java Https双向验证

    CA: Certificate Authority,证书颁发机构 CA证书:证书颁发机构颁发的数字证书 参考资料 CA证书和TLS介绍 HTTPS原理和CA证书申请(满满的干货) 单向 / 双向认证 ...

  6. iOS 10.3 以上系统实现应用内评分及开发者回复评论

    在 iOS 10.3 之前,如果你要给一个应用评分,那么你需要打开 App Store,搜索应用,找到评论,点击撰写评论,然后评分.整个评分流程非常繁琐,还要忍受漫长的页面加载,导致很少有用户愿意主动 ...

  7. 移动端 safari苹果手机对大额数字自动变成电话号码

    1.苹果手机safari浏览器,用<meta name="format-detection" content="telephone=no">解决.缺 ...

  8. 【摘录自MDN】客户端和服务器

    客户端和服务器 连接到互联网的计算机被称作客户端和服务器.下面是一个简单描述它们如何交互的图表: 客户端是典型的Web用户入网设备(比如,你连接了Wi-Fi的电脑,或接入移动网络的手机)和设备上可联网 ...

  9. idea选择主题

    主题下载地址1:http://color-themes.com/?view=index 主题下载地址2:http://www.themesmap.com/ 主题下载地址3:http://www.ria ...

  10. Mysql基本用法-left join、right join、 inner join、子查询和join-02

    left join #左连接又叫外连接 left join 返回左表中所有记录和右表中连接字段相等的记录  test_user表 phpcvs表 SQL: select * from test_use ...