2019.03.04 bzoj5308: [Zjoi2018]胖(二分答案+st表)

传送门

想题5分钟调题两小时系列 其实还是我tcl

读完题之后自然会知道一个关键点能够更新的点是一段连续的区间,于是我们对于每个点能到的左右区间二分答案,用ststst表维护一下查询即可。

代码:

#include<bits/stdc++.h>
#define ri register int
using namespace std;
inline int read(){
	int ans=0;
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
	return ans;
}
const int N=2e5+5;
typedef long long ll;
ll s1[N],s2[N],ans;
int mp[N],a[N],n,m,k;
struct pot{int a;ll w;friend inline bool operator<(const pot&a,const pot&b){return a.a<b.a;}}b[N];
struct ST{
	ll st[N][20];
	int Log[N];
	inline void init(){Log[0]=-1;for(ri i=1;i<=n;++i)Log[i]=Log[i>>1]+1;}
	inline void Pre(){
		for(ri i=1;i<=k;++i)st[i][0]=b[i].w;
		for(ri j=1;j<=Log[k];++j)for(ri i=1;i<=k;++i)st[i][j]=min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
	}
	inline ll query(int l,int r){
		l=lower_bound(mp+1,mp+k+1,max(l,1))-mp;
		r=upper_bound(mp+1,mp+k+1,min(r,n))-mp-1;
		if(l>r)return 1e18;
		int k=Log[r-l+1];
		return min(st[l][k],st[r-(1<<k)+1][k]);
	}
}pre,suf;
inline bool check1(int mid,int x){
	if(mid==x)return 1;
	ll d1=pre.query(mid*2-x+1,mid)+s1[mid],d2=suf.query(mid,x-1)-s1[mid],d3=suf.query(x,x)-s1[mid];
	if(d1<=d3||d2<=d3)return 0;
	return mid*2-x>=1?pre.query(mid*2-x,mid*2-x)+s1[mid]>=d3:1;
}
inline bool check2(int x,int mid){
	if(x==mid)return 1;
	ll d1=suf.query(mid,mid*2-x-1)-s1[mid],d2=pre.query(x+1,mid)+s1[mid],d3=pre.query(x,x)+s1[mid];
	if(d1<=d3||d2<=d3)return 0;
	return mid*2-x<=n?suf.query(mid*2-x,mid*2-x)-s1[mid]>d3:1;
}
inline void solve(int id){
	int l=1,r=b[id].a,res1=0,res2=0,mid;
	while(l<=r){if(check1((mid=l+r>>1),b[id].a))r=mid-1,res1=mid;else l=mid+1;}
	l=b[id].a,r=n;
	while(l<=r){if(check2(b[id].a,(mid=l+r>>1)))l=mid+1,res2=mid;else r=mid-1;}
	ans+=res2-res1+1;
}
int main(){
	n=read(),m=read();
	pre.init(),suf.init();
	for(ri i=2;i<=n;++i)s1[i]=s1[i-1]+(a[i]=read());
	while(m--){
		ans=0,k=read();
		for(ri i=1;i<=k;++i)b[i].a=read(),b[i].w=read()-s1[b[i].a];
		sort(b+1,b+k+1);
		for(ri i=1;i<=k;++i)mp[i]=b[i].a;
		pre.Pre();
		for(ri i=1;i<=k;++i)b[i].w+=s1[b[i].a]<<1;
		suf.Pre();
		for(ri i=1;i<=k;++i)solve(i);
		cout<<ans<<'\n';
	}
	return 0;
}
上一篇:Chapter_4_JAVA作业


下一篇:Maven创建工程 WEB