题解:
因为n,m很大
所以复杂度应该是和m相关的
考虑到每个点的影响区间是连续的
就很简单了
区间查询最小值线段树维护(st表也可以)
然后注意一下不要重复算一个就可以了
max函数用template<class T> 不能与原来min重名
代码:
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define IL inline
#define rint register int
#define rep(i,h,t) for (rint i=h;i<=t;i++)
#define dep(i,t,h) for (rint i=t;i>=h;i--)
const int N=3e5;
const ll INF=1e18;
const int N2=1e7;
ll dis[N],sum1[N],sum2[N];
int n,m,cnt;
struct re{
int a;
ll b;
};
vector<int> ve;
ll data1[N2],data2[N2];
int ls[N2],rs[N2];
char ss[<<],*A=ss,*B=ss;
IL char gc()
{
return (A==B&&(B=(A=ss)+fread(ss,,<<,stdin),A==B)?EOF:*A++);
}
template<class T>void read(T &x)
{
rint f=,c; while (c=gc(),c<||c>) if (c=='-') f=-; x=c^;
while (c=gc(),<c&&c<) x=(x<<)+(x<<)+(c^); x*=f;
}
char sr[<<],z[]; int C=-,Z;
template <class T> void wer(T x)
{
if (x<) sr[++C]='-',x=-x;
while (z[++Z]=x%+,x/=);
while (sr[++C]=z[Z],--Z); sr[++C]='\n';
}
template<class T> T MAX(T x,T y)
{
if (x>y) return(x); else return(y);
}
template<class T> T MIN(T x,T y)
{
if (x<y) return(x); else return(y);
}
IL void swap(int &x,int &y)
{
int tmp=x; x=y; y=tmp;
}
void clear()
{
cnt=;
for (int i=;i<ve.size();i++)
{
int x=ve[i];
data1[x]=INF; data2[x]=INF; ls[x]=; rs[x]=;
}
ve.clear();
}
void updata(int x)
{
data1[x]=MIN(data1[ls[x]],data1[rs[x]]);
data2[x]=MIN(data2[ls[x]],data2[rs[x]]);
}
void change(int &x,int h,int t,int pos,ll k)
{
if (!x) x=++cnt;
ve.push_back(x);
if (h==t)
{
data1[x]=MIN(data1[x],sum1[pos-]+k);
data2[x]=MIN(data2[x],sum2[pos]+k);
return;
}
int mid=(h+t)/;
if (pos<=mid) change(ls[x],h,mid,pos,k);
else change(rs[x],mid+,t,pos,k);
updata(x);
}
ll query1(int x,int h,int t,int h1,int t1)
{
if (!x) return(INF);
if (h1<=h&&t<=t1) return(data1[x]);
ll ans=INF;
int mid=(h+t)/;
if (h1<=mid) ans=query1(ls[x],h,mid,h1,t1);
if (mid<t1) ans=MIN(ans,query1(rs[x],mid+,t,h1,t1));
return(ans);
}
ll query2(int x,int h,int t,int h1,int t1)
{
if (!x) return(INF);
if (h1<=h&&t<=t1) return(data2[x]);
ll ans=INF;
int mid=(h+t)/;
if (h1<=mid) ans=query2(ls[x],h,mid,h1,t1);
if (mid<t1) ans=MIN(ans,query2(rs[x],mid+,t,h1,t1));
return(ans);
}
IL ll check1(int h,int t,int x)
{
h=MAX(h,); t=MIN(t,n);
return(query2(,,n,h,t)-sum2[x]);
}
IL ll check2(int h,int t,int x)
{
h=MAX(h,); t=MIN(t,n);
return(query1(,,n,h,t)-sum1[x-]);
}
int main()
{
read(n); read(m);
rep(i,,n-) read(dis[i]);
rep(i,,n-) sum1[i]=dis[i],sum1[i]+=sum1[i-];
dep(i,n-,) sum2[i]=dis[i],sum2[i]+=sum2[i+];
rep(i,,N2-) data1[i]=INF,data2[i]=INF;
rep(i,,m)
{
int x,y;
ll ans=,z;
read(x);
vector<re> ve1;
ve1.push_back((re){,});
clear();
int root=;
rep(j,,x)
{
read(y); read(z);
change(root,,n,y,z);
ve1.push_back((re){y,z});
}
rep(j,,x)
{
int y=ve1[j].a;
ll z=ve1[j].b;
int h=y,t=n;
while (h<t)
{
int mid=(h+t+)/;
ll jl=sum1[mid-]-sum1[y-]+z;
if (MIN(check1(y+,mid,mid),check2(mid,*mid-y-,mid))>jl&&
check2(*mid-y,*mid-y,mid)>=jl) h=mid;
else t=mid-;
}
ans+=t-y;
h=,t=y;
while (h<t)
{
int mid=(h+t)/;
ll jl=sum1[y-]-sum1[mid-]+z;
if (MIN(check2(mid,y-,mid),check1(*mid-y+,mid,mid))>jl&&
check1(*mid-y,*mid-y,mid)>jl) t=mid;
else h=mid+;
}
ans+=y-h+;
}
wer(ans);
}
fwrite(sr,,C+,stdout);
return ;
}