There are NN light bulbs indexed from 00 to N-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)means to flip all bulbs xx such that L \leq x \leq RL≤x≤R. So for example, FLIP(3, 5)FLIP(3,5) means to flip bulbs 33 , 44 and 55, and FLIP(5, 5)FLIP(5,5) means to flip bulb 55.

Given the value of NN and a sequence of MMflips, 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, TT. TT test cases follow. Each test case starts with a line containing two integers NN and MM, the number of light bulbs and the number of operations, respectively. Then, there are MMmore lines, the ii-th of which contains the two integers L_iLi​ and R_iRi​, indicating that the ii-th operation would like to flip all the bulbs from L_iLi​ to R_iRi​ , inclusive.

1 \leq T \leq 10001≤T≤1000

1 \leq N \leq 10^61≤N≤106

1 \leq M \leq 10001≤M≤1000

0 \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 xx is the test case number (starting from 11) and yy 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 题意:
有T组样例,依次输入每组样例,第一行为n,表示有n个灯泡,然后m次操作,最后输出经过m次操作后亮着的灯泡有多少个? 思路:
首先,我们会想就操作区间的进行加1,最后n个数,依次代表n个灯泡操作的次数,次数%2==0,就是关着的,次数%2==1,就是开着的。
就拿第一个样例来说:
第一行:开始状态 第二行:第一次操作后 第三行:第二次操作后

m次操作,每次是将区间的所有值都加一。
这里不是不有点前缀和的味道,给定 n 个数和 m 次询问,每次询问一段区间的和,一个 O(n + m) 的做法。
 for(int i = ; i <= n; ++i) sum[i] = sum[i - ] + a[i];  //O(n)
while(m--)        //O(m)
{
int L, R; scanf("%d%d", &L, &R);
printf("%d\n", sum[R] - sum[L - ]);
}

我们先简单的引入有关差分的概念。

设a数组表示原始的数组;

设d[i]=a[i]-a[i-1](1<i≤n,d[1]=a[1]);

设f[i]=f[i-1]+d[i](1<i≤n,f[1]=d[1]=a[1]);

设sum[i]=sum[i-1]+f[i](1<i≤n,sum[1]=f[1]=d[1]=a[1])。

这道题我们完全用差分
为了不出现负数的情况,我们可以初始化每个数都为1000(一千次操作),然后对区间操作的时候[l,r],将a[l]+1,a[r+1]-1,
然后我们直接进行求和
for(i = 1;i<=n;i++)
b[i] = b[i-1]+a[i];
b[i]%2==0灯就是关着的,b[i]%2==1灯就是开着的
但是还是不行,为什么呢?我们分析一下,每个样例我们都要对数组进行初始化1e6,然后我们对区间的端点进行操作m,然后最后来个类似于前缀和的操作1e6
所有大约1e3*2e6=2e9,两秒,题目要求一秒,超时,然后我们想到用空间换时间,这也是我之前想过的,不用想,直接爆,我太难了!!!
因此我们要再想一想如何优化,可以满足要求
既然我们是直接对区间的端点进行操作,那么我们想一想能不能把1e6转为1e3,这样1e3*1e3=1e6,不会超
显然是可以的。
这里是%2==1/0进行判断灯是否亮,前面差分我们是+1 ,-1(把对后面的影响抵消掉),但是对这一道题我们可以直接+1,+1,两次加1作用就抵消了,并且操作相同。
经过m次操作就直接输出结果,那么哪个操作在前,哪个操作在后其实就不重要了
我们记录端点(l,r+1),然后所有操作完后进行排序,从小到大,我们两两配对,从前往后,
2 4 7 9,
4-2 2相当于从第二个灯泡影响到最后的灯泡,4就把从第四个灯泡开始的影响给消除了,然后这一对操作其实影响的就只有2个灯泡,7-8类似
4-2+9-7=4. 代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+;
int num[maxn*];
int main(){
int t;
cin>>t;
for(int i=;i<=t;i++){
int n,m;
scanf("%d%d",&n,&m);
int count = ;
for(int j = ; j<m;j++){
int x,y;
scanf("%d%d",&x,&y);
num[count++] = x;
num[count++] = y+;
}
sort(num,num+m*);
int sum = ;
for(int i = ;i <count;i+=){
sum +=num[i+] - num[i];
}
printf("Case #%d: %d\n",i,sum);
}
return ;
}

B. Light bulbs(2019 ICPC上海站)的更多相关文章

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

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

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

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

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

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

  4. 2019 ICPC 南昌网络赛

    2019 ICPC 南昌网络赛 比赛时间:2019.9.8 比赛链接:The 2019 Asia Nanchang First Round Online Programming Contest 总结 ...

  5. B. Light bulbs

    B. Light bulbs There are NNN light bulbs indexed from 000 to N−1N-1N−1. Initially, all of them are o ...

  6. 2019 ICPC Asia Nanjing Regional

    2019 ICPC Asia Nanjing Regional A - Hard Problem 计蒜客 - 42395 若 n = 10,可以先取:6,7,8,9,10.然后随便从1,2,3,4,5 ...

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

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

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

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

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

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

随机推荐

  1. 2019牛客多校第六场J-Upgrading Technology(枚举+单调队列)

    Upgrading Technology 题目传送门 解题思路 对于这题,我们可以枚举一个k从0~m,表示当前我们把所有技能最少升到了k级,且至少有一个为k级. 此时我们刚好获得了前k个d[]的收益, ...

  2. C/C++ 吐槽第一期:你最讨厌的C/C++里面的数据类型是什么

    C/C++ 这里面讨论的范围包括从以往开始,到现有的所有官方标准,VC扩展,GCC扩展, C语言部分包括C89.C90.C99.C11这些知名的大版本,中间或者之前的比如K&R这种不出名的小版 ...

  3. 【Luogu】【关卡2-4】排序Ex(2017年10月)

    任务说明:这里的排序就更上一层了.不仅融合了别的算法与技巧,排序本身也有各种花招.

  4. poj 2752 kmp的next数组

    题目大意: 求一个字符串中某一个既是前缀又是后缀的前缀的结尾下标: 基本思路: 从_next[len]开始找_next[_next[len]],再找_next[_next[_next[len]]],一 ...

  5. github fork代码后提交

    点击他人github上的fork 在自己的Github上将代码拷贝下来 git clone 在本地修改代码后创建分支 git checkout -b work master(work为新建的特性分支, ...

  6. Laravel5.5添加新路由文件并制定规则

    Laravel5.5里面有4个默认的路由文件,其中web.php是默认路由文件,如果需要添加其他路由文件,按照以下步骤进行. 此处以添加网站home前端路由举例,我已经先在/app/Http/Cont ...

  7. Mysql学习笔记(004)- 条件查询

    条件查询 #进阶2:条件查询 /* 语法: select 查询列表③ from 表名① where 条件筛选② 分类: 一.按条件表达式筛选 条件运算符:> < = != <> ...

  8. shell脚本每五分钟执行一次可执行程序(nohup)

    两种解决方案:个人推荐第二种,使用crontab来定时执行任务   1.shell代码如下: nohup command &. 解释: 后台永久运行command命令. (nohup表示后台永 ...

  9. java 获取的是本地的IP地址

    1 public static void main(String[] args) { 2 try { 3 InetAddress address = InetAddress.getLocalHost( ...

  10. Yii2中一些常用的参数

    系统类型: <?= php_uname() ?> 解译引擎: <?= $_SERVER['SERVER_SOFTWARE'] ?>, Zend: <?= Zend_Ver ...