简要题意

有一个长度为 \(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. 构建Springboot项目、实现简单的输出功能、将项目打包成可以执行的JAR包(详细图解过程)

    1.构建SpringBoot项目 大致流程 1.新建工程 2.选择环境配置.jdk版本 3.选择 依赖(可省略.后续要手动在pom文件写) 4.项目名 1.1 图解建立过程 1.2 项目结构 友情提示 ...

  2. 不妨试试更快更小更灵活Java开发框架Solon

    @ 目录 概述 定义 性能 架构 实战 Solon Web示例 Solon Mybatis-Plus示例 Solon WebSocket示例 Solon Remoting RPC示例 Solon Cl ...

  3. Java并发编程 | Synchronized原理与使用

    Java提供了多种机制实现多线程之间有需要同步执行的场景需求.其中最基本的是Synchronized ,实现上使用对象监视器( Monitor ). Java中的每个对象都是与线程可以锁定或解锁的对象 ...

  4. 16.-admin管理后台

    一.admin管理后台 Django提供给了比较完善的后台管理数据库接口,可供开发过程中调用和测试使用 Django会搜集所有已注册的模型类,为这些模型类提供数据管理界面,供开发者使用   命令:py ...

  5. day02-实现01

    实现01 1.实现任务阶段1 编写mytomcat,该服务器能给浏览器返回"你好,我是服务器!"的简单信息. 根据之前的tomcat框架整体分析,我们将浏览器发送请求,tomcat ...

  6. 记一次mybatis性能问题分析过程

    说明 今天发现个2个问题,一是mybatisplus执行一条某个字段值比较长(约1.8M的文本)的INSERT语句耗时要90s+;二是读取这个1.8M文本返回给前端耗时6min.查查查查了半天搞不清楚 ...

  7. ctfshow web入门部分题目 (更新中)

    CTFSHOW(WEB) web入门 给她 1 参考文档 https://blog.csdn.net/weixin_51412071/article/details/124270277 查看链接 sq ...

  8. kubernetes笔记-1-基础环境部署

    一.环境信息: 操作系统:ubuntu 18.04 server amd64 docker:docker 19.03.ce kubernetes:v1.19 IP地址 主机名   角色 172.29. ...

  9. 数据结构初阶--顺序表(讲解+C++类模板实现)

    顺序的概念与结构 顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储.在数组上完成数据的增删查改. 一般分为两种:静态顺序表和动态顺序表 静态顺序表 #define ...

  10. 使用repo上传代码

    前言~ repo是一款安卓用于管理源码的工具,由python实现,基于git工具 本文介绍了repo的常用使用方式. 一,下载代码 1. repo init 初始化命令 此命令常用选项就那几个,此处取 ...