简要题意

有一个长度为 \(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. 现有一个接口DataOperation定义了排序方法sort(int[])和查找方法search(int[],int),已知类QuickSort的quickSort(int[])方法实现了快速排序算法

    欢迎大家加入我的社区:http://t.csdn.cn/Q52km 社区中不定时发红包 文章目录 1.UML类图 2.源码: 3.优缺点分析 1.UML类图 2.源码: package com.bac ...

  2. 齐博x1标签动态调用数据

    示例代码如下: {qb:tag name="news_list_page_listdata02" type="cms" union="fid" ...

  3. <四>1:全面掌握Const的用法

    const怎么理解? const修饰的变量不能够在作为左值!!初始化完成后,值不能被修改!! C 和C++中const 的区别? 在C程序中 test.c const int a; 只定义,不做初始化 ...

  4. 题解 P4058 [Code+#1]木材

    前言 这什么题啊,不就是个二分答案我从65到100都经历了一遍--(瞬间气哭) \(\sf {Solution}\) 题目理解起来不难的,大意就懒得写了. 一眼二分答案. 此题属于在形如 \(\{0, ...

  5. MySQL索引报错

    今天在MySQL 5.7版本的数据库中导库InnoDB表字段长度时遇到了"ERROR 1071 (42000): Specified key was too long; max key le ...

  6. Codeforces Round #831 (Div. 1 + Div. 2) A-E

    比赛链接 A 题解 知识点:数学. \(2\) 特判加 \(7\),其他加 \(3\) 直接偶数. 时间复杂度 \(O(1)\) 空间复杂度 \(O(1)\) 代码 #include <bits ...

  7. Azure DevOps Server 入门实践与安装部署

    一,引言 最近一段时间,公司希望在自己的服务器上安装本地版的 Azure DevOps Service(Azure DevOps Server),用于项目内的测试,学习.本着学习的目的,我也就开始学习 ...

  8. C# 语法分析器(二)LR(0) 语法分析

    系列导航 (一)语法分析介绍 (二)LR(0) 语法分析 (三)LALR 语法分析 (四)二义性文法 (五)错误恢复 (六)构造语法分析器 首先,需要介绍下 LALR 语法分析的基础:LR(0) 语法 ...

  9. Vue 实现小小记事本

    1.实现效果 用户输入后按回车,输入的内容自动保存,下方会显示记录的条数,鼠标移动到文字所在div上,会显示删除按钮,点击按钮,相应记录会被删除,下方的记录条数会相应变化,点击clear,所有记录会被 ...

  10. 想开发DAYU200,我教你

    摘要:本文主要介绍OpenHarmony富设备DAYU200开发板的入门指导. 本文分享自华为云社区<DAYU200开发指导>,作者: 星辰27. 1 概述 DAYU200开发板属于Ope ...