Submission #113409

# Submission time Handle Problem Language Result Execution time Memory
113409 2019-05-25T12:39:37 Z TadijaSebez Sails (IOI07_sails) C++11
100 / 100
31 ms 2396 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=100050;
ll sum[N],cnt[N];
ll suf[N];
const int M=2*N;
int ls[M],rs[M],tsz,root;
pair<int,int> mn[M];
pair<int,int> Get(int c, int ss, int se, int qs, int qe)
{
	if(qs<=ss && qe>=se) return mn[c];
	int mid=ss+se>>1;
	if(qe<=mid) return Get(ls[c],ss,mid,qs,qe);
	if(qs>mid) return Get(rs[c],mid+1,se,qs,qe);
	return min(Get(ls[c],ss,mid,qs,qe),Get(rs[c],mid+1,se,qs,qe));
}
void Set(int &c, int ss, int se, int qi, int f)
{
	if(!c) c=++tsz;
	if(ss==se){ mn[c]={f,ss};return;}
	int mid=ss+se>>1;
	if(qi<=mid) Set(ls[c],ss,mid,qi,f);
	else Set(rs[c],mid+1,se,qi,f);
	mn[c]=min(mn[ls[c]],mn[rs[c]]);
}
int main()
{
	int n,h,k,mxh=0;
	scanf("%i",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%i %i",&h,&k);
		cnt[h]++;
		sum[h-k+1]++;
		sum[h+1]--;
		mxh=max(mxh,h);
	}
	for(int i=1;i<=mxh;i++) sum[i]+=sum[i-1];

	/*while(1)
	{
		bool ok=0;
		for(int i=mxh-1;i>=1;i--) if(sum[i]<sum[i+1])
		{
			int z=sum[i]+sum[i+1];
			sum[i]=(z+1)/2;
			sum[i+1]=z/2;
			ok=1;
		}
		if(!ok) break;
	}*/
	/*Set(root,1,mxh,1,sum[1]);
	for(int i=2;i<=mxh;i++)
	{
		while(1)
		{
			pair<int,int> p=Get(root,1,mxh,1,i-1);
			if(p.first>=sum[i]) break;
			sum[p.second]++;
			sum[i]--;
			Set(root,1,mxh,p.second,sum[p.second]);
		}
		Set(root,1,mxh,i,sum[i]);
	}*/
	stack<pair<int,int>> stk;
	auto push_stk=[&](int x, int sz)
	{
		//printf("Push (%i %i)\n",x,sz);
		if(stk.empty() || stk.top().first!=x) stk.push({x,sz});
		else
		{
			pair<int,int> p=stk.top();
			stk.pop();
			sz+=p.second;
			stk.push({x,sz});
		}
	};
	push_stk(N,0);
	for(int i=1;i<=mxh;i++)
	{
		//printf("i:%i sum:%i\n",i,sum[i]);
		while(stk.size() && stk.top().first<sum[i])
		{
			pair<int,int> p=stk.top();
			//printf("i:%i x:%i sz:%i\n",i,p.first,p.second);
			stk.pop();
			ll dif=1e9,nh=1e9;
			if(stk.size()) dif=stk.top().first-p.first,nh=stk.top().first;
			dif*=p.second;
			if(sum[i]-dif<nh)
			{
				int sz=p.second;
				ll lo=0,hi=1e5,mi=lo+hi+1>>1;
				for(;lo<hi;mi=lo+hi+1>>1)
				{
					if(sum[i]-mi*sz>=p.first+mi) lo=mi;
					else hi=mi-1;
				}
				//printf("lo:%i\n",lo);
				sum[i]-=lo*sz;
				p.first+=lo;
				int ost=sum[i]-p.first;
				sum[i]-=ost;
				if(ost>0) push_stk(p.first+1,ost);
				if(ost!=sz) push_stk(p.first,sz-ost);
			}
			else
			{
				//printf(":D\n");
				sum[i]-=dif;
				push_stk(nh,p.second);
			}
		}
		push_stk(sum[i],1);
	}

	ll ans=0;
	//for(int i=1;i<=mxh;i++) ans+=(ll)sum[i]*(sum[i]-1)/2;
	while(stk.size())
	{
		pair<int,int> p=stk.top();
		stk.pop();
		ans+=(ll)p.first*(p.first-1)/2*p.second;
		//printf("%i %i\n",p.first,p.second);
	}
	/*for(int i=mxh;i>=1;i--) suf[i]=suf[i+1]+(ll)sum[i]*(sum[i]-1)/2;
	ll tmp=0,ans=suf[1];
	for(int i=1;i<=mxh;i++)
	{
		tmp+=sum[i];
		ll all=tmp/i,ost=tmp%i;
		ll now=suf[i+1]+(i-ost)*all*(all-1)/2+ost*all*(all+1)/2;
		ans=min(ans,now);
	}*/

	printf("%lld\n",ans);
	return 0;
}

Compilation message

sails.cpp: In function 'std::pair<int, int> Get(int, int, int, int, int)':
sails.cpp:13:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid=ss+se>>1;
          ~~^~~
sails.cpp: In function 'void Set(int&, int, int, int, int)':
sails.cpp:22:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid=ss+se>>1;
          ~~^~~
sails.cpp: In function 'int main()':
sails.cpp:94:28: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     ll lo=0,hi=1e5,mi=lo+hi+1>>1;
                       ~~~~~^~
sails.cpp:95:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     for(;lo<hi;mi=lo+hi+1>>1)
                   ~~~~~^~
sails.cpp:30:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i",&n);
  ~~~~~^~~~~~~~~
sails.cpp:33:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%i %i",&h,&k);
   ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 8 ms 1792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 768 KB Output is correct
2 Correct 8 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 1272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 1280 KB Output is correct
2 Correct 23 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 2296 KB Output is correct
2 Correct 19 ms 1752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 1912 KB Output is correct
2 Correct 21 ms 1664 KB Output is correct