NEERC 17 G.The Great Wall

NEERC 17 G.The Great Wall

Solution

这题的第一步 t r i c k trick trick是:我们注意到取 a , b , c a,b,c a,b,c的集合两两不交且并集为 U U U,因此确定了 b , c b,c b,c之后可以简单地唯一确定 a a a,于是我们通过让 b i − = a i , c i − = a i , A n s + = ∑ a i b_i-=a_i,c_i-=a_i,Ans+=\sum a_i bi=ai,ci=ai,Ans+=ai来削减一维。

然后令 b i ′ = ∑ j = 1 i b i b'_i=\sum_{j=1}^ib_i bi=j=1ibi c i ′ = ∑ j = 1 i c i c'_i=\sum_{j=1}^ic_i ci=j=1ici,二分答案 k k k

  • x + r − 1 ≥ y x+r-1\geq y x+r1y s u m = c x + r − 1 ′ − c y − 1 ′ + b y − 1 ′ − b x − 1 ′ + b y + r − 1 ′ − b x + r − 1 ′ = ( c x + r − 1 ′ − b x − 1 ′ − b x + r − 1 ′ ) + ( − c y − 1 ′ + b y − 1 ′ + b y + r − 1 ′ ) ≤ k sum=c'_{x+r-1}-c'_{y-1}+b'_{y-1}-b'_{x-1}+b'_{y+r-1}-b'_{x+r-1}=(c'_{x+r-1}-b'_{x-1}-b'_{x+r-1})+(-c'_{y-1}+b'_{y-1}+b'_{y+r-1})\leq k sum=cx+r1cy1+by1bx1+by+r1bx+r1=(cx+r1bx1bx+r1)+(cy1+by1+by+r1)k,可以简单地用二维数点解决。
  • x + r − 1 < y x+r-1<y x+r1<y s u m = ( b x + r − 1 ′ − b x − 1 ′ ) + ( b y + r − 1 ′ − b y − 1 ′ ) ≤ k sum=(b'_{x+r-1}-b'_{x-1})+(b'_{y+r-1}-b'_{y-1})\leq k sum=(bx+r1bx1)+(by+r1by1)k,同样可以简单地用二维数点解决。

时间复杂度 O ( n l g 2 n ) O(n lg^2n) O(nlg2n)

Code

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=998244353;
const int MAXN=600005;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline ll read()
{
	ll f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
int n,r,s[MAXN];
ll p[MAXN],b[MAXN],c[MAXN],B[MAXN],a[MAXN],k;
void add(int x,int y) { if (!x) return; for (;x<=n-r+1;x+=x&(-x)) s[x]+=y; }
int query(int x) { upmin(x,n-r+1); int ans=0; for (;x;x-=x&(-x)) ans+=s[x]; return ans; }
ll solve1(ll x)
{
	ll ans=0;
	for (int i=1;i<=n-r+1;i++) B[i]=p[i]=-c[i-1]+b[i-1]+b[i+r-1];
	sort(p+1,p+n-r+2);
	for (int i=1;i<=n-r+1;i++) B[i]=lower_bound(p+1,p+n-r+2,B[i])-p;
	for (int i=1;i<=r-1;i++) add(B[i],1);
	for (int i=1;i<=n-r+1;i++)
	{
		add(B[i],-1);
		if (i<=n-r-r+2) add(B[i+r-1],1);
		ans+=query(upper_bound(p+1,p+n-r+2,x-(c[i+r-1]-b[i-1]-b[i+r-1]))-p-1);
	}
	return ans;
}
ll solve2(ll x)
{
	ll ans=0;
	for (int i=1;i<=n-r+1;i++) B[i]=p[i]=b[i+r-1]-b[i-1];
	sort(p+1,p+n-r+2);
	for (int i=1;i<=n-r+1;i++) B[i]=lower_bound(p+1,p+n-r+2,B[i])-p;
	for (int i=r;i<=n-r+1;i++) add(B[i],1);
	for (int i=1;i<=n-r+1;i++)
	{
		if (i<=n-r-r+2) add(B[i+r-1],-1);
		ans+=query(upper_bound(p+1,p+n-r+2,x-(b[i+r-1]-b[i-1]))-p-1);
	}
	return ans;
}
signed main()
{
	ll sum=0;
	n=read(),r=read(),k=read();
	for (int i=1;i<=n;i++) a[i]=read(),sum+=a[i];
	for (int i=1;i<=n;i++) b[i]=read()-a[i]+b[i-1];
	for (int i=1;i<=n;i++) c[i]=read()-a[i]+c[i-1];
	
	ll l=0,r=1000000ll*n;
	while (l<r)
	{
		ll mid=(l+r)>>1,t=solve1(mid)+solve2(mid);
		if (t>=k) r=mid;
		else l=mid+1;
	}
	printf("%lld\n",r+sum);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值