http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22254
You are given a sequence of N integers A1, A2 .. AN. (-10000 <= Ai <= 10000, N <= 50000)
Let Si denote the sum of A1..Ai. You need to apply M (M <= 50000) operations:
0 x y k: increase all integers from Ax to Ay by k(1 <= x <= y <= N, -10000 <= k <= 10000).
1 x y: ask for max{ Si | x <= i <= y }.(1 <= x <= y <= N)
对于区间增值[L,R,k],
若 L<=i<=R,那么 S[i]=S[i]+k∗(i−L+1)=S[i]+k∗i+k∗(1−L)
若 i>R,那么 S[i]=S[i]+(R−L+1)
可以写成这个形式: S[i]=X[i]∗i+Y[i]
将序列分成N−−√ 块,每块记录两个标记 sumk 和 add,
表示对于这个块内元素都加值 sumk∗i+add
对于区间加值的操作,修改整块的标记,对于不在整块的部分暴力修改即可。
对于询问操作,考虑得到单块的答案,观察 S[i]=sumk∗i+add,
可以发现,它可以写成斜率形式,可以用斜率优化来得到单块内的S[i]最大值,在凸包上二分斜率或者三分点即可。
但是斜率优化需要维护凸包,区间加值操作时对于修改了S[i]的块重新建造凸包即可。
时间复杂度:O(M∗N−−√∗logN−−√)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <string>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <utility>
#include <iostream>
#include <algorithm>
template<class Num>void read(Num &x)
{
char c; int flag = 1;
while((c = getchar()) < '0' || c > '9')
if(c == '-') flag *= -1;
x = c - '0';
while((c = getchar()) >= '0' && c <= '9')
x = (x<<3) + (x<<1) + (c-'0');
x *= flag;
return;
}
template<class Num>void write(Num x)
{
if(x < 0) putchar('-'), x = -x;
static char s[20];int sl = 0;
while(x) s[sl++] = x%10 + '0',x /= 10;
if(!sl) {putchar('0');return;}
while(sl) putchar(s[--sl]);
}
const double eps = 1e-3;
const int maxn = 50005, maxS = 300, maxB = 300;
const long long LINF = 1LL<<60;
#define REP(__x,__st,__ed) for(int __x = (__st); __x <= (__ed); __x++)
int n, m, S, B, a[maxn];
long long sum[maxn], sk[maxB], c[maxB];
struct type_block
{
int id, ll, rr;
int t[maxS], len;
#define check(x,y,z) ((y - x)*(sum[z] - sum[y]) - (sum[y] - sum[x])*(z - y) >= 0)
void build()
{
len = 0;
for(int i = ll; i <= rr; i++)
{
while(len >= 2 && check(t[len-1], t[len], i)) t[len--] = 0;
t[++len] = i;
}
}
#undef check
void init(int __id)
{
id = __id;
ll = (id - 1)*S + 1;
rr = std::min(id * S, n);
build();
}
long long calcu()
{
int l = 1, r = len;
long long c1, c2, ret = -LINF;
while(r - l >= 5)
{
int mid1 = (l + r)>>1, mid2 = (mid1 + r)>>1;
c1 = sum[t[mid1]] + t[mid1]*sk[id];
c2 = sum[t[mid2]] + t[mid2]*sk[id];
if(c1 < c2) l = mid1; else r = mid2;
}
for(int i = l; i <= r; i++)
ret = std::max(ret, sum[t[i]] + t[i]*sk[id]);
return ret + c[id];
}
}block[maxB];
int main()
{
#ifndef ONLINE_JUDGE
freopen("spoj.in","r",stdin);
freopen("spoj.out","w",stdout);
#endif
read(n);
S = sqrt(n) + eps, B = n/S + ((n%S)?1:0);
REP(i, 1, n) read(a[i]), sum[i] = sum[i-1] + a[i];
REP(i, 1, B) block[i].init(i);
read(m);
REP(i, 1, m)
{
int op, l, r, k;
int l_blo, r_blo, st, ed;
read(op), read(l), read(r);
l_blo = (l - 1)/S + 1;
r_blo = (r - 1)/S + 1;
st = block[l_blo + 1].ll;
ed = block[r_blo - 1].rr;
if(!op)
{
read(k);
if(l_blo == r_blo)
{
REP(i, l, r) sum[i] += (i - l + 1)*k;
ed += S, ed = std::min(ed, n);
REP(i, r + 1, ed) sum[i] += (r - l + 1)*k;
REP(i, r_blo + 1, B) c[i] += (r - l + 1)*k;
block[l_blo].build();
}
else
{
REP(i, l, st - 1) sum[i] += (i - l + 1)*k;
REP(i, l_blo + 1, r_blo - 1) sk[i] += k, c[i] += (1 - l)*k;
REP(i, ed + 1, r) sum[i] += (i - l + 1)*k;
ed += S, ed = std::min(ed, n);
REP(i, r + 1, ed) sum[i] += (r - l + 1)*k;
REP(i, r_blo + 1, B) c[i] += (r - l + 1)*k;
block[l_blo].build();
block[r_blo].build();
}
}
else
{
long long ans = -LINF;
if(l_blo == r_blo)
{
REP(i, l, r) ans = std::max(sum[i] + i*sk[l_blo] + c[l_blo], ans);
}
else
{
REP(i, l, st - 1) ans = std::max(sum[i] + i*sk[l_blo] + c[l_blo], ans);
REP(i, l_blo + 1, r_blo - 1) ans = std::max(block[i].calcu(), ans);
REP(i, ed + 1, r) ans = std::max(sum[i] + i*sk[r_blo] + c[r_blo], ans);
}
write(ans), puts("");
}
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。