简要题意

有一个长度为 \(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;
}

AC Record

ABC238E Range Sums的更多相关文章

  1. [LeetCode] Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  2. LeetCode Count of Range Sum

    原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...

  3. 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 ...

  4. 【LeetCode】327. Count of Range Sum

    题目: Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusiv ...

  5. [Swift]LeetCode327. 区间和的个数 | Count of Range Sum

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  6. 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 ...

  7. 327 Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  8. [LeetCode] 327. Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  9. leetcode 学习心得 (2) (301~516)

    源代码地址:https://github.com/hopebo/hopelee 语言:C++ 301. Remove Invalid Parentheses Remove the minimum nu ...

  10. AtCoder Beginner Contest 238 A - F 题解

    AtCoder Beginner Contest 238 \(A - F\) 题解 A - Exponential or Quadratic 题意 判断 \(2^n > n^2\)是否成立? S ...

随机推荐

  1. 分享个好东西两行前端代码搞定bilibili链接转视频!

    只需要在您的要解析B站视频的页面的</body>前面加上下面两行代码即可,脚本会在客户端浏览器里解析container所匹配到的容器里的B站超链接 (如果不是外围有a标签的超链接只是纯粹的 ...

  2. 使用doctest代码测试和Sphinx自动生成文档

    python代码测试并自动生成文档 Tips:两大工具:doctest--单元测试.Sphinx--自动生成文档 1.doctest doctest是python自带的一个模块.doctest有两种使 ...

  3. Linux进程间通信(一)

    进程间通信 概念:进程是一个独立的资源分配单位,不同进程之间有关联,不能在一个进程中直接访问另一个进程的资源. 进程和进程之间的资源是相互独立的,一个进程不能直接访问另外一个进程的资源,但是进程和进程 ...

  4. [VUE]报错: No Babel config file detected for

    在使用vue脚手架创建的项目中,项目中每个文件的第一行都会有红色波浪线. 解决方法:在项目文件中找到package.json文件,在parserOptions里添加"requireConfi ...

  5. Pthread 并发编程(一)——深入剖析线程基本元素和状态

    Pthread 并发编程(一)--深入剖析线程基本元素和状态 前言 在本篇文章当中讲主要给大家介绍 pthread 并发编程当中关于线程的基础概念,并且深入剖析进程的相关属性和设置,以及线程在内存当中 ...

  6. ES6 学习笔记(十二)代理器Proxy的简单使用

    1.前言 以前在学习react时做了个仿手机端的QQ音乐项目.当时的数据是通过proxy代理的QQ音乐数据接口,直接写在package.json里面.Proxy 对象(Proxy)是 ES6的特性,只 ...

  7. prefetch和preload

    前面的话 基于VUE的前端小站改造成SSR服务器端渲染后,HTML文档会自动使用preload和prefetch来预加载所需资源,本文将详细介绍preload和prefetch的使用 资源优先级 在介 ...

  8. Python学习之实例3

    一.文字读取并打印拼接字符串 1 with open('G:\python\char.txt') as f: #使用open()函数以只读模式打开文件 2 s=f.read() #使用read()方法 ...

  9. 关于Docker的一些事--Docker-Compose 升级版本

    起源 近来一直在研究怎么搭建自己的私有网盘,本着虚心耐心,认真求是态度,开始做起了实验,最终种草了Nextcloud这款开源网盘,然而用私人的服务器感觉很卡,故转战到了一个基友的服务器,感觉非常吊! ...

  10. 2022春每日一题:Day 16

    题目:不同子串个数 这题需要利用后缀数组求出的height的性质,我们发现对于每个后缀,他的height后的所有子串就是算在答案里,因此答案只需要求出n-height[i]-sa[i]+1的和就可以了 ...