当前位置:网站首页>#796 Div.2 F. Sanae and Giant Robot set *
#796 Div.2 F. Sanae and Giant Robot set *
2022-06-28 00:33:00 【Strezia】
1688F
set,2500
The question
give a , b a,b a,b Two sequences , And give m m m Intervals [ l i , r i ] [l_i,r_i] [li,ri], You can satisfy from them every time ∑ i = l r a i = ∑ i = l r b i \sum_{i=l}^ra_i=\sum_{i=l}^rb_i ∑i=lrai=∑i=lrbi Any one of the intervals of , Execute on all numbers in this interval a i = b i a_i=b_i ai=bi, Ask if you can go through several operations , bring a a a Sequence sum b b b The same sequence ?
Ideas
An interval can be selected if and only if the sum of two sequences in this interval is the same , That is, the difference is 0, So use arrays s i s_i si Represents the difference between the prefixes of two arrays , namely s i = s i − 1 + a i − b i s_i=s_{i-1} + a_i - b_i si=si−1+ai−bi, Then interval [ l , r ] [l,r] [l,r] Can be selected if and only if s l − 1 = s r s_{l-1}=s_r sl−1=sr, And after the operation s l − 1 = s l = . . . = s r s_{l-1}=s_l=...=s_r sl−1=sl=...=sr, The ultimate goal is to make s s s The array is all 0 0 0 , So choose s l − 1 ≠ 0 s_{l-1}\neq 0 sl−1=0 It doesn't make sense .
So the question can be translated into : For interval [ l , r ] [l,r] [l,r] , If s l − 1 = s r = 0 s_{l-1}=s_{r}=0 sl−1=sr=0 , makes s l − 1 = s l = . . . = s r = 0 s_{l-1}=s_l=...=s_r=0 sl−1=sl=...=sr=0 , Is it possible to make s s s All for 0 0 0 .
It can be used queue Save array s s s Have been to 0 The location of ,set Not yet 0 The location of , The sequence traversal has been 0 The side connected by the position of , If the other point is also 0, Then traverse the range within this interval set And add to the queue . If the final team is empty but set Not empty , There is no solution. . Each point can join the queue at most once , Time complexity O ( ( n + m ) log n ) O((n+m)\log n) O((n+m)logn)
Code
int n, m;
int a[maxn], b[maxn], s[maxn];
vector<int> e[maxn];
void solve() {
cin >> n >> m;
set<int> se;
for(int i = 1; i <= n; i++) {
cin >> a[i];
se.insert(i);
}
for(int i = 1; i <= n; i++) {
cin >> b[i];
s[i] = s[i-1] + a[i] - b[i];
e[i].clear();
}
e[0].clear();
//se Express non 0 The location of
for(int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
x--;
e[x].pb(y);
e[y].pb(x);
}
queue<int> q;
for(int i = 0; i <= n; i++) {
if(!s[i]) {
q.push(i);
se.erase(i);
}
}
while(!q.empty()) {
int x = q.front();
q.pop();
for(int i = 0; i < e[x].size(); i++) {
int y = e[x][i];
if(s[y]) continue;
int l = min(x, y), r = max(x, y);
auto itl = se.lower_bound(l), itr = se.upper_bound(r);
for(auto it = itl; it != itr; it++) {
s[*it] = 0;
q.push(*it);
}
se.erase(itl, itr);
}
}
if(!se.empty()) {
cout << "NO\n";
}
else {
cout << "YES\n";
}
}
边栏推荐
- 每次启动项目的服务,电脑竟然乖乖的帮我打开了浏览器,100行源码揭秘!
- 数仓的字符截取三胞胎:substrb、substr、substring
- TIME_WAIT过多的解决办法
- [untitled]
- Local visualization tool connects to redis of Alibaba cloud CentOS server
- Flutter series: Transformers in flutter
- #796 Div.2 F. Sanae and Giant Robot set *
- internship:业务流程初识
- Hcip/hcie Routing & Switching / datacom Reference Dictionary Series (19) comprehensive summary of PKI knowledge points (public key infrastructure)
- Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
猜你喜欢

Modern programming language: rust

Hcip/hcie Routing & Switching / datacom Reference Dictionary Series (19) comprehensive summary of PKI knowledge points (public key infrastructure)

炼金术(1): 识别项目开发中的ProtoType、Demo、MVP

LabVIEW连续采样与有限采样模式
![[Reading Abstract] what is wrong with English Reading Teaching in schools-- Empiricism and the PK of cognitive science](/img/7b/8b3619d7726fdaa58da46b0c8451a4.png)
[Reading Abstract] what is wrong with English Reading Teaching in schools-- Empiricism and the PK of cognitive science

Instructions for vivado FFT IP

MATLB|基于复杂网络的配电系统微电网优化配置

GFS 分布式文件系统概述与部署

Character interception triplets of data warehouse: substrb, substr, substring

Comprehensive evaluation of free, easy-to-use and powerful open source note taking software
随机推荐
Transmitting and receiving antenna pattern
Summary of wuenda's machine learning course (11)_ Support vector machine
flutter系列之:flutter中的变形金刚Transform
本地可视化工具连接阿里云centOS服务器的redis
Technical debt wall: a way to make technical debt visible and negotiable
MATLB|基于复杂网络的配电系统微电网优化配置
计数质数[枚举 -> 空间换时间]
Validaterequest= "false" is a "suggestion collection" for what
RecyclerView实现分组效果,多种实现方式
Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
软件工程作业设计(1): [个人项目] 实现一个日志查看页面
Local visualization tool connects to redis of Alibaba cloud CentOS server
GFS 分布式文件系统概述与部署
QStringList 的学习笔记
用两个栈实现队列[两次先进后出便是先进先出]
Quickly master grep commands and regular expressions
JVM的内存模型简介
#796 Div.2 F. Sanae and Giant Robot set *
炼金术(4): 程序员的心智模型
剑指 Offer 61. 扑克牌中的顺子