The Preliminary Contest for ICPC Asia Shanghai 2019 B Light bulbs (离散的差分)
复杂度分析,询问一千次,区间长1e6,O(1e9)超时。
那么我们知道对于差分来说,没必要一个一个求,只需要知道区间长就可以了,所以我们定义结构体差分节点,一个头结点,一个尾节点。
这样tail.loc-head.loc就是整个区间长,该区间的实际的大小就是 Add标记的大小,Add标记就是从头加到尾。
排序2*m个差分节点,对于loc相同的节点,说明是同一个位置的差分,add合并就可以了,不需要进行统计,直接跳过本次循环。
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e3+10;
const int INF=0x3f3f3f3f;
int T,N,M,L,R;
struct Node {
int loc,add;
}node[maxn];
bool cmp(const Node &a,const Node &b)
{
return a.loc<b.loc;
}
long long getAns()
{
M*=2;
sort(node,node+M,cmp);
long long ans=0,tmp=0;
for (int i=0;i<M-1;i++) {
if (node[i].loc==node[i+1].loc) {
tmp+=node[i].add;
continue;
}
tmp+=node[i].add;
ans+=(tmp%2)*(node[i+1].loc-node[i].loc);
}
return ans;
}
inline int read()
{
int f=1,num=0;
char ch=getchar();
while (ch>'9'||ch<'0') {
if (ch=='-') {
f=-1;
}
ch=getchar();
}
while (ch>='0'&&ch<='9') {
num=num*10+ch-'0';
ch=getchar();
}
return num*f;
}
inline void print(int x)
{
if (x<0) {
putchar('-');
}
if (x>9) {
print(x/10);
}
putchar(x%10+'0');
}
int main()
{
int kase=1;
//scanf("%d",&T);
T=read();
while (T--) {
N=read(),M=read();
//scanf("%d%d",&N,&M);
for (int i=0;i<2*M;i++) {
node[i].add=node[i].loc=0;
}
for (int i=0;i<M;i++) {
L=read(),R=read();
//scanf("%d%d",&L,&R);
node[i].loc=L;
node[i+M].loc=R+1;
node[i].add++;
node[i+M].add--;
}
printf("Case #%d: %lld\n",kase++,getAns());
}
return 0;
}
The Preliminary Contest for ICPC Asia Shanghai 2019 B Light bulbs (离散的差分)的更多相关文章
- The Preliminary Contest for ICPC Asia Shanghai 2019 B. Light bulbs
题目:https://nanti.jisuanke.com/t/41399 思路:差分数组 区间内操作次数为奇数次则灯为打开状态 #include<bits/stdc++.h> using ...
- The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力)
The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力) 传送门:https://nanti.jisuanke.com/ ...
- The Preliminary Contest for ICPC Asia Shanghai 2019
传送门 B. Light bulbs 题意: 起初\(n\)个位置状态为\(0\),\(m\)次操作,每次操作更换区间状态:\(0\)到\(1\),\(1\)到\(0\). 共有\(T,T\leq 1 ...
- The Preliminary Contest for ICPC Asia Shanghai 2019 C. Triple
[传送门] FFT第三题! 其实就是要求有多少三元组满足两短边之和大于等于第三边. 考虑容斥,就是枚举最长边,另外两个数组里有多少对边之和比它小,然后就是 $n^3$ 减去这个答案. 当 $n \le ...
- 01背包方案数(变种题)Stone game--The Preliminary Contest for ICPC Asia Shanghai 2019
题意:https://nanti.jisuanke.com/t/41420 给你n个石子的重量,要求满足(Sum<=2*sum<=Sum+min)的方案数,min是你手里的最小值. 思路: ...
- 给定进制下1-n每一位数的共享(Digit sum)The Preliminary Contest for ICPC Asia Shanghai 2019
题意:https://nanti.jisuanke.com/t/41422 对每一位进行找循环节规律就行了. #define IOS ios_base::sync_with_stdio(0); cin ...
- The Preliminary Contest for ICPC Asia Shanghai 2019 A. Lightning Routing I
传送门 因为某些原因,所以我就去学了 $LCT$ 维护直径, $LCT$ 维护直径我上一个博客讲得很详细了:传送门 这里维护虚儿子用的是 $multiset$ ,没写可删堆 #include<i ...
- The Preliminary Contest for ICPC Asia Shanghai 2019 L. Digit sum
题目:https://nanti.jisuanke.com/t/41422 思路:预处理 #include<bits/stdc++.h> using namespace std; ][]= ...
- The Preliminary Contest for ICPC Asia Shanghai 2019 J. Stone game
题目:https://nanti.jisuanke.com/t/41420 思路:当a(a∈S′)为最小值 如果Sum(S′)−a≤Sum(S−S′)成立 那么(∀t∈S′,Sum(S′)−t≤Sum ...
随机推荐
- python之路正则表达式
元字符 蓝色框中有非贪婪模式也不起作用,非贪婪模式在+后加上?号 有空字符串
- mybatis(五):源码分析 - sqlsession执行流程
- 添加一个layer
let testLayer = new TestLayer1();this.addChild(testLayer); let TestLayer1 = cc.Layer.extend({ ctor:f ...
- 网易云信融合CDN方案及实践
日前,网易云信视频云架构师席智勇在第七届GFIC全球家庭互联网大会进行了题为<网易云信融合CDN方案及实践>的分享,以下是演讲内容回顾. 图为 网易云信视频云架构师席智勇 CDN所面临的问 ...
- Xampp在Mac下报403的问题
将httpd.conf里的 User daemonGroup daemon 改为 User myusername(当前使用的用户名)Group admin
- Java EE开发课外事务管理平台
Java EE开发课外事务管理平台 演示地址:https://ganquanzhong.top/edu 说明文档 一.系统需求 目前课外兴趣培训学校众多,完善,但是针对课外兴趣培训学校教务和人事管理信 ...
- shell 预定义变量
echo "上一次后台pid is $!"echo "当前进程pid is $$"echo "last command return code is ...
- mybatis-plus 错误
错误:java.lang.NoClassDefFoundError: org/apache/velocity/context/Context 原因: 缺少velocity的依赖 解决方案: <d ...
- C#常见几种集合比较
1. ArrayList 1.1 ArrayList是一个特殊数组,通过添加和删除元素就可以动态改变数组的长度. ArrayList集合相对于数组的优点:支持自动改变大小,可以灵活的插入元素,可以灵活 ...
- opencv3.3 基础:Mat类里setTo函数
Mat& setTo(InputArray value, InputArray mask=noArray()); 说明: 1.功能:把矩阵mask中元素不为0的点全部变为value值: 2.当 ...