Submission #467946

# Submission time Handle Problem Language Result Execution time Memory
467946 2021-08-25T17:11:29 Z Vladithur Safety (NOI18_safety) C++17
100 / 100
723 ms 24316 KB
//#pragma GCC optimize ("Ofast")
//#pragma GCC target("avx2")
//#pragma GCC target ("sse4")
//#pragma GCC optimize("unroll-loops")
//#include <x86intrin.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

#define PI 3.141592653589793L
#define FAST ios::sync_with_stdio(false); cin.tie(NULL);
// Use for file I/O;
#define FIN string _fname = "input"; \
			string _is = _fname + ".in", _os = _fname + ".out"; \
			freopen(_is.c_str(), "r", stdin); \
			freopen(_os.c_str(), "w", stdout);

#define FIN2 freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define RINIT mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define gsize(x) (int)((x).size())

using namespace std;
//using namespace __gnu_pbds;

struct chash {
    static inline uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
 
    inline size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM =
            chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
 
    inline size_t operator()(const pair<uint64_t, uint64_t> &v) const {
        chash int_hasher;
        return int_hasher(v.first) * 31 + int_hasher(v.second);
    }
};

//template <class K>
//using hash_set = gp_hash_table<K, null_type, chash>;

const char nl = '\n';

typedef long long ll;
typedef long double ld;
// typedef unsigned int uint;
typedef unsigned long long ull;

const ll inf = 1e9 + 10;
const ll inf2 = 1e18 + 99LL;
const ld inf3 = 1e14;
const ll mod = 1e9+7, mod2 = 998244353;
const ld eps = 1e-9;
const bool local = false;

//RINIT;
const int logn = 21, maxn = 200001, maxm = 1, maxn2 = 3;	

struct node {
	ll v, prop;
	int size, pr;
	node *l, *r;
	
	node (int x, node * tl = nullptr, node * tr = nullptr) {
		size = 1;
		v = x;
		prop = 0;
		pr = rand();
		l = tl;
		r = tr;
	}
};

int size(node * t) {
	if (t) return t->size;
	return 0;
}

void recalc(node * t) {
	if (!t) return;
	t->size = 1 + size(t->l) + size(t->r);
}

void prop(node * t) {
	if (!t || !t->prop) return;
	t->v += t->prop;
	if (t->l) t->l->prop += t->prop;
	if (t->r) t->r->prop += t->prop;
	t->prop = 0;
}

// We want to split of the first 'n' ellements
array<node *, 2> split(node * t, int n) {
	if (!t) return {nullptr, nullptr};
	prop(t);
	if (size(t->l) >= n) {
		auto res = split(t->l, n);
		t->l = res[1];
		recalc(t);
		return {res[0], t};
	} else {
		auto res = split(t->r, n - size(t->l) - 1);
		t->r = res[0];
		recalc(t);
		return {t, res[1]};
	}
}

// Merge two treaps
node * merge(node *a, node *b) {
	if (!a) return b;
	if (!b) return a;
	prop(a); prop(b);
	if (a->pr < b->pr) {
		a->r = merge(a->r, b);
		recalc(a);
		return a;
	} else {
		b->l = merge(a, b->l);
		recalc(b);
		return b;
	}
}

// Add 'x' on [l; r)
node *update(node * t, int l, int r, int x) {
	if (l >= r) return t;
	auto a = split(t, l);
	auto b = split(a[1], r - l);
	//cout << "U: " << size(a[0]) << ' ' << size(b[0]) << ' ' << size(b[1]) << endl;
	b[0]->prop += x;
	return merge(a[0], merge(b[0], b[1]));
}

// Get first index with value > x
int tres = 0;
void find(node * t, ll x, int ti = 0) {
	if (!t) return;
	prop(t);
	if (t->v > x) {
		tres = min(tres, size(t->l) + ti);
		find(t->l, x, ti);
	} else {
		find(t->r, x, ti + size(t->l) + 1);
	}
}

void print(node * t) {
	if (!t) return;
	prop(t);
	print(t->l);
	cout << t->v << ' ';
	print(t->r);
}

vector<ll> ta;
void toArray(node * t) {
	if (!t) return;
	prop(t);
	toArray(t->l);
	ta.push_back(t->v);
	toArray(t->r);
	delete t;
}

int main() {
	FAST;
	srand(0);
	
	int n, h;
	cin >> n >> h;
	
	__int128 k = -1, b;
	ll ttta;
	cin >> ttta;
	b = ttta;
	node * t = new node(b);
	t = merge(t, new node(b));
	
	for (int i = 1; i < n; i++) {
		ll a;
		cin >> a;
		
		//print(t);
		//cout << nl;
			
		int ti = - 1 - k;
		//cout << 0 << ' ' << ti + 1 << ' ' << ti + 1 << ' ' << i * 2 << endl;
		t = update(t, 0, ti + 1, -h);
		t = update(t, ti + 1, i * 2, +h);

		//print(t);
		//cout << nl;

		
		tres = inf;
		find(t, a);
		
		// cout << tres << nl;
		
		node * t2 = new node(a);
		t2 = merge(t2, new node(a));
		if (tres == inf) {
			t = merge(t, t2);
		} else {
			auto c = split(t, tres);
			c[0] = merge(c[0], t2);
			t = merge(c[0], c[1]);
		}
		b += k * h;	
		k--;
		b += a;
		//print(t);
		//cout << nl;
		//cout << "F: " << k << ' ' << b << nl << nl;
		
	}
	
	__int128 ans = inf2;
	
	toArray(t);
	for (ll x: ta) {
		ans = min(ans, x * k + b);
		k++;
		b -= x;
	}
	cout << (ll)(ans) << nl;
}

// © Benq 
/* stuff you should look for
	* int overflow, array bounds	
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 451 ms 17044 KB Output is correct
2 Correct 668 ms 22472 KB Output is correct
3 Correct 719 ms 23092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 312 KB Output is correct
22 Correct 1 ms 312 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 312 KB Output is correct
22 Correct 1 ms 312 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 14 ms 1008 KB Output is correct
29 Correct 8 ms 836 KB Output is correct
30 Correct 14 ms 960 KB Output is correct
31 Correct 10 ms 716 KB Output is correct
32 Correct 8 ms 844 KB Output is correct
33 Correct 10 ms 844 KB Output is correct
34 Correct 9 ms 972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 312 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 312 KB Output is correct
28 Correct 1 ms 312 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 316 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 14 ms 1008 KB Output is correct
35 Correct 8 ms 836 KB Output is correct
36 Correct 14 ms 960 KB Output is correct
37 Correct 10 ms 716 KB Output is correct
38 Correct 8 ms 844 KB Output is correct
39 Correct 10 ms 844 KB Output is correct
40 Correct 9 ms 972 KB Output is correct
41 Correct 11 ms 972 KB Output is correct
42 Correct 11 ms 816 KB Output is correct
43 Correct 9 ms 972 KB Output is correct
44 Correct 10 ms 960 KB Output is correct
45 Correct 9 ms 972 KB Output is correct
46 Correct 9 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 312 KB Output is correct
19 Correct 451 ms 17044 KB Output is correct
20 Correct 668 ms 22472 KB Output is correct
21 Correct 719 ms 23092 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 312 KB Output is correct
31 Correct 1 ms 312 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 316 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 14 ms 1008 KB Output is correct
38 Correct 8 ms 836 KB Output is correct
39 Correct 14 ms 960 KB Output is correct
40 Correct 10 ms 716 KB Output is correct
41 Correct 8 ms 844 KB Output is correct
42 Correct 10 ms 844 KB Output is correct
43 Correct 9 ms 972 KB Output is correct
44 Correct 11 ms 972 KB Output is correct
45 Correct 11 ms 816 KB Output is correct
46 Correct 9 ms 972 KB Output is correct
47 Correct 10 ms 960 KB Output is correct
48 Correct 9 ms 972 KB Output is correct
49 Correct 9 ms 844 KB Output is correct
50 Correct 723 ms 23496 KB Output is correct
51 Correct 605 ms 20196 KB Output is correct
52 Correct 434 ms 19516 KB Output is correct
53 Correct 547 ms 23732 KB Output is correct
54 Correct 523 ms 24316 KB Output is correct
55 Correct 436 ms 21428 KB Output is correct
56 Correct 517 ms 22960 KB Output is correct
57 Correct 450 ms 21432 KB Output is correct
58 Correct 466 ms 16640 KB Output is correct
59 Correct 501 ms 23264 KB Output is correct