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

代码:#include<bits/stdc++.h>using namespace std;const int maxn = 1e6+20;int num[maxn*2];int main(){    int t;    cin>>t;    for(int i=1;i<=t;i++){        int n,m;        scanf("%d%d",&n,&m);        int count = 0;        for(int j = 0; j<m;j++){            int x,y;            scanf("%d%d",&x,&y);            num[count++] = x;            num[count++] = y+1;        }        sort(num,num+m*2);        int sum = 0;        for(int i = 0;i <count;i+=2){            sum +=num[i+1] - num[i];        }        printf("Case #%d: %d\n",i,sum);        }    return 0;}思路:有一点差分的思想,但是却进行了一定的改变

原文地址:https://www.cnblogs.com/lusiqi/p/11586100.html

时间: 2024-10-06 08:13:14

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

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

题目链接:Light bulbs 比赛链接:The Preliminary Contest for ICPC Asia Shanghai 2019 题意 给定 \(N\) 个灯泡 (编号从 \(0\) 到 \(N - 1\)),初始都是关闭的. 给定 \(M\) 个操作,每个操作包含 \(L\) 和 \(R\),对 \([L, R]\) 内的所有灯泡改变状态. 求最后有几个灯泡是亮的. 思路 题目挺简单的,翻转奇数次的灯泡是亮的,所以要求每个灯泡翻转的次数. 容易想到可以用差分. 对所有操作的两

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

Light BulbsTime Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu SubmitStatusPracticeZOJ 2976 Description Wildleopard had fallen in love with his girlfriend for 20 years. He wanted to end the long match for their love and get marri

zoj 2976 Light Bulbs(暴力枚举)

Light Bulbs Time Limit: 2 Seconds      Memory Limit: 65536 KB Wildleopard had fallen in love with his girlfriend for 20 years. He wanted to end the long match for their love and get married this year. He bought a new house for his family and hired a

2019 ICPC Malaysia National G(拓扑排序)

2019 ICPC Malaysia National G 有点绕,两层拓扑排序. 有空再补详细. 甚至有点丑,因为绕,为了区分,当时变量名写得很长. #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #include<vector> #define debug printf("!") using namespace std; type

2019 ICPC Malaysia National F(状态压缩)

2019 ICPC Malaysia National F 赛后补题.看了这个题解,说是状态压缩. 以第一行的士兵为主,第二行士兵为次,即,第二行被第一行士兵匹配,更新第一行士兵的状态. 用当前第i个士兵的状态更新第i+1个士兵的状态. f[i][j]:i为士兵的下标,j为第i个士兵的状态.(1<j<(1<<(e*2+1))). 比如e=3,二进制 j=1000011,表示第i个士兵之前包括第i个士兵,在[i-3,i+3]范围内,第二行的士兵已被匹配了下标为i-3,i+2,i+3的

2019 ICPC 南昌网络赛

2019 ICPC 南昌网络赛 比赛时间:2019.9.8 比赛链接:The 2019 Asia Nanchang First Round Online Programming Contest 总结 // 史上排名最高一次,开场不到两小时队友各A一题加水题共四题,排名瞬间升至三四十名 // 然后后三小时就自闭了,一题都没有突破...最后排名211 hhhh ? ? B. Fire-Fighting Hero 题意 队友做的,待补. ? AC代码 #include<cstdio> #includ

Light bulbs (树状数组模板题)

There are N light bulbs indexed from 00 to N−1. Initially, all of them are off. A FLIP operation switches the state of a contiguous subset of bulbs. FLIP(L, R)means to flip all bulbs x such that L≤x≤R. So for example, FLIP(3, 5) means to flip bulbs 3

【上海网络赛】B.Light bulbs

题目描述 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 exampl

日常补题——ICPC网络赛上海站第二题B Light bulbs

题目链接: https://nanti.jisuanke.com/t/41399 博客借鉴: https://blog.csdn.net/weixin_43701790/article/details/100867368 题目大意: 每次操作使 L,R区间内的等的状态改变,最后一共有多少开着的灯 题解: 这道题的时间限制是1000ms,内存限制是8192K 可以说是很不常见的卡内存的题 一开始我们用的是线段树,直接超内存 然后想着用short int 代替int,也还是不行 然后想着如果不用la