当前位置:网站首页>#796 Div.2 F. Sanae and Giant Robot set *
#796 Div.2 F. Sanae and Giant Robot set *
2022-06-27 22:14:00 【Strezia】
1688F
set,2500
题意
给出 a , b a,b a,b 两个序列,并给出 m m m 个区间 [ l i , r i ] [l_i,r_i] [li,ri],每次可以从它们中满足 ∑ i = l r a i = ∑ i = l r b i \sum_{i=l}^ra_i=\sum_{i=l}^rb_i ∑i=lrai=∑i=lrbi 的区间中任选一个,对这个区间的所有数执行 a i = b i a_i=b_i ai=bi,问能否经过若干次操作,使得 a a a 序列和 b b b 序列相同?
思路
区间可以被选当且仅当这个区间内两序列和相同,也就是差为0,所以用数组 s i s_i si 表示两个数组前缀和之差,即 s i = s i − 1 + a i − b i s_i=s_{i-1} + a_i - b_i si=si−1+ai−bi,则区间 [ l , r ] [l,r] [l,r] 可以被选当且仅当 s l − 1 = s r s_{l-1}=s_r sl−1=sr,且操作过后 s l − 1 = s l = . . . = s r s_{l-1}=s_l=...=s_r sl−1=sl=...=sr,最终目标是令 s s s 数组全为 0 0 0 ,因此选 s l − 1 ≠ 0 s_{l-1}\neq 0 sl−1=0 的没有意义。
所以题可以转化为:对于区间 [ l , r ] [l,r] [l,r] ,如果 s l − 1 = s r = 0 s_{l-1}=s_{r}=0 sl−1=sr=0 ,则使 s l − 1 = s l = . . . = s r = 0 s_{l-1}=s_l=...=s_r=0 sl−1=sl=...=sr=0 , 是否可以使 s s s 全为 0 0 0 。
可以用 queue 存数组 s s s 已经为0的位置,set 存还不是0的位置,依次遍历已经为0的位置所连的边,若另一点也为0,则遍历范围在这个区间内的 set 中元素并加入队列。如果最终队空但 set 未空,则无解。每个点至多加入队列一次,时间复杂度 O ( ( n + m ) log n ) O((n+m)\log n) O((n+m)logn)
代码
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 表示非0的位置
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";
}
}
边栏推荐
- Comprehensive evaluation of free, easy-to-use and powerful open source note taking software
- It supports deleting and updating the priority queue of any node
- Chenyun pytorch learning notes_ Build RESNET with 50 lines of code
- NDSS 2022 received list
- 安全省油環保 駱駝AGM啟停電池魅力十足
- TIME_ Solutions to excessive wait
- MongoDB-在windows电脑本地安装一个mongodb的数据库
- [microservices sentinel] sentinel data persistence
- 技术的极限(11): 有趣的编程
- 翻译(5): 技术债务墻:一种让技术债务可见并可协商的方法
猜你喜欢

Safe, fuel-efficient and environment-friendly camel AGM start stop battery is full of charm

Transmitting and receiving antenna pattern

Matlb| optimal configuration of microgrid in distribution system based on complex network
![用两个栈实现队列[两次先进后出便是先进先出]](/img/de/07297816f1a44d41389bb45d012c80.png)
用两个栈实现队列[两次先进后出便是先进先出]

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

Comprehensive evaluation of free, easy-to-use and powerful open source note taking software
![Software engineering job design (1): [personal project] implements a log view page](/img/95/0c3f0dde16d220ddecb5758a4c31e7.png)
Software engineering job design (1): [personal project] implements a log view page

Sell notes | brief introduction to video text pre training

每次启动项目的服务,电脑竟然乖乖的帮我打开了浏览器,100行源码揭秘!

Introduction to data warehouse
随机推荐
MongoDB-在windows电脑本地安装一个mongodb的数据库
夏日的晚会
Hcip/hcie Routing & Switching / datacom Reference Dictionary Series (19) comprehensive summary of PKI knowledge points (public key infrastructure)
Msp430f5529 MCU reads gy-906 infrared temperature sensor
Arduino UNO通过电容的直接检测实现简易触摸开关
flutter系列之:flutter中的变形金刚Transform
[黑苹果系列] M910x完美黑苹果系统安装教程 – 2 制作系统U盘-USB Creation
Mongodb- install a mongodb database locally on the windows computer
Squid代理服务器(缓存加速之Web缓存层)
[black apple series] m910x perfect black apple system installation tutorial – 2 making system USB disk -usb creation
最新MySQL高级SQL语句大全
[microservices sentinel] sentinel data persistence
zotero文献管理工具安装使用
表单form 和 表单元素(input、select、textarea等)
吴恩达《机器学习》课程总结(14)_降维
Code neatness -- function
单片机之IIC通信协议「建议收藏」
Eliminate gaps around El image images
request对象、response对象、session对象
Sentinel