Submission #435938

# Submission time Handle Problem Language Result Execution time Memory
435938 2021-06-24T02:21:49 Z dqhungdl Distributing Candies (IOI21_candies) C++17
100 / 100
4608 ms 34896 KB
#include "candies.h"

#include <bits/stdc++.h>
using namespace std;

typedef pair<long long,long long> ii;
const int MAX=200005;
const long long INF=1e18;
int n,m;
long long Pmin[4*MAX],Pmax[4*MAX],lazy[4*MAX];
vector<ii> events[MAX];

void down(int k) {
	Pmin[2*k]+=lazy[k],Pmin[2*k+1]+=lazy[k];
	Pmax[2*k]+=lazy[k],Pmax[2*k+1]+=lazy[k];
	lazy[2*k]+=lazy[k],lazy[2*k+1]+=lazy[k];
	lazy[k]=0;
}

void update(int k,int l,int r,int L,int R,int val) {
	if(l>R||L>r)
		return;
	if(L<=l&&r<=R) {
		Pmin[k]+=val;
		Pmax[k]+=val;
		lazy[k]+=val;
		return;
	}
	down(k);
	int mid=(l+r)/2;
	update(2*k,l,mid,L,R,val);
	update(2*k+1,mid+1,r,L,R,val);
	Pmin[k]=min(Pmin[2*k],Pmin[2*k+1]);
	Pmax[k]=max(Pmax[2*k],Pmax[2*k+1]);
}

void update(int limit,int val) {
	update(1,0,m,limit,m,val);
}

ii query(int k,int l,int r,int L,int R) {
	if(l>R||L>r)
		return {INF,-INF};
	if(L<=l&&r<=R)
		return {Pmin[k],Pmax[k]};
	down(k);
	int mid=(l+r)/2;
	ii p1=query(2*k,l,mid,L,R);
	ii p2=query(2*k+1,mid+1,r,L,R);
	return {min(p1.first,p2.first),max(p1.second,p2.second)};
}

long long queryPmin(int limit) {
	return query(1,0,m,limit,m).first;
}

long long queryPmax(int limit) {
	return query(1,0,m,limit,m).second;
}

long long queryPos(int pos) {
	return query(1,0,m,pos,pos).first;
}

std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
                                    std::vector<int> r, std::vector<int> v) {
    n=c.size(),m=v.size();
	for(int i=0;i<m;i++) {
		events[l[i]].push_back({i+1,v[i]});
		events[r[i]+1].push_back({i+1,-v[i]});
	}
	vector<int> rs(n);
	for(int i=0;i<n;i++) {
		// Update
		for(ii event:events[i])
			update(event.first,event.second);
		// If all updates are in range
		if(queryPmax(0)-queryPmin(0)<=c[i]) {
			int l=1,r=m,pivot=0;
			long long limit=queryPmin(0);
			while(l<=r) {
				int mid=(l+r)/2;
				if(queryPmin(mid)==limit) {
					l=mid+1;
					pivot=mid;
				}
				else
					r=mid-1;
			}
			rs[i]=queryPos(m)-queryPos(pivot);
			continue;
		}
		int l=0,r=m,pivot;
		while(l<=r) {
			int mid=(l+r)/2;
			if(queryPmax(mid)-queryPmin(mid)>c[i]) {
				pivot=mid;
				l=mid+1;
			} else
				r=mid-1;
		}
		// Find the last time touching the upper wall
		if(queryPmin(pivot)==queryPos(pivot)) {
			l=pivot+1,r=m;
			long long limit=queryPmax(pivot);
			while(l<=r) {
				int mid=(l+r)/2;
				if(queryPmax(mid)==limit) {
					pivot=mid;
					l=mid+1;
				} else
					r=mid-1;
			}
			rs[i]=c[i]+queryPos(m)-queryPos(pivot);
		} else { // Find the last time touching the lower wall
			l=pivot+1,r=m;
			long long limit=queryPmin(pivot);
			while(l<=r) {
				int mid=(l+r)/2;
				if(queryPmin(mid)==limit) {
					pivot=mid;
					l=mid+1;
				} else
					r=mid-1;
			}
			rs[i]=queryPos(m)-queryPos(pivot);
		}
	}
	return rs;
}

Compilation message

candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:104:5: warning: 'pivot' may be used uninitialized in this function [-Wmaybe-uninitialized]
  104 |    l=pivot+1,r=m;
      |    ~^~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 5 ms 5196 KB Output is correct
4 Correct 6 ms 5196 KB Output is correct
5 Correct 18 ms 5284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3944 ms 34828 KB Output is correct
2 Correct 4202 ms 34852 KB Output is correct
3 Correct 3754 ms 34832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4940 KB Output is correct
2 Correct 426 ms 30788 KB Output is correct
3 Correct 1364 ms 8772 KB Output is correct
4 Correct 4550 ms 34848 KB Output is correct
5 Correct 4608 ms 34848 KB Output is correct
6 Correct 3745 ms 34852 KB Output is correct
7 Correct 3695 ms 34856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 5 ms 4940 KB Output is correct
3 Correct 228 ms 28464 KB Output is correct
4 Correct 1049 ms 7704 KB Output is correct
5 Correct 3014 ms 30744 KB Output is correct
6 Correct 3086 ms 30664 KB Output is correct
7 Correct 3113 ms 30664 KB Output is correct
8 Correct 2896 ms 30660 KB Output is correct
9 Correct 4480 ms 30672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 5 ms 5196 KB Output is correct
4 Correct 6 ms 5196 KB Output is correct
5 Correct 18 ms 5284 KB Output is correct
6 Correct 3944 ms 34828 KB Output is correct
7 Correct 4202 ms 34852 KB Output is correct
8 Correct 3754 ms 34832 KB Output is correct
9 Correct 6 ms 4940 KB Output is correct
10 Correct 426 ms 30788 KB Output is correct
11 Correct 1364 ms 8772 KB Output is correct
12 Correct 4550 ms 34848 KB Output is correct
13 Correct 4608 ms 34848 KB Output is correct
14 Correct 3745 ms 34852 KB Output is correct
15 Correct 3695 ms 34856 KB Output is correct
16 Correct 4 ms 4940 KB Output is correct
17 Correct 5 ms 4940 KB Output is correct
18 Correct 228 ms 28464 KB Output is correct
19 Correct 1049 ms 7704 KB Output is correct
20 Correct 3014 ms 30744 KB Output is correct
21 Correct 3086 ms 30664 KB Output is correct
22 Correct 3113 ms 30664 KB Output is correct
23 Correct 2896 ms 30660 KB Output is correct
24 Correct 4480 ms 30672 KB Output is correct
25 Correct 4 ms 4940 KB Output is correct
26 Correct 908 ms 7636 KB Output is correct
27 Correct 451 ms 30920 KB Output is correct
28 Correct 3043 ms 34864 KB Output is correct
29 Correct 3447 ms 34832 KB Output is correct
30 Correct 3685 ms 34820 KB Output is correct
31 Correct 3611 ms 34896 KB Output is correct
32 Correct 3586 ms 34884 KB Output is correct