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 ...
随机推荐
- 是未来还是“有毒”?紧抓球鞋风口的毒APP机遇与危机并存
编辑 | 于斌 出品 | 于见(mpyujian) 新一代的"潮流"之风正在席卷新生代消费市场,从去年开始,国内二手球鞋交易领域突然开始火爆,大有成为新一轮"风口&quo ...
- [CF]Round 516
A Make a triangle! 题意:给定三根线段,问最少要延长多少才能拼成一个三角形. 数学题. B Equations of Mathematical Magic 题意:求$a - (a \ ...
- python UI自动化生成BeautifulReport测试报告并保存截图
前面已经写过利用BeautifulReport生成测试报告,那么接下来讲讲如何在测试报告里面保存截图 首先需要在测试用例中定义一个截图的方法: # 截图方法 """ os ...
- AcWing 895. 最长上升子序列
//设上升序列的最后一个数字为第i个,那么就以第i-1个位分类标准, //i-1可以没有,也可以是在数组中下标为1,下标为2 //一直到下标为i-1的数字 #include <iostream& ...
- jumpserver 常见错误解决
官方链接:https://jumpserver.readthedocs.io/zh/master/faq_install.html 重启jumpserver后台 #cd /opt#python3.6 ...
- Iris配置
package main import ( "github.com/kataras/iris" "os" "encoding/json" & ...
- 使用maven构建项目时,SSM和springboot项目的打包与云服务器部署
下面讲讲如何打包SSM和springboot项目,并部署到云服务器上. 由于使用的IDE不同,有的使用eclipse,有的使用idea,所以如果在IDE中按照 maven clean 再 maven ...
- python做批量剪切、旋转等小程序
我自己在学习python时自己动手做了几个简单实用的小程序,对大家做图像的增强很有效果,我包括我的同学目前都在用我的小程序来做图像增强,非常的实用.话不多说上代码: import globimport ...
- angular清空node_modules
安装全局包 npm install rimraf -g 执行清空命令 rimraf node_modules
- Linux下查看进程和端口信息
1.根据进程名查看进程信息,以查看tomcat进程名为例,查看所对应的进程id为1095(或者使用: ps -aux | grep tomcat 查看占用内存等信息) ps -ef | grep to ...