ABC238E Range Sums
简要题意
有一个长度为 \(N\) 的序列 \(a\),你知道 \(Q\) 个区间的和。求是否可以知道 \([1,n]\) 的和。
\(1 \leq N,Q \leq 2 \times 10^5\)
思路
这是一道并查集题。
首先考虑,我们是如何快速求区间和的:前缀和!
首先考虑以下前缀和,令 \(P\) 为 \(a\) 的前缀和,那么我们只需要知道 \(P_r\) 和 \(P_{l-1}\) 就可以了。
所以我们自然想到对于知道的区间 \([l,r]\),连边 \((l-1,r)\)。最后查 \(0\) 和 \(N\) 是否连通即可。
使用并查集实现,时间复杂度 \(O(Q\log N)\)。
代码
#include <bits/stdc++.h>
#define int long long
#pragma GCC optimize("Ofast", "inline", "-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
using namespace std;
int n,q;
int fa[2000005];
int find(int x){
if(fa[x]==x)return x;
else {
int parent=find(fa[x]);
fa[x]=parent;
return parent;
}
}
void merge(int x,int y){
int fx=find(x),fy=find(y);
if(fx==fy)return;
else{
fa[fy]=fx;
}
}
bool same(int x,int y){
return find(x)==find(y);
}
signed main(){
ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n>>q;
for(int i=1;i<=n;i++){
fa[i]=i;
}
for(int i=1;i<=q;i++){
int l,r;
cin>>l>>r;
merge(l-1,r);
}
cout<<(same(0,n)?"Yes":"No")<<'\n';
return 0;
}
ABC238E Range Sums的更多相关文章
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- LeetCode Count of Range Sum
原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...
- leetcode@ [327] Count of Range Sum (Binary Search)
https://leetcode.com/problems/count-of-range-sum/ Given an integer array nums, return the number of ...
- 【LeetCode】327. Count of Range Sum
题目: Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusiv ...
- [Swift]LeetCode327. 区间和的个数 | Count of Range Sum
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- 327. Count of Range Sum(inplace_marge)
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- 327 Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- [LeetCode] 327. Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- leetcode 学习心得 (2) (301~516)
源代码地址:https://github.com/hopebo/hopelee 语言:C++ 301. Remove Invalid Parentheses Remove the minimum nu ...
- AtCoder Beginner Contest 238 A - F 题解
AtCoder Beginner Contest 238 \(A - F\) 题解 A - Exponential or Quadratic 题意 判断 \(2^n > n^2\)是否成立? S ...
随机推荐
- winscp报错Server sent passive reply with unroutable address. Using server address instead
找了一堆没用. 最后终于 1.使用winSCP连接ftp时,编辑会话,单击高级. 2.进入高级设置之后,单击连接,查看连接模式,把被动模式的勾,勾掉. 3.单击确定,然后保存配置,重新连接FTP,OK
- C#中下载项目中的文件
1.将需要下载的文档添加到项目的文件夹中 2.接口部分 public IActionResult DownLoad() { var filePath = Directory.GetCurrentDir ...
- js 获取开始时间和结束时间相隔小时及分钟(时间戳操作)
js 获取开始时间和结束时间相隔小时及分钟(时间戳操作) 场景描述:获取开始时间和结束时间相隔小时及分钟 实例: TimeOnConfirm(curDate) { if(this.pickernum ...
- 三、Go环境安装
3.1.Go编译器的下载 官网:https://golang.google.cn/go中文网:https://studygolang.com/dl 3.2.安装 for Mac 3.2.1. mac ...
- 基于Camera Link和PCIe DMA的多通道视频采集和显示系统
基于Camera Link和PCIe DMA的多通道视频采集和显示系统 在主机端PCIe驱动的控制和调度下,视频采集与显示系统可以同时完成对多个Camera Link接口视频采集以及Camera Li ...
- mybatis-获取参数值的方式
MyBatis获取参数值的两种方式(重点) MyBatis获取参数值的两种方式:${}和#{} ${}的本质就是字符串拼接,#{}的本质就是占位符赋值 ${}使用字符串拼接的方式拼接sql,若为字符串 ...
- Docker基础和常用命令
Docker基础和常用命令 一,Docker 简介 1.1,什么是 Docker Docker 使用 Google 公司推出的 Go 语言 进行开发实现,基于 Linux 内核的 cgroup,nam ...
- HMM算法python实现
基础介绍,后5项为基础5元素 Q = ['q0', 'q1', 'q2', 'q3'] # 状态集合 States,共 N 种状态 V = ['v0', 'v1'] # 观测集合 Observatio ...
- C#where关键字约束
where关键字的用法 where关键词一个最重要的用法就是在泛型的声明.定义中做出约束. 约束又分为接口约束.基类约束.构造函数约束.函数方法的约束. 1.接口约束,泛型参数必须实现相应的接口才可以 ...
- srcddd
目录 application assembler UserReq.go UserRsp.go dto MessageResult.go UserDTO.go services UserService. ...