The Preliminary Contest for ICPC Asia Shanghai 2019

时间:2019-09-15
本文章向大家介绍The Preliminary Contest for ICPC Asia Shanghai 2019,主要包括The Preliminary Contest for ICPC Asia Shanghai 2019使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

The Preliminary Contest for ICPC Asia Shanghai 2019

Light bulbs

题意:有n盏灯,初始都是灭的状态,p次操作,每次操作翻转a到b盏灯的状态,问最终操作完成后几盏灯是亮着的。

思路:内存限制8192K,显然不能用线段树,只能用数组操作,但是也不能直接遍历1e6的数组,所以我们用map标记头和尾,最后只遍历所存的点就好,将头每次加1,尾后面的点每次减1,对于每次操作,只有奇数次才能操作。具体看代码。

#include <bits/stdc++.h>
using namespace std;

map<int,int>mp;
map<int,int>::iterator it;
int n,m;
int main() {
    int _,ca=0;
    scanf("%d", &_);
    while (_--) {
        scanf("%d%d", &n, &m);
        for (int i = 1, a, b; i <= m; i++) {
            scanf("%d%d", &a, &b);
            mp[a]++;
            mp[b + 1]--;
        }
        int ans=0,sum = 0,last=0;
        for (it = mp.begin(); it != mp.end(); it++) {
            if (sum % 2 == 1) {
                ans += it->first - last;
            }
            last = it->first;
            sum += it->second;
        }
        mp.clear();
        printf("Case #%d: %d\n",++ca,ans);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/Accpted/p/11523674.html