题目链接:https://nanti.jisuanke.com/t/41399
题意:一排 N个初始关着的灯泡,M个操作,每个操作使得区间 [L,R] 的状态反转,求最后开着灯泡的数量。
思路:内存只有8192K,考虑差分,但不能直接遍历,考虑到每一个区间只有奇数才会有贡献,而 M 只有1000,直接遍历区间计算贡献即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
#define pb push_back
#define all(x) x.begin(), x.end()
#define bug prllf("*********\n")
#define debug(x) cerr<<#x<<" = "<<(x)<<endl
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int mod = 998244353;
const double eps = 1e-8;
const double pi = acos(-1);
template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}
const int N = 2e5 + 7;
int n, m;
map <int, int > mp;
int main()
{
int T, ca = 1; scanf("%d", &T);
while(T--)
{
scanf("%d%d",&n, &m);
while(m--)
{
int x, y; scanf("%d%d",&x, &y);
mp[x]++; mp[y+1]--;
}
int pre = 0, ans = 0, sum = 0;
for(auto x : mp)
{
if(sum & 1) ans += x.first - pre;
pre = x.first;
sum += x.second;
}
mp.clear();
printf("Case #%d: %d\n", ca++, ans);
}
}