Light bulbs【差分】

19.98%
1000ms
8192K
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 ( 1 ≤ T ≤ 1000 ) T(1\le T\le 1000) T(1T1000),代表有 T T T组测试数据,对于每组测试数据,先输入两个整数 n ( 1 ≤ n ≤ 1 e 6 ) , m ( 1 ≤ m ≤ 1000 ) n(1 \le n \le 1e6),m(1\le m \le1000) n(1n1e6),m(1m1000),代表有 n n n个灯泡,初始状态全为灭, m m m次操作,下面 m m m行每行输入两个整数 l , r l,r l,r,代表将从第 l l l个灯泡到第 r r r个灯泡的状态全部反转,即开着的灯泡灭掉,灭掉的灯泡打开,问经过 m m m次操作后最终有多少灯泡是打开的。

解题思路:
如果是普通差分然后暴力跑一遍的话时间复杂度为 1 e 9 1e9 1e9,会超时。
所以此题可以将每次操作的两个端点存起来,按端点从小到大排好序,对于每次的 l l l,可以认为后面的操作均加 1 1 1,对于 r r r,可认为均减 1 1 1,因此这样就可以把区间拆分成连续不相交的短区间。

代码:

//#pragma GCC optimize(3,"Ofast","inline")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
struct node
{
    int id;
    int f;
}arr[2100];
int cnt=0;
bool cmp(node a,node b) {
    return a.id<b.id;
}
int main() 
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif
    //ios::sync_with_stdio(0),cin.tie(0);
    int T;
    scanf("%d",&T);
    rep(t,1,T) {
        int n,m;
        int l,r;
        scanf("%d %d",&n,&m);
        cnt=0;
        rep(i,1,m) {
            scanf("%d %d",&l,&r);
            arr[++cnt].id=l;
            arr[cnt].f=1;
            arr[++cnt].id=r+1;
            arr[cnt].f=0;
        }
        sort(arr+1,arr+1+cnt,cmp);
        int ans=0;
        int nape=0;
        for(int i=1;i<cnt;i++) {
            if(arr[i].f==1) nape++;
            else nape--;
            if(nape&1) {
                ans+=(arr[i+1].id-arr[i].id);
            }
        }
        printf("Case #%d: %d\n",t,ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值