CF862E Mahmoud and Ehab and the function

首先 \(f(j)=\displaystyle|\sum_{i=1}^{n}(-1)^{i-1}a_i+(-1)^ib_{i+j}|=|\sum_{i=1}^{n}(-1)^{i-1}a_i+\sum_{i=1}^{n}(-1)^{i}b_{i+j}|\)

我们先处理出 suma,表示 \(\displaystyle\sum_{i=1}^{n}(-1)^{i-1}a_i\) 的值。

然后处理出 sumb[i],这个是等于 \(-(sumb[i-1]+b_i)+b_{i+n}* n\) 是否为奇数,为奇数就乘 \(-1\),否则乘 \(1\)

然后我们发现对于每次的更新,只有 \(r-l+1\) 为奇数时,且 \(l\) 也为奇数时,suma 才会发生变化,所以对于每次更新就判断一下就行了。

那么对于 \(f(j)\) 的最小值,我们二分一下,在 sumb 中找最接近 suma 的就行了。

代码:

#include <bits/stdc++.h>
#define reg register
#define ll long long
#define int long long
#define ull unsigned long long
#define db double
#define pi pair<int, int>
#define pl pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vpi vector<pi>
#define vpl vector<pl>
#define pb push_back
#define er erase
#define SZ(x) (int) x.size()
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mkp make_pair
using namespace std;
char ch, B[1 << 20], *S = B, *T = B;
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 20, stdin), S == T) ? 0 : *S++)
#define isd(c) (c >= '0' && c <= '9')
int aa, bb;
int rd() {
  while(ch = getc(), !isd(ch) && ch != '-');
  ch == '-' ? aa = bb = 0 : (aa = ch - '0', bb = 1);
  while(ch = getc(), isd(ch))
    aa = aa * 10 + ch - '0';
  return bb ? aa : -aa;
}
const int MAXN = 1e5 + 10;
int n, m, q, a[MAXN], b[MAXN], suma, sumb[MAXN];
int ask(int l, int r, int x, int delta) {
  if((r - l) % 2 == 0) {
    if(l % 2 == 1)
      suma += x;
    else
      suma -= x;
  }
  int *pos = lb(sumb, sumb + delta, -suma);
  int ans = 1e18 + 10;
  if(pos != sumb + delta)
    ans = min(ans, abs(suma + *pos));
  if(pos != sumb) {
    --pos;
    ans = min(ans, abs(suma + *pos));
  }
  return ans;
}
inline void work() {
  scanf("%lld%lld%lld", &n, &m, &q);
  suma = 0;
  for(reg int i = 1, mul = 1; i <= n; ++i, mul *= -1) {
    scanf("%lld", &a[i]);
    suma += a[i] * mul;
    // printf("%d ", suma);
  }
  // puts("");
  int delta = m - n + 1;
  sumb[0] = 0;
  for(reg int i = 1, mul = -1; i <= m; ++i, mul *= -1) {
    scanf("%lld", &b[i]);
    if(i <= n)
      sumb[0] += b[i] * mul;
  }
  for(reg int i = 1; i + n <= m; ++i)
    sumb[i] = -(sumb[i - 1] + b[i]) + b[i + n] * ((n & 1) ? -1 : 1);
  // for(reg int i = 1; i <= m; ++i)
    // printf("%d ", &b[i]);
  // puts("");
  sort(sumb, sumb + delta);
  printf("%lld\n", ask(1, 1, 0, delta));
  for(reg int i = 1; i <= q; ++i) {
    int l, r, x;
    scanf("%lld%lld%lld", &l, &r, &x);
    printf("%lld\n", ask(l, r, x, delta));
  }
}
signed main() {
  // freopen("input.txt", "r", stdin);
  work();
  return 0;
}
posted @ 2020-09-12 22:48  Nylch  阅读(120)  评论(0编辑  收藏  举报