INPUT
2
10 2
2 6
4 8
6 3
1 1
2 3
3 4
OUTPUT
Case #1: 4
Case #2: 3
题意:有n盏灯,初始都是灭的状态,p次操作,每次操作翻转a到b盏灯的状态,问最终操作完成后几盏灯是亮着的。
思路:内存限制8192K,显然不能用线段树,只能用数组操作,但是也不能直接遍历1e6的数组,所以我们用map标记头和尾,最后只遍历所存的点就好,将头每次加1,尾后面的点每次减1,对于每次操作,只有奇数次才能操作。具体看代码。
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include<map>
using namespace std;
#define MAX 1e9+7
#define inf 0x3f3f3f
//const int mm=1010;
const int M=1e6+10;
typedef long long ll;
map<int,int>mp;
map<int,int>mp1;
int main()
{
int t,cnt=1;
scanf("%d",&t);
for(int i=1;i<=t;i++){
int n,m,ans=0;
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++){
int a,b;
scanf("%d %d",&a,&b);
mp[a]++,mp[b+1]--;
//记录b+1这个点就不用考虑a==b的情况了
}
int temp=0,p=0,q=0;
map<int,int>::iterator it;
for(it = mp.begin();it!=mp.end();it++){
if(temp%2!=0){//奇数次就进行
ans+=it->first-p;//减上一次的点
}
p=it->first;
temp+=it->second;
}
mp.clear();
printf("Case #%d: %d\n",cnt++,ans);
}
return 0;
}