Submission #504662

# Submission time Handle Problem Language Result Execution time Memory
504662 2022-01-10T07:16:14 Z arthur_nascimento Distributing Candies (IOI21_candies) C++17
8 / 100
3415 ms 72876 KB
#include "candies.h"

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

#define maxn 200200
#define mid ((ini+fim)/2)
#define pb push_back
#define ll long long
#define search iwfew
#define debug 

ll T[4*maxn];
ll mn[4*maxn];
ll mx[4*maxn];
int pos_mn[4*maxn];

ll lazy[4*maxn];

int n,q;

void refresh(int ini,int fim,int p){

	if(ini < fim){
		lazy[2*p] += lazy[p];
		lazy[2*p+1] += lazy[p];
	}

	mn[p] += lazy[p];
	mx[p] += lazy[p];

	lazy[p] = 0;
	
}

void merge(int ini,int fim,int p){
	mx[p] = max(mx[2*p],mx[2*p+1]);
	mn[p] = min(mn[2*p],mn[2*p+1]);
	pos_mn[p] = pos_mn[2*p];
	if(mn[2*p+1] < mn[2*p]) pos_mn[p] = pos_mn[2*p+1];
	T[p] = mx[p] - mn[p];
	
}

void bd(int ini,int fim,int p){
	if(ini == fim){
		pos_mn[p] = ini;
		return;
	}
	bd(ini,mid,2*p);
	bd(mid+1,fim,2*p+1);
	pos_mn[p] = ini;
}

int search(int ini,int fim,int p,ll d,ll mnr=0,ll mxr=0,ll hasr=0){

	refresh(ini,fim,p);

	if(ini == fim) return ini;

	ll gapR = T[2*p+1];
	if(hasr) gapR = max(gapR, mxr - mn[2*p+1]);
	if(hasr) gapR = max(gapR, mx[2*p+1] - mnr);

	if(gapR > d) return search(mid+1,fim,2*p+1,d,mnr,mxr,hasr);

	ll mn_ = mn[2*p+1]; if(hasr) mn_ = min(mn_, mnr);
	ll mx_ = mx[2*p+1]; if(hasr) mx_ = max(mx_, mxr);

	return search(ini,mid,2*p,d,mn_,mx_,1);
	
}

ll get(int ini,int fim,int p,int pos){
	refresh(ini,fim,p);
	if(ini > pos || fim < pos) return 0;
	if(ini == fim) return mx[p];
	ll ret = get(ini,mid,2*p,pos) + get(mid+1,fim,2*p+1,pos);
	merge(ini,fim,p);
	return ret;
}

void upd(int ini,int fim,int p,int l,int r,int x){

	refresh(ini,fim,p);

	if(l > fim || r < ini) return;

	if(ini >= l && fim <= r){
		lazy[p] += x;
		refresh(ini,fim,p);
		return;
	}

	upd(ini,mid,2*p,l,r,x);
	upd(mid+1,fim,2*p+1,l,r,x);

	merge(ini,fim,p);

}



vector<int> Ti[4*maxn];

void updi(int ini,int fim,int p,int a,int b,int k){
	if(ini > b) return;
	if(fim < a) return;
	if(ini >= a && fim <= b){
		Ti[p].pb(k);
		return;
	}
	updi(ini,mid,2*p,a,b,k);
	updi(mid+1,fim,2*p+1,a,b,k);
}

vector<int> ans;

vector<int> l,r,add,cap;

void go(int ini,int fim,int p,ll var = 0){

	debug("go %d~%d\n",ini,fim);

	for(int a : Ti[p]){
		upd(0,q,1,a+1,q,add[a]);
		debug("add %d at %d\n",add[a],a);
		var += add[a];
	}
	if(ini == fim) ans[ini] = min(var,(ll)cap[ini]);
	if(ini == fim && 0){

		int c = cap[ini];

		int beg = 0;

		debug("qry %d cap %d\n",ini,c);

		//if(T[1] > c)
			//beg = 1 + search(0,q,1,c);

		beg = max(beg,pos_mn[1]);

		ll //delta = get(0,q,1,q) - get(0,q,1,beg);

		delta = get(0,q,1,q);

		ans[ini] = min((ll)c,delta);
		

		debug("get %d = %lld\n",q,get(0,q,1,q));
		debug("get %d = %lld\n",beg-1,get(0,q,1,beg-1));
		debug("get %d = %lld\n",beg,get(0,q,1,beg));

		debug("T[1] = %d, beg = %d, delta = %lld\n\n",T[1],beg,delta);

		/*if(beg > 0){
			if(get(0,q,1,beg-1) < get(0,q,1,beg)) ans[ini] = c;
		}

		if(delta >= 0) ans[ini] = min(ans[ini]+delta,c);
		else ans[ini] = max(ans[ini]+delta,0);
		*/
	//	if(delta == 0 && beg > 0 && get(0,q,1,beg-1) > get(0,q,1,beg)) ans[ini] = 0;
	
	}

	if(ini < fim) go(ini,mid,2*p,var);
	if(ini < fim) go(mid+1,fim,2*p+1,var);

	for(int a : Ti[p]){
		upd(0,q,1,a+1,q,-add[a]);
		debug("rem %d at %d\n",add[a],a);
		var -= add[a];
	}
}

std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> L,
                                    std::vector<int> R, std::vector<int> v) {
    n = c.size();
    q = L.size();

	cap = c;
	l = L;
	r = R;
	add = v;
    
	for(int i=0;i<q;i++)
		updi(0,n-1,1,l[i],r[i],i);

	bd(0,q-1,1);		

	for(int i=0;i<n;i++) ans.pb(0);

	go(0,n-1,1);

    return ans;
}

Compilation message

candies.cpp: In function 'void go(int, int, int, long long int)':
candies.cpp:126:8: warning: left operand of comma operator has no effect [-Wunused-value]
  126 |  debug("go %d~%d\n",ini,fim);
      |        ^~~~~~~~~~~~
candies.cpp:126:25: warning: right operand of comma operator has no effect [-Wunused-value]
  126 |  debug("go %d~%d\n",ini,fim);
      |                         ^~~
candies.cpp:130:9: warning: left operand of comma operator has no effect [-Wunused-value]
  130 |   debug("add %d at %d\n",add[a],a);
      |         ^~~~~~~~~~~~~~~~
candies.cpp:140:9: warning: left operand of comma operator has no effect [-Wunused-value]
  140 |   debug("qry %d cap %d\n",ini,c);
      |         ^~~~~~~~~~~~~~~~~
candies.cpp:140:31: warning: right operand of comma operator has no effect [-Wunused-value]
  140 |   debug("qry %d cap %d\n",ini,c);
      |                               ^
candies.cpp:154:9: warning: left operand of comma operator has no effect [-Wunused-value]
  154 |   debug("get %d = %lld\n",q,get(0,q,1,q));
      |         ^~~~~~~~~~~~~~~~~
candies.cpp:154:40: warning: right operand of comma operator has no effect [-Wunused-value]
  154 |   debug("get %d = %lld\n",q,get(0,q,1,q));
      |                                        ^
candies.cpp:155:9: warning: left operand of comma operator has no effect [-Wunused-value]
  155 |   debug("get %d = %lld\n",beg-1,get(0,q,1,beg-1));
      |         ^~~~~~~~~~~~~~~~~
candies.cpp:155:30: warning: right operand of comma operator has no effect [-Wunused-value]
  155 |   debug("get %d = %lld\n",beg-1,get(0,q,1,beg-1));
      |                           ~~~^~
candies.cpp:156:9: warning: left operand of comma operator has no effect [-Wunused-value]
  156 |   debug("get %d = %lld\n",beg,get(0,q,1,beg));
      |         ^~~~~~~~~~~~~~~~~
candies.cpp:156:44: warning: right operand of comma operator has no effect [-Wunused-value]
  156 |   debug("get %d = %lld\n",beg,get(0,q,1,beg));
      |                                            ^
candies.cpp:158:9: warning: left operand of comma operator has no effect [-Wunused-value]
  158 |   debug("T[1] = %d, beg = %d, delta = %lld\n\n",T[1],beg,delta);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
candies.cpp:158:52: warning: right operand of comma operator has no effect [-Wunused-value]
  158 |   debug("T[1] = %d, beg = %d, delta = %lld\n\n",T[1],beg,delta);
      |                                                 ~~~^
candies.cpp:158:58: warning: right operand of comma operator has no effect [-Wunused-value]
  158 |   debug("T[1] = %d, beg = %d, delta = %lld\n\n",T[1],beg,delta);
      |                                                          ^~~~~
candies.cpp:176:9: warning: left operand of comma operator has no effect [-Wunused-value]
  176 |   debug("rem %d at %d\n",add[a],a);
      |         ^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19020 KB Output is correct
2 Incorrect 12 ms 19092 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3220 ms 68576 KB Output is correct
2 Correct 3415 ms 72876 KB Output is correct
3 Correct 3127 ms 72712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 19148 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 11 ms 19148 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19020 KB Output is correct
2 Incorrect 12 ms 19092 KB Output isn't correct
3 Halted 0 ms 0 KB -