1. 程式人生 > >CodeForces 862E Mahmoud and Ehab and the function 暴力,二分

CodeForces 862E Mahmoud and Ehab and the function 暴力,二分

push_back fin style name https img print ret esp

CodeForces 862E

題意:技術分享 給出長度為 n 的數組 a[] 和長度為 m 的數組 b[],有 q 個詢問,每次詢問把區間標號 [l,r] 的 a[] 加上 x, 每次求最小的 f(j) 。

tags: 發現有奇偶規律,每次詢問其實只會加或減一個 x ,而 b[] 相加減可以前綴和預處理出來。 即 s(a) 可 O(1) 求出, s(b) 是固定的一些值,要求最小的 | s(a)+s(b) | ,只要在 s(b) 中二分 - s(a) 即可。

#include<bits/stdc++.h>
using namespace std;
#pragma
comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i) #define mes(a,b) memset(a,b,sizeof(a)) #define INF 0x3f3f3f3f #define MP make_pair #define PB push_back #define fi first #define se second typedef long long ll;
const int N = 200005; int n, m, q, cnt; ll a[N], b[N], s1, c[N], sum[N]; ll getMin(ll s) { int pos = lower_bound(c, c+1+cnt, -s)-c; return min(abs(s+c[pos]), abs(s+c[pos-1])); } int main() { scanf("%d%d%d", &n, &m, &q); rep(i,1,n) { scanf("%lld", &a[i]);
if(i&1) s1+=a[i]; else s1-=a[i]; } rep(i,1,m) { scanf("%lld", &b[i]); if(i>1) sum[i]=sum[i-2]; sum[i]+=b[i]; } c[cnt]=-1e18; rep(i,n,m) { ll s2=sum[i], s3=sum[i-1]; int pos = i-n+1; if(n&1) { s2 -= sum[max(pos-2,0)]; s3 -= sum[max(pos-1,0)]; c[++cnt] = -s2+s3; } else { s2 -= sum[max(pos-1,0)]; s3 -= sum[max(pos-2,0)]; c[++cnt] = s2-s3; } } c[++cnt]=1e18; sort(c, c+1+cnt); printf("%lld\n", getMin(s1) ); ll l, r, x; rep(i,1,q) { scanf("%lld%lld%lld", &l, &r, &x); if((r-l+1)&1) { if(l&1) s1+=x; else s1-=x; } printf("%lld\n", getMin(s1) ); } return 0; }

CodeForces 862E Mahmoud and Ehab and the function 暴力,二分