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]即可得到进行区间所有数+1操作后每个数的值sum[i])

利用差分的思想,但如果是遍历n还是会超时超内存,此题m较小,所以可以从m下手,只需存端点,最后遍历所有端点即可。官方题解如下:

Code

#include<bits/stdc++.h>
using namespace std;
const int maxn=2005;
pair<int,int>p[maxn];
int main()
{
    int T,cas=0;
    scanf("%d",&T);
    while(T--){
        int n,m,l,r,cnt=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++){
            scanf("%d%d",&l,&r);
            p[cnt++]=make_pair(l,1);
            p[cnt++]=make_pair(r+1,-1);
        }
        sort(p,p+cnt);
        int sum=0,now=0,ans=0;
        for(int i=1;i<cnt;i++){
            sum+=p[i].second;
            if(sum&1)
                ans+=p[i].first-p[i-1].first;
        }
        printf("Case #%d: %d\n",++cas,ans);
    }
    return 0;
}

差分:

何为差分?差分就是将数列中的每一项分别与前一项数做差。

例如:原数列a[n],差分数列b[n],b[i]=a[i]-a[i-1],a[i]=差分数列的前缀和sum[i]。

(注意:1.差分序列的第一个数和原来的第一个数一样,相当于第一个数去减0;2.差分序列最后会比原序列多一个数,相当于0减最后一个数)

一个序列原本为 1 3 5 2 9 3,差分后得到 1 2 2 -3 7 -6。

两个性质:

①差分序列求前缀和可得原序列;

②原序列区间[L,R]中的元素全部+K,可以转化操作为差分序列L处+K,R+1处-K;

原文地址:https://www.cnblogs.com/HOLLAY/p/11533869.html

时间: 2024-07-30 13:58:36

2019上海icpc网络赛B. Light bulbs(思维+差分)的相关文章

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]\) 内的所有灯泡改变状态. 求最后有几个灯泡是亮的. 思路 题目挺简单的,翻转奇数次的灯泡是亮的,所以要求每个灯泡翻转的次数. 容易想到可以用差分. 对所有操作的两

2019南昌icpc网络赛 I题 分块套BIT

https://nanti.jisuanke.com/t/41356 对于带修的二维数点,可以bit套主席树,也可CDQ三维偏序 但是最后我选择分块套BIT暴力... 复杂度为$m(blocksize*logn+blocknum)$ 显然,如果按照$\sqrt{n}$分块,并不是最优的 我们可以适当的增加块的大小,减少块的数量,让$blocksize*logn=blocknum$ 在这个题中,大概就是$\sqrt{1e6}$到$\sqrt{2e6}$之间吧 #include<bits/stdc+

2019上海赛区网络赛(C,G)

7题弟弟. 提交次数最多的两题是C和G,这里决定写一下. G: 一眼题,题目即是问,给定字符串T,有多少S的子区间s,满足s和T的首尾相同,中间的那些字母每一种的个数相同.不难想到hash. 因为时限比较长,所以可以暴力一点的,把询问长度相同的拿出来一块处理.  这样的不同种类<sqrt种. 所以总的复杂度就是O(NsqrtN*k).k是hash的复杂度,我开始用unorderedmap存的,T了,然后改成二分才过了. 可以参考CF963D的hash做法. 吃饭了先 #include<bits

Ryuji doesn&#39;t want to study 2018徐州icpc网络赛 树状数组

Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]. Unfortunately, the longer he learns, the fewer he gets. That means, if he reads books from ll to rr, he will get a

日常补题——ICPC网络赛上海站第二题B Light bulbs

题目链接: https://nanti.jisuanke.com/t/41399 博客借鉴: https://blog.csdn.net/weixin_43701790/article/details/100867368 题目大意: 每次操作使 L,R区间内的等的状态改变,最后一共有多少开着的灯 题解: 这道题的时间限制是1000ms,内存限制是8192K 可以说是很不常见的卡内存的题 一开始我们用的是线段树,直接超内存 然后想着用short int 代替int,也还是不行 然后想着如果不用la

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

hdu 5053 the Sum of Cube---2014acm上海赛区网络赛

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5053 the Sum of Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 140    Accepted Submission(s): 80 Problem Description A range is given, the b

hdu 5050 Divided Land---2014acm上海赛区网络赛

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5050 Divided Land Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 115    Accepted Submission(s): 57 Problem Description It's time to fight the loc

Trace 2018徐州icpc网络赛 思维+二分

There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy) means the wave is a rectangle whose vertexes are ( 00 , 00 ), ( xx , 00 ), ( 00 , yy ), ( xx , yy ). Every time the wave will wash out the trace of f