zoukankan      html  css  js  c++  java
  • 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
    ",(x))
    #define yes printf("YES
    ");
    #define no printf("NO
    ");
    #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;
    }
    所遇皆星河
  • 相关阅读:
    iOS7 iOS8 毛玻璃效果的分别实现
    关于app transfer之后的开发
    微信登录后,请求用户信息时, 返回地址信息是拼音的解决方案
    获得设备型号(含iPhone6 , iPhone 6+)
    iOS 开发者计划申请 2014 年最新心得[转]
    Facebook的Pop动画库相关资料
    iOS添加自定义字体方法
    iOS8新增加的frameworks, 在目前基于7以上开发的情况下, 使用下列sdk要注意设置成optional
    iOS开发 .framework的Optional(弱引用)和Required(强引用)区别, 有错误 Library not found………………
    python接口自动化测试二:常用操作
  • 原文地址:https://www.cnblogs.com/Shallow-dream/p/11530610.html
Copyright © 2011-2022 走看看