B Light bulbs (离散化+暴力)(The Preliminary Contest for ICPC Asia Shanghai 2019)

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 MM 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, 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 MM more 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-10LiRiN1

OutputFile

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 11) and yyis 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 <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
#define lli long long 
#define pq priority_queue<int>
#define pql priority_queue<ll>
#define pqn priority_queue<node>
#define v vector<int>
#define vl vector<ll> 
#define read(x) scanf("%d",&x)
#define read2(x,y) scanf("%d %d",&x,&y);
#define lread(x) scanf("%lld",&x);
#define pt(x) printf("%d\n",(x))
#define yes printf("YES\n");
#define no printf("NO\n");
#define gcd __gcd
#define out(x) cout<<x<<endl;
#define rep(j,k) for (int i = (int)(j); i <= (int)(k); i++)
#define input(k) for (int i = 1; i <= (int)(k); i++)  {scanf("%d",&a[i]) ; }
#define mem(s,t) memset(s,t,sizeof(s))
#define ok return 0;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define test cout<<"     ++++++      "<<endl;
//二叉树 
#define lson rt<<1, l, m
#define rson rt<<1|1, m+1, r
//线段树
#define ls now<<1
#define rs now<<1|1 
const int inf = 1e6+5;

int dp[inf];
int main()
{
    TLE;
    int n,m,k,cnt,l,r,t,ans;
    cin>>t;
    for(int kk=1;kk<=t;kk++)
    {
        cin>>n>>m;
        cnt=0;ans=0;
        for(int i=0;i<m;i++)
        {
            cin>>l>>r;
            dp[cnt++]=l;
            dp[cnt++]=r+1;
        }
        sort(dp,dp+2*m);
        for(int i=1;i<=cnt;i+=2)        //这里是两个单位长度,右端点 - 左端点 = 区间长度
            ans+=dp[i]-dp[i-1];
        cout<<"Case #"<<kk<<": "<<ans<<endl;
    }
    ok;
}

 

posted @ 2019-09-16 22:07  __MEET  阅读(167)  评论(0编辑  收藏  举报