当前位置:网站首页>Deltix Round, Summer 2021 E. Equilibrium
Deltix Round, Summer 2021 E. Equilibrium
2022-04-21 15:27:00 【合金Bunny酱】
洛谷链接:Equilibrium - 洛谷
解法:设一个新数组c满足c[i] = a[i]-b[i],再对c数组求前缀和,那么c[i]就是sum[i]的差分,那么我们的目标就是让[l, r]区间内所有c[i]都变成0,即sum[l-1] = sum[l] = .... = sum[r-1] = sum[r],根据题目要求,我们是让c[i]++, c[j]--,其中i<j,那么造成的效果就是sum[i] ~ sum[j-1]都+1,其他不变。
容易发现,这种操作只能让sum增加,而不能减少,并且sum[l-1]和sum[r]都是不能被改变的。
所以得到有解的两个条件:sum[l-1] == sum[r],以及中间的所有sum值都必须<=0(操作只能加,不能减,要是一开始就>0,那肯定没救了)。有解之后,最少操作次数是多少?显然就是看sum值最小的那个点了,答案就是sum[l-1] - minsum。
#include <bits/stdc++.h>
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
using namespace std;
const int N = 1e5+5;
int n,q,a[N],b[N],sum[N],l,r;
int mn[N][40], mx[N][40];
inline void build(){
for(int i=1; (1<<i)<=n; i++)
for(int j=1; j+(1<<i)-1 <= n; j++){
mn[j][i] = min(mn[j][i-1], mn[j+(1<<(i-1))][i-1]);
mx[j][i] = max(mx[j][i-1], mx[j+(1<<(i-1))][i-1]);
}
}
inline int mnVal(int l,int r){
int k=(int)(log(r-l+1)/log(2));
return min(mn[l][k], mn[r-(1<<k)+1][k]);
}
inline int mxVal(int l,int r){
int k=(int)(log(r-l+1)/log(2));
return max(mx[l][k], mx[r-(1<<k)+1][k]);
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin>>n>>q; FOR(i,1,n) cin>>a[i]; FOR(i,1,n) cin>>b[i]; //input
FOR(i,1,n) a[i]-=b[i], sum[i]=sum[i-1]+a[i]; //预处理,求前缀和
FOR(i,1,n) mn[i][0] = mx[i][0] = sum[i]; //mn,mx初始化
build(); //建立st表
FOR(i,1,q){
cin>>l>>r;
if(sum[r]!=sum[l-1] || mxVal(l,r)>sum[l-1]) {cout<<-1<<'\n'; continue;}
cout<<sum[l-1]-mnVal(l,r)<<'\n';
}
}
版权声明
本文为[合金Bunny酱]所创,转载请带上原文链接,感谢
https://blog.csdn.net/bunny_1024/article/details/124310403
边栏推荐
- 107页企业数字化转型规划设计
- AcWing 1866. Fence painting (section crossing)
- 【二分查找-简单】367. 有效的完全平方数
- Design and implementation of public welfare website based on JSP
- MySQL
- 企业邮箱怎么登录?企业邮箱登录方法
- [binary search - simple] sword finger offer II 072 take a square root
- JD cloud has launched cloud computing to create an unbounded office experience for the future
- Hand in hand to teach you to realize hand-painted style graphics
- 72页互联网智慧园区解决方案
猜你喜欢

华为电力PON配网解决方案

终极套娃 2.0|云原生 PaaS 平台的可观测性实践分享

Introduction to openlayers (I)

解决I.MX6U驱动移植LED闪烁
![[Unity] error CS0433: The type ‘Task‘ exists in both Unity.Tasks,....](/img/17/3a0b2e52336bc702ff3b38420e9e6a.png)
[Unity] error CS0433: The type ‘Task‘ exists in both Unity.Tasks,....

AcWing 1866. Fence painting (section crossing)

Plug in development practice of a simple annotation Library

AcWing1800. Do not do the last (enumeration)

Jetpack Compose使用自定义操作符实现绘制五角星效果

Smart Park Digital financing - Digital enabling operation management platform solution
随机推荐
49页企业数字化转型案例及常用工具企业数字化转型思路
企业邮箱怎么登录?企业邮箱登录方法
Ultimate doll 2.0 | observable practice sharing of cloud native PAAS platform
[binary search - simple] 69 Square root of X
On the import and export of browser bookmarks
GLASS:用于子图表示学习的 GNN 标签技巧
【二分查找-简单】剑指 Offer II 068. 查找插入位置
Embedded development: three skills of reusing development board for testing
What is an email address? Easy to use email registration application
105 page digital twin city information model CIM platform construction technical scheme
Mysql
JD cloud has launched cloud computing to create an unbounded office experience for the future
Mysql
[binary search - medium] 1855 Maximum distance of subscript alignment (need to review double pointer method)
Hanoi tower game and recursion
Acwing 1775 Lost cattle (simulated)
华为电力PON配网解决方案
返璞归真,多方安全计算要回归到“安全”的本源考虑
Dry goods | environmental problems or chronic difficulties in testing? It's easy to do it in two steps
Ultimate doll 2.0 | observable practice sharing of cloud native PAAS platform