[CSP-S Simulation Test]: sequence (binary tree arrays answers +)

Portal Title (title 98 inside)


Input Format

  A first line integer $ n $, $ second row integers n-$ $ a_1 \ sim a_n $, $ third row integers n-$ $ b_1 \ sim b_n $.


Output Format

  A line integer $ \ max (r-l + 1) $. Ensure that at least a range of conditions are met.


Sample

Sample input:

5
2 -4 1 2 -2
-2 3 1 -3 1

Sample output:

1


Data range and tips

  For $ 20 \% $ data, $ n \ leqslant 5,000 $.
  For $ 60 \% $ data, $ n \ leqslant 10 ^ 5 $.
  For $ 100 \% $ data, $ 1 \ leqslant n \ leqslant 5 \ times 10 ^ 5, | ai |, | bi | \ leqslant 10 ^ 9 $.
  Data have a gradient.


answer

He did not play a positive solution.

A first look nature, provided $ s $ prefix and a sequence, then if $ i <j $ and $ s [j] \ geqslant s [i] $, and then adding this section is greater than $ 0 $.

So we can enumerate half the length, and then build a tree array to maintain a minimum value of the prefix and the array is $ A $ $ b $ with the subscript prefixes and arrays.

Time complexity: $ \ Theta (n \ log ^ 2 n) $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
unordered_map<long long,int>mp;
int n;
int a[500001],b[500001],tr[1000001],cnt;
long long sa[500001],sb[500001],sta[1000001];
int ans;
int lowbit(int x){return x&-x;}
void add(int x,int w){for(int i=x;i<=sta[0];i+=lowbit(i))tr[i]=min(tr[i],w);}
int ask(int x){int res=0x3f3f3f3f;for(int i=x;i;i-=lowbit(i))res=min(res,tr[i]);return res;}
bool judge(int x)
{
	memset(tr,0x3f,sizeof(tr));
	for(int i=x;i<=n;i++)
	{
		add(sb[i-x],sa[i-x]);
		if(ask(sb[i])<=sa[i])return 1;
	}
	return 0;
}
int main()
{
	scanf("%d",&n);It is [++ is [0]] = 0;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		sa[i]=sa[i-1]+a[i];
		sta[++sta[0]]=sa[i];
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&b[i]);
		sb[i]=sb[i-1]+b[i];
		sta[++sta[0]]=sb[i];
	}
	sort(sta+1,sta+sta[0]+1);
	for(int i=1;i<=sta[0];i++)if(sta[i]!=sta[i-1])mp[sta[i]]=++cnt;
	for(int i=0;i<=n;i++){sa[i]=mp[sa[i]];sb[i]=mp[sb[i]];}
	int lft=1,rht=n,res=1;
	while(lft<=rht)
	{
		int mid=(lft+rht)>>1;
		if(judge(mid)){res=mid;lft=mid+1;}
		else rht=mid-1;
	}
	printf("%d",res);
	return 0;
}

rp ++

Guess you like

Origin www.cnblogs.com/wzc521/p/11762757.html