Submission #553749

# Submission time Handle Problem Language Result Execution time Memory
553749 2022-04-26T19:35:11 Z QwertyPi Distributing Candies (IOI21_candies) C++17
100 / 100
347 ms 39352 KB
#include "candies.h"
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int N = 2e5+5;
const ll inf = 1LL << 60;
vector<int> L, R, V;
int q;

int bound(int l, int r, int x){
	if(x < l) return l;
	if(x > r) return r;
	return x;
}
namespace Segtree{
	struct node{
		ll mxp = 0, mnp = 0, s = 0;
		node(){}
		node(ll v) : mxp(max(v, 0LL)), mnp(min(v, 0LL)), s(v){};
	} t[N * 4];
	
	node merge(node a, node b){
		if(a.mxp == inf || b.mxp == inf) return a.mxp == inf ? b : a;
		node ret;
		ret.mxp = max(a.mxp, a.s + b.mxp);
		ret.mnp = min(a.mnp, a.s + b.mnp);
		ret.s = a.s + b.s;
		return ret;
	}
	
	void build(int v = 0, int l = 0, int r = q - 1){
		if(l == r){
			t[v] = node(0); return;
		}
		int m = (l + r) / 2;
		build(v * 2 + 1, l, m);
		build(v * 2 + 2, m + 1, r);
		t[v] = merge(t[v * 2 + 1], t[v * 2 + 2]);
	}
	void add(int id, int v = 0, int tl = 0, int tr = q - 1){
		if(tl == tr){
			t[v] = node(V[tl]); return;
		}
		int tm = (tl + tr) / 2;
		if(id <= tm){
			add(id, v * 2 + 1, tl, tm);
		}else{
			add(id, v * 2 + 2, tm + 1, tr);
		}
		t[v] = merge(t[v * 2 + 1], t[v * 2 + 2]);
	}
	
	void remove(int id, int v = 0, int tl = 0, int tr = q - 1){
		if(tl == tr){
			t[v] = node(0); return;
		}
		int tm = (tl + tr) / 2;
		if(id <= tm){
			remove(id, v * 2 + 1, tl, tm);
		}else{
			remove(id, v * 2 + 2, tm + 1, tr);
		}
		t[v] = merge(t[v * 2 + 1], t[v * 2 + 2]);
	}
	
	int query(int c){
		int v = 0, tl = 0, tr = q - 1;
		node cur {0};
		while(tl != tr){
			int tm = (tl + tr) / 2;
			node nxt = merge(t[v * 2 + 2], cur);
			if(nxt.mxp - nxt.mnp <= c){
				cur = nxt;
				tr = tm;
				v = v * 2 + 1;
			}else{
				tl = tm + 1;
				v = v * 2 + 2;
			}
		}
		node nxt = merge(t[v], cur);
		if(nxt.mxp - nxt.mnp <= c){
			cur = nxt;
			tr--;
		}
		int st = (tr < 0 ? 0 : bound(0, c, V[tr]));
		return bound(-cur.mnp, c - cur.mxp, st) + cur.s;
	}
};

vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
    int n = c.size();
    vector<int> ans(n);
    q = l.size();
    ::L = l, ::R = r, ::V = v;
    vector<pii> Q;
    for(int i = 0; i < q; i++){
    	Q.push_back({l[i], i+1});
    	Q.push_back({r[i]+1, -i-1});
	}
	sort(Q.begin(), Q.end());
	int qi = 0;
	for(int i = 0; i < n; i++){
		while(qi < Q.size() && Q[qi].fi <= i){
			if(Q[qi].se > 0) Segtree::add(Q[qi].se - 1);
			else Segtree::remove(-Q[qi].se - 1);
			qi++;
		}
		ans[i] = Segtree::query(c[i]);
	}
    return ans;
}

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:107:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |   while(qi < Q.size() && Q[qi].fi <= i){
      |         ~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 19028 KB Output is correct
2 Correct 8 ms 19056 KB Output is correct
3 Correct 10 ms 19156 KB Output is correct
4 Correct 10 ms 19112 KB Output is correct
5 Correct 10 ms 19196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 347 ms 37496 KB Output is correct
2 Correct 341 ms 36792 KB Output is correct
3 Correct 340 ms 36616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 19052 KB Output is correct
2 Correct 263 ms 33376 KB Output is correct
3 Correct 66 ms 24824 KB Output is correct
4 Correct 338 ms 38588 KB Output is correct
5 Correct 338 ms 38972 KB Output is correct
6 Correct 333 ms 39352 KB Output is correct
7 Correct 323 ms 38708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 19028 KB Output is correct
2 Correct 8 ms 19028 KB Output is correct
3 Correct 157 ms 32944 KB Output is correct
4 Correct 62 ms 22888 KB Output is correct
5 Correct 191 ms 36228 KB Output is correct
6 Correct 196 ms 36924 KB Output is correct
7 Correct 196 ms 37500 KB Output is correct
8 Correct 191 ms 36104 KB Output is correct
9 Correct 185 ms 37648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 19028 KB Output is correct
2 Correct 8 ms 19056 KB Output is correct
3 Correct 10 ms 19156 KB Output is correct
4 Correct 10 ms 19112 KB Output is correct
5 Correct 10 ms 19196 KB Output is correct
6 Correct 347 ms 37496 KB Output is correct
7 Correct 341 ms 36792 KB Output is correct
8 Correct 340 ms 36616 KB Output is correct
9 Correct 9 ms 19052 KB Output is correct
10 Correct 263 ms 33376 KB Output is correct
11 Correct 66 ms 24824 KB Output is correct
12 Correct 338 ms 38588 KB Output is correct
13 Correct 338 ms 38972 KB Output is correct
14 Correct 333 ms 39352 KB Output is correct
15 Correct 323 ms 38708 KB Output is correct
16 Correct 8 ms 19028 KB Output is correct
17 Correct 8 ms 19028 KB Output is correct
18 Correct 157 ms 32944 KB Output is correct
19 Correct 62 ms 22888 KB Output is correct
20 Correct 191 ms 36228 KB Output is correct
21 Correct 196 ms 36924 KB Output is correct
22 Correct 196 ms 37500 KB Output is correct
23 Correct 191 ms 36104 KB Output is correct
24 Correct 185 ms 37648 KB Output is correct
25 Correct 8 ms 19028 KB Output is correct
26 Correct 62 ms 22792 KB Output is correct
27 Correct 281 ms 32932 KB Output is correct
28 Correct 328 ms 37432 KB Output is correct
29 Correct 337 ms 37692 KB Output is correct
30 Correct 343 ms 37812 KB Output is correct
31 Correct 336 ms 38064 KB Output is correct
32 Correct 338 ms 38268 KB Output is correct