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 ...
随机推荐
- 如何实现一个SQL解析器
作者:vivo 互联网搜索团队- Deng Jie 一.背景 随着技术的不断的发展,在大数据领域出现了越来越多的技术框架.而为了降低大数据的学习成本和难度,越来越多的大数据技术和应用开始支持SQL进 ...
- 题解 SP24 FCTRL2 - Small factorials
双倍经验. 题意 给\(t\) 组数据,求每组数据中\(n\) 的阶乘. 思路 \(n≤100\) . \(100!\) 肯定爆int128,所以高精呗. 那么就是一个阶乘的板子了,应该不难的吧. 具 ...
- 真正“搞”懂HTTP协议02之空间穿梭
时隔四年,这个系列鸽了四年,我终于觉得我可以按照自己的思路和想法把这个系列完整的表达出来了. 想起四年前,那时候还是2018年的六月份,那时候我还工作不到两年,那时候我翻译了RFC2616的部分内容, ...
- ahk_more
;20:47 2022/5/8 #NoEnv #Warn #SingleInstance Force ;设工作目录为桌面 SetWorkingDir %A_Desktop% ;托盘提示必须放在热键前面 ...
- docker容器化业务
1.环境准备: 设备 IP地址 作用 系统版本 mysql-master 192.168.100.213 Nginx-Web服务器 Ubuntu2004 mysql-slave 192.168.100 ...
- LVS之NAT、DR、TUNNEL实验
1.LVS-NAT规则+WRR算法 服务器 IP地址 作用 系统版本 RS1 10.0.0.8/24GW:10.0.0.101 网站服务器 Rocky8.6 RS2 10.0.0.18/24GW:10 ...
- 2022NISACTF--WEB
easyssrf 打开题目,显示的是 尝试输入, 发现输入flag有东西 读取文件 访问下一个网站 读取文件 不能以file开头 直接伪协议,base64解码 checkIn 奇怪的unicode编码 ...
- Archlinux + Dwm 配置流程
本着学习C的态度来了解dwm,本身作为一个i3wm的追崇者,与dwm会擦出怎么样的火花呢? 下载安装dwm archlinuxcn源配置 编辑/etc/pacman.conf文件,添加bfsu的arc ...
- xshell调整字体大小
1. 进入设置 %default%:默认对全部终端生效 如果想对单一终端单独设置,可先进入会话管理,针对单一会话单独调整 2. 点击外观配置,修改字体大小
- 链接脚本(Linker Scripts)语法和规则解析(自官方手册)
为了便于与英文原文对照学习与理解(部分翻译可能不准确),本文中的每个子章节标题和引用使用的都是官方手册英文原称.命令及命令行选项统一使用斜体书写.高频小节会用蓝色字体标出. 3 Linker Scri ...