2019 ACM-ICPC 上海网络赛 B. Light bulbs (差分)

题目链接:Light bulbs
比赛链接:The Preliminary Contest for ICPC Asia Shanghai 2019

题意

给定 \(N\) 个灯泡 (编号从 \(0\) 到 \(N - 1\)),初始都是关闭的。

给定 \(M\) 个操作,每个操作包含 \(L\) 和 \(R\),对 \([L, R]\) 内的所有灯泡改变状态。

求最后有几个灯泡是亮的。

思路

题目挺简单的,翻转奇数次的灯泡是亮的,所以要求每个灯泡翻转的次数。

容易想到可以用差分。

对所有操作的两个端点排序,求差分数组 \(d[]\)。

然后根据差分数组求前缀和,差分数组相邻两个数 \(d[l]\) 和 \(d[r]\) 所在的区间 \([l, r)\) 内的每个数都加上 \(d[l]\),那么如果 \(d[l]\) 为奇数,\(ans += (r - l)\)。时间复杂度 \(O(MlogM)\)。

于是就有了下面的代码。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 10;

int pos[2010];

int main() {
    int T;
    scanf("%d", &T);
    int kase = 0;
    while(T--) {
        int n, m;
        scanf("%d%d", &n, &m);
        vector<int> d(n + 10);
        memset(pos, 0, sizeof(pos));
        unordered_map<int, int> mp;
        int cnt = 0;
        for(int i = 1; i <= m; ++i) {
            int l, r;
            scanf("%d%d", &l, &r);
            ++d[l];
            --d[r + 1];
            if(mp[l] == 0) {
                pos[cnt++] = l;
                mp[l] = 1;
            }
            if(mp[r + 1] == 0) {
                pos[cnt++] = r + 1;
                mp[r + 1] = 1;
            }
        }
        sort(pos, pos + cnt);
        ll ans = 0;
        for(int i = 1; i < cnt; ++i) {
            if(d[pos[i - 1]] & 1) ans += pos[i] - pos[i - 1];
            d[pos[i]] = d[pos[i - 1]] + d[pos[i]];
        }
        if(d[pos[cnt - 1]] & 1) ans += n - pos[cnt - 1];
        printf("Case #%d: %lld\n", ++kase, ans);
    }
    return 0;
}

然后就 TLE 了。这题时间和空间卡的很紧。

AC 代码

用 map 存差分数组,还自动排序了。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 10;

int main() {
    int T;
    scanf("%d", &T);
    int kase = 0;
    while(T--) {
        int n, m;
        scanf("%d%d", &n, &m);
        map<int, int> d;
        int cnt = 0;
        for(int i = 1; i <= m; ++i) {
            int l, r;
            scanf("%d%d", &l, &r);
            ++d[l];
            --d[r + 1];
        }
        ll ans = 0;
        auto it = d.begin();
        int p = it->first;
        int v = it->second;
        ++it;
        for(; it != d.end(); ++it) {
            if(v & 1) ans += it->first - p;
            it->second = v + it->second;
            p = it->first;
            v = it->second;
        }
        if(v & 1) ans += n - p;
        printf("Case #%d: %lld\n", ++kase, ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/wulitaotao/p/11524929.html

时间: 2024-08-30 12:21:37

2019 ACM-ICPC 上海网络赛 B. Light bulbs (差分)的相关文章

2019上海icpc网络赛B. Light bulbs(思维+差分)

题目传送门 题意 T组案例,每组案例:n个灯泡(from 0 to n-1),m次操作,每次操作把区间[L,R]内的灯泡翻转(开变关,关变开),问m次操作之后有多少灯泡是亮着的.(时间限制:1000ms  内存限制:8192K) 题解 这道题不仅卡时间,更是卡内存,所以用线段树会爆内存 正解: 该题可以转换为经典的差分问题:每次操作对[L,R]的所有数进行+1操作,求最后有多少个奇数.(设该数组为a[n],每次操作a[L]+1,a[R+1]-1,求前缀和sum[i]=sum[i-1]+a[i]即

2019 ICPC上海网络赛 G. Substring 哈希+尺取法+unordered_map

题目链接:https://nanti.jisuanke.com/t/41415 赛后补题. 参考博客:https://blog.csdn.net/bjfu170203101/article/details/100889468 题意:给出一个主串(假设长度为m),再给出n个模式串,对于每一个模式串,如果在主串中有一个子串,它的第一个字符和最后一个字符分别与这个模式串的开头字符和结尾字符相同,并且每一种字符出现的次数相同,那么就把这个模式串和这个子串看成是相同的,认为模式串在主串中出现过一次,比如模

2019年ICPC南昌网络赛 J. Distance on the tree 树链剖分+主席树

边权转点权,每次遍历到下一个点,把走个这条边的权值加入主席树中即可. #include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> using namespace std; const int maxx = 2e5+10; struct node{ int l,r,cnt; }tree[maxx*40]; int head[maxx],rk[maxx],siz[maxx

2019 ICPC 南昌网络赛

2019 ICPC 南昌网络赛 比赛时间:2019.9.8 比赛链接:The 2019 Asia Nanchang First Round Online Programming Contest 总结 // 史上排名最高一次,开场不到两小时队友各A一题加水题共四题,排名瞬间升至三四十名 // 然后后三小时就自闭了,一题都没有突破...最后排名211 hhhh ? ? B. Fire-Fighting Hero 题意 队友做的,待补. ? AC代码 #include<cstdio> #includ

2013 ACM/ICPC 长沙现场赛 A题 - Alice&#39;s Print Service (ZOJ 3726)

Alice's Print Service Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her print service found some tricks to save money. For example, the price when

2018 ICPC 徐州网络赛

2018 ICPC 徐州网络赛 A. Hard to prepare 题目描述:\(n\)个数围成一个环,每个数是\(0\)~\(2^k-1\),相邻两个数的同或值不为零,问方案数. solution 将环变成链,设\(f[i][0\)~\(2]\),分别表示与第一个数相同,与第一个数不同,与第一个数相同,与第一个数的反相同.然后\(dp\)即可. 时间复杂度:\(O(n)\) B. BE, GE or NE solution 根据题目描述\(dp\)即可. 时间复杂度:\(O(nm)\) C.

【上海网络赛】B.Light bulbs

题目描述 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 exampl

hdu 5053 the Sum of Cube(上海网络赛)

the Sum of Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 405    Accepted Submission(s): 224 Problem Description A range is given, the begin and the end are both integers. You should sum

Digit sum (第 44 届 ACM/ICPC 亚洲区域赛(上海)网络赛)进制预处理水题

131072K A digit sum S_b(n)Sb?(n) is a sum of the base-bb digits of nn. Such as S_{10}(233) = 2 + 3 + 3 = 8   S10?(233)=2+3+3=8, S_{2}(8)=1 + 0 + 0 = 1S2?(8)=1+0+0=1, S_{2}(7)=1 + 1 + 1 = 3S2?(7)=1+1+1=3. Given NN and bb, you need to calculate \sum_{n