# Description

$$n\le 40000$$

# Solution

### Hall定理：

$(s[j] - s[i]) - (a[j] - b[i]) \geq 0$

$$f[i] = s[i] - a[i], g[i] = s[i] - b[i]$$

$f[r...n] -= k\\ g[l...n] -= k$

$i\in [0, l - 1], j\in [r + 1, n]\\ f[j] - k - g[i] \geq 0\\ k\le min(f[j]) - max(g[i])\\ k\le min(f[r+1...n]) - max(g[1...l-1])$

$$k$$$$min(f[r+1...n]) - max(g[0...l-1])$$ 时最优。

## Code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <fstream>

typedef long long LL;
typedef unsigned long long uLL;

#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define MP(x, y) std::make_pair(x, y)
#define DE(x) cerr << x << endl;
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define GO cerr << "GO" << endl;
#define rep(i, a, b) for (register int (i) = (a); (i) <= (b); ++(i))

using namespace std;

inline void proc_status()
{
ifstream t("/proc/self/status");
cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>()) << endl;
}
{
register int x = 0; register int f = 1; register char c;
while (!isdigit(c = getchar())) if (c == '-') f = -1;
while (x = (x << 1) + (x << 3) + (c xor 48), isdigit(c = getchar()));
return x * f;
}
template<class T> inline void write(T x)
{
static char stk[30]; static int top = 0;
if (x < 0) { x = -x, putchar('-'); }
while (stk[++top] = x % 10 xor 48, x /= 10, x);
while (putchar(stk[top--]), top);
}
template<typename T> inline bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }

const int maxN = 4e4;

int n, m;
int s[maxN + 2], K[maxN + 2];

#define ls (x << 1)
#define rs (x << 1 | 1)
#define Rson rs, mid + 1, r
#define Lson ls, l, mid
int f[maxN << 2], g[maxN << 2], tagG[maxN << 2], tagF[maxN << 2];

void pushup(int x)
{
f[x] = min(f[ls], f[rs]);
g[x] = max(g[ls], g[rs]);
}

void build(int x, int l, int r)
{
if (l == r) { f[x] = g[x] = s[l]; return; }
int mid = l + r >> 1;
build(Lson); build(Rson);
pushup(x);
}

void pushG(int x, int k)
{
tagG[x] += k;
g[x] += k;
}

void pushF(int x, int k)
{
tagF[x] += k;
f[x] += k;
}

void pushdown(int x)
{
if (tagG[x] != 0)
{
pushG(ls, tagG[x]);
pushG(rs, tagG[x]);
tagG[x] = 0;
}
if (tagF[x] != 0)
{
pushF(ls, tagF[x]);
pushF(rs, tagF[x]);
tagF[x] = 0;
}
}

void addF(int x, int l, int r, int L, int R, int k)
{
if (L <= l and r <= R) { return pushF(x, k); }
int mid = l + r >> 1;
pushdown(x);
if (L <= mid) addF(Lson, L, R, k);
if (R > mid) addF(Rson, L, R, k);
pushup(x);
}

void addG(int x, int l, int r, int L, int R, int k)
{
if (L <= l and r <= R) { return pushG(x, k); }
int mid = l + r >> 1;
pushdown(x);
if (L <= mid) addG(Lson, L, R, k);
if (R > mid) addG(Rson, L, R, k);
pushup(x);
}

int queryG(int x, int l, int r, int L, int R)
{
if (L <= l and r <= R) { return g[x]; }
int mid = l + r >> 1;
pushdown(x);
int ans = -0x3f3f3f3f;
if (L <= mid) chkmax(ans, queryG(Lson, L, R));
if (mid < R) chkmax(ans, queryG(Rson, L, R));
return ans;
}

int queryF(int x, int l, int r, int L, int R)
{
if (L <= l and r <= R) { return f[x]; }
int mid = l + r >> 1;
pushdown(x);
int ans = 0x3f3f3f3f;
if (L <= mid) chkmin(ans, queryF(Lson, L, R));
if (mid < R) chkmin(ans, queryF(Rson, L, R));
return ans;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("stone.in", "r", stdin);
freopen("stone.out", "w", stdout);
#endif
int x, y, z, P;
scanf("%d", &n);
scanf("%d%d%d%d", &x, &y, &z, &P);
for (int i = 1; i <= n; ++i)
s[i] = s[i - 1] + ((i - x) * (i - x) + (i - y) * (i - y) + (i - z) * (i - z)) % P;
scanf("%d", &m);
scanf("%d%d%d%d%d%d", &K[1], &K[2], &x, &y, &z, &P);
for (int i = 3; i <= m; ++i)
K[i] = (1ll * x * K[i - 1] + 1ll * y * K[i - 2] + z) % P;
build(1, 0, n);
for (int i = 1; i <= m; ++i)
{
int L, R, k;
scanf("%d%d", &L, &R);
printf("%d\n", k = min(queryF(1, 0, n, R, n) - queryG(1, 0, n, 0, L - 1), K[i]));
addF(1, 0, n, R, n, -k);
addG(1, 0, n, L, n, -k);
}
return 0;
}

posted @ 2019-08-31 17:21  茶Tea  阅读(291)  评论(0编辑  收藏  举报