2019年icpc上海网络赛 B Light bulbs (分块、差分)
https://nanti.jisuanke.com/t/41399
题目大意:
有n个灯,m次操作,每次修改[l,r]内的灯,(off - on ,on - off),问最后有几盏灯亮着.
换种说法:n个点m个区间,每次区间内的数+1,最后n个点中计数为奇数的点的个数就是答案。
刚开始没注意,直接用线段树写,超内存了。。。。
这题因为外层有个T,并且n太大,还要卡内存,太过分了。
卡数据卡内存,每组样例一重循环都会超时。所以可以分块和对m处理来做。
对m处理的话,仔细想一想,只有区间次数被操作奇数次的话,这个区间操作才有效,所以我们只要把左右端点从小到大排序然后区间求和就行了。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <math.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const int maxn=1e7+;
using namespace std;
int a[]; int main()
{
int T;
scanf("%d",&T);
for(int k=;k<=T;k++)
{
int n,m;
int l,r;
scanf("%d %d",&n,&m);
for(int i=;i<*m;i++)
{
scanf("%d %d",&l,&r);
a[i++]=l;
a[i]=++r;
}
m*=;
int ans=;
sort(a,a+m);
// for(int i=0;i<m;i++)
// {
// printf("%d ",a[i]);
// }
for(int i=;i<m;i+=)
{
ans+=a[i]-a[i-];
}
printf("Case #%d: %d\n",k,ans);
}
return ;
}
对m下手,注意到:
把每一个端点存起来之后(并且排序),毎两个点之间的区间的 亮与否至于左边有关,不包括右边.
所以利用差分的思想,修改 L,R相当于a(L)+1,a(R+1)-1
我们把端点 排好序之后,就直接跑一遍m
如果当前为左端点 则 覆盖层数++(意思就是 之后的每个数都多了一个区间覆盖)
如果当前为右端点 则覆盖层数--(意思就是 之后每个数都少了一个覆盖区间)
因为区间左闭右开,只要为奇数(灯亮),就加上左边右开区间 R-L的值
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <math.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const int maxn=1e7+;
using namespace std; struct node
{
int f;
int id;
bool friend operator < (node a,node b)
{
if(a.id==b.id)
return a.f<b.f;
return a.id<b.id;
}
}pos[]; int main()
{
int T;
scanf("%d",&T);
for(int k=;k<=T;k++)
{
int n,m;
int l,r;
int cnt=;
scanf("%d %d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d %d",&l,&r);
pos[++cnt].id=l;
pos[cnt].f=;
pos[++cnt].id=r+;
pos[cnt].f=;
}
int cot=;
int ans=;
sort(pos+,pos++cnt);
for(int i=;i<=cnt-;i++)
{
if(pos[i].f)
cot--;
else
cot++;
if(cot%)
ans+=pos[i+].id-pos[i].id; }
printf("Case #%d: %d\n",k,ans);
}
return ;
}
2019年icpc上海网络赛 B Light bulbs (分块、差分)的更多相关文章
- 2019 ACM-ICPC 上海网络赛 B. Light bulbs (差分)
题目链接:Light bulbs 比赛链接:The Preliminary Contest for ICPC Asia Shanghai 2019 题意 给定 \(N\) 个灯泡 (编号从 \(0\) ...
- 2019 ICPC上海网络赛 A 题 Lightning Routing I (动态维护树的直径)
题目: 给定一棵树, 带边权. 现在有2种操作: 1.修改第i条边的权值. 2.询问u到其他一个任意点的最大距离是多少. 题解: 树的直径可以通过两次 dfs() 的方法求得.换句话说,到任意点最远的 ...
- 2019年ICPC南昌网络赛 J. Distance on the tree 树链剖分+主席树
边权转点权,每次遍历到下一个点,把走个这条边的权值加入主席树中即可. #include<iostream> #include<algorithm> #include<st ...
- 2019 ICPC 南昌网络赛
2019 ICPC 南昌网络赛 比赛时间:2019.9.8 比赛链接:The 2019 Asia Nanchang First Round Online Programming Contest 总结 ...
- 2018 ICPC 沈阳网络赛
2018 ICPC 沈阳网络赛 Call of Accepted 题目描述:求一个算式的最大值与最小值. solution 按普通算式计算方法做,只不过要同时记住最大值和最小值而已. Convex H ...
- 2018 ICPC 徐州网络赛
2018 ICPC 徐州网络赛 A. Hard to prepare 题目描述:\(n\)个数围成一个环,每个数是\(0\)~\(2^k-1\),相邻两个数的同或值不为零,问方案数. solution ...
- 2019 ICPC 上海区域赛总结
2019上海区域赛现场赛总结 补题情况(以下通过率为牛客提交): 题号 标题 已通过代码 通过率 我的状态 A Mr. Panda and Dominoes 点击查看 5/29 未通过 B Prefi ...
- 2018 ICPC 焦作网络赛 E.Jiu Yuan Wants to Eat
题意:四个操作,区间加,区间每个数乘,区间的数变成 2^64-1-x,求区间和. 题解:2^64-1-x=(2^64-1)-x 因为模数为2^64,-x%2^64=-1*x%2^64 由负数取模的性质 ...
- 2018 ICPC上海大都会赛重现赛 D Thinking-Bear magic (几何)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 D Thinking-Bear magic (几何) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
随机推荐
- CodeForces - 404B Marathon(精度)
题意:一个人绕着一个长度为a的正方形逆时针跑,以(0,0)为起点,喝一次水可以跑d米,问每喝一次水可以跑到的位置坐标. 分析:这道题卡精度卡的太厉害了. 设l是正方形的周长,只有d对l取余且每次跑d米 ...
- file:///D:/Program%20Files/Microsoft%20Visual%20Studio%2011.0/VC/VCWizards/CodeWiz/MFC/Variable/HTML
title VS2005 VS2008添加变量,添加函数,添加类时弹出 Script Error 解决办法 问现象描述 : 问题大家都清楚了.不赘述 错误提示 :file:///C:/Progra ...
- zookeeper 安装及集群
一.zookeeper介绍 zookeeper是一个中间件,为分布式系统提供协调服务,可以为大数据服务,也可以为java服务. 分布式系统,很多计算机组成一个整体,作为一个整体一致对外并处理同一请求, ...
- iOS消息转发
消息转发是一种功能强大的技术,可以大大增加Objective-C的表现力.什么是消息转发?简而言之,它允许未知的消息被困住并作出反应.换句话说,无论何时发送未知消息,它都会以一个很好的包发送到您的 ...
- 原生js完成打地鼠小游戏
:这是首页,有简单模式和地狱模式两种模式进行选择 这是选择完模式之后的游戏界面:30秒一局游戏倒计时,每打中一只老鼠加一分,没砸中减一分,没砸不加不减 首先准备几张图片 html代码: <!-- ...
- 一个简单完整的promiseDemo
想要完全理解代码,需要理解 this 和闭包的含义. Promise是什么 简单来说,Promise 主要就是为了解决异步回调的问题.用 Promise 来处理异步回调使得代码层次清晰,便于理解,且更 ...
- bzoj 4260REBXOR
什么什么trie树??呵呵呵,,,, 一直在困惑怎么处理哪连续一段最大..看了题解迷惑了好久.. 然后突然发现,是xor啊,,在trie树里找到以前得插入的前缀和,然后找到与现在前缀和每一位都不同的, ...
- centos7-DNS(主从)
一.安装DNS. 本机ip:192.168.1.3 [root@localhost ~]# yum -y install bind bind-utlis 二.编辑配置文件. [root@localho ...
- Maven - web 实例
版权所有,未经授权,禁止转载 章节 Maven – 简介 Maven – 工作原理 Maven – Repository(存储库) Maven – pom.xml 文件 Maven – 依赖管理 Ma ...
- 获取网站IP地址(Linux,C)
#include <netdb.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> ...