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(1≤T≤1000),代表有 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(1≤n≤1e6),m(1≤m≤1000),代表有 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
#include
#include
#include
#include
#include
#include