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 ...
随机推荐
- mybatis(五):源码分析 - mapper文件加载流程
- Python常用的类库、对应的方法和属性
Python常用的类库.对应的方法和属性
- Codeforces Round #603 (Div. 2) A.Sweet Problem
#include <cstdio> #include <algorithm> using namespace std; int main() { int t; scanf(&q ...
- Application Comparison Of LED Holiday Light And Traditional Incandescent Lights
Obviously, LED holiday lights are leading the competition in economical Christmas decorations, but t ...
- Motif
Motif discovery is in loose terms the problem of finding interesting patterns in sequences. motif: i ...
- An easy problem(位运算)
As we known, data stored in the computers is in binary form.(数据以二进制形式存储于电脑之中.)The problem we discuss ...
- webrtc vp8与h264 sdp文件解读
参考地址:https://blog.csdn.net/zhangjikuan/article/details/27367437, https://www.cnblogs.com/idignew/p/7 ...
- 搭建Python开发环境(Mac)
准备 Python官网: https://www.python.org/ Python官方文档: https://docs.python.org/ 环境搭建 简介 pipenv是Python官方推荐的 ...
- String.valueOf()和toString()的区别
1.String.valueOf(): Object obj=null; String str=""; str=String.valueOf(obj); //str=obj.toS ...
- 2019-08-05 纪中NOIP模拟B组
T1 [JZOJ1432] 输油管道 题目描述 请你帮忙设计一个从城市M到城市Z的输油管道,现在已经把整个区域划分为R行C列,每个单元格可能是空的也可能是以下7种基本管道之一: 油从城市M流向Z,‘+ ...