贝尔福特曼(?)的方式相当于每次将所有与源点直接相连的点的影响区域向两边各扩展一格。显然每个点在过程中最多更新其他点一次且这些点构成一段连续区间。这个东西二分st表查一下就可以了。注意某一轮中两点都更新某节点的情况。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cassert>
using namespace std;
#define ll long long
#define N 200010
#define inf 100000000000000000ll
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<''||c>'')) c=getchar();return c;}
int gcd(int n,int m){return m==?n:gcd(m,n%m);}
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
int T,n,m,a[N],L[N],R[N],lg2[N];
ll s[N],f[N][],g[N][];
struct data
{
int x,y;
bool operator <(const data&a) const
{
return x<a.x;
}
}edge[N];
ll query(int x,int y,ll f[N][])
{
if (x>y) return inf;
return min(f[x][lg2[y-x+]],f[y-(<<lg2[y-x+])+][lg2[y-x+]]);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj5308.in","r",stdin);
freopen("bzoj5308.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
n=read(),T=read();
for (int i=;i<n;i++) a[i]=read();
for (int i=;i<=n;i++) s[i]=s[i-]+a[i-];
for (int i=;i<=n;i++)
{
lg2[i]=lg2[i-];
if ((<<lg2[i])<=i) lg2[i]++;
}
while (T--)
{
int m=read();
for (int i=;i<=m;i++) edge[i].x=read(),edge[i].y=read();
sort(edge+,edge+m+);
for (int i=;i<=m;i++) f[i][]=edge[i].y-s[edge[i].x];
for (int j=;j<=lg2[m];j++)
for (int i=;i<=m;i++)
f[i][j]=min(f[i][j-],f[min(i+(<<j-),n)][j-]);
for (int i=;i<=m;i++) g[i][]=edge[i].y+s[edge[i].x];
for (int j=;j<=lg2[m];j++)
for (int i=;i<=m;i++)
g[i][j]=min(g[i][j-],g[min(i+(<<j-),n)][j-]);
ll ans=;
for (int i=;i<=m;i++)
{
int l=,r=edge[i].x-;L[i]=r+;
while (l<=r)
{
int mid=l+r>>,p=max(,mid-(edge[i].x-mid));
int x=lower_bound(edge+,edge+i+,(data){mid,})-edge,y=lower_bound(edge+,edge+i+,(data){p,})-edge;
if (query(y,x-,f)+s[mid]>g[i][]-s[mid]&&query(x,i-,g)>g[i][]) L[i]=mid,r=mid-;
else l=mid+;
}
l=edge[i].x+,r=n;R[i]=l-;
while (l<=r)
{
int mid=l+r>>,p=min(n,mid+(mid-edge[i].x)-);
int x=lower_bound(edge+i,edge+m+,(data){mid+,})-edge-,y=lower_bound(edge+i,edge+m+,(data){p+,})-edge-;
if (query(i+,x,f)>f[i][]&&query(x+,y,g)-s[mid]>f[i][]+s[mid]
&&(y==m||edge[y+].x!=p+||g[y+][]-s[mid]>=f[i][]+s[mid])) R[i]=mid,l=mid+;
else r=mid-;
}
}
for (int i=;i<=m;i++) ans+=R[i]-L[i]+;
printf(LL,ans);
}
return ;
}
05-11 11:36
查看更多