码迷,mamicode.com
首页 > 其他好文 > 详细

B. Light bulbs(2019 ICPC上海站)

时间:2019-09-25 17:32:49      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:pre   targe   ips   include   its   pen   tar   bin   desc   

There are NN light bulbs indexed from 00 to N-1N1. 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 RLxR. 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 10001T1000

1 \leq N \leq 10^61N106

1 \leq M \leq 10001M1000

0 \leq L_i \leq R_i \leq N-10Li?Ri?N1

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;
}
思路:
有一点差分的思想,但是却进行了一定的改变

B. Light bulbs(2019 ICPC上海站)

标签:pre   targe   ips   include   its   pen   tar   bin   desc   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!