Submission #838550

# Submission time Handle Problem Language Result Execution time Memory
838550 2023-08-27T11:27:58 Z adrilen Horses (IOI15_horses) C++17
100 / 100
671 ms 33228 KB
#include "horses.h"
#include<bits/stdc++.h>

using namespace std;
using ll = long long;
constexpr int maxn = 5e5, bit = 19;
constexpr ll mod = 1e9 + 7, siz = (1ll << bit);
using pii = pair<ll, bool>;

struct node {
	ll gfac; // Growth factor
	int best_pos;
	bool over_mod;
};

int n;
node segtree[siz * 2] = { 0 };
ll prices[siz] = { 0 };

void fm(ll &a)
{
	if (a >= mod) 
	{
		a %= mod;
	}
}


pair<ll, bool> query(int p, int l, int r, int gl, int gr)
{
	if (gl > gr) assert(0);

	if (l > gr || gl > r) return {1, false};
	// cout << l << " " << r << "\n";

	if (gl <= l && r <= gr) return {segtree[p].gfac, segtree[p].over_mod};

	int mid = (l + r) >> 1;
	pii le = query(p * 2, l, mid, gl, gr), ri = query(p * 2 + 1, mid + 1, r, gl, gr);

	le.first *= ri.first;
	le.second |= (ri.second || (le.first >= mod));
	fm(le.first);
	return le;
}


bool test_pos(int a, int b)
{
	int swapped = 0;
	if (a > b)
	{
		swap(a, b);
		swapped = 1;
	}


	pii le = {prices[a], false};
	pii re = query(1, 0, siz - 1, a + 1, b);

	re.first *= prices[b];
	re.second |= (re.first >= mod);

	// cout << a << " " << b << "\t" << le.first << " " << re.first << "\n";


	if (re.second) return 1 ^ swapped;
	if (re.first > le.first) return 1 ^ swapped;
	return 0 ^ swapped;
}


node comp(node a, const node &b)
{
	a.gfac *= b.gfac;
	a.over_mod |= (b.over_mod || (a.gfac >= mod));
	fm(a.gfac);

	return a;
}

void best_pos_comp(node &o, node &a, node &b)
{
	// cout << a.best_pos << " " << b.best_pos << "\n";
	if (test_pos(a.best_pos, b.best_pos))
	{
		o.best_pos = b.best_pos;
	} else {
		o.best_pos = a.best_pos;
	}
}

ll get_val_pos(int p)
{
	ll out = query(1, 0, siz - 1, 0, p).first * prices[p];
	fm(out);
	return out;
}

int init(int N, int X[], int Y[]) {
	n = N;

	for (int i = 0; i < n; i++)
	{
		prices[i] = Y[i];
		segtree[i + siz] = node{X[i], i, false};
	}

	for (int i = n; i < siz; i++)
	{
		segtree[i + siz] = node{1, i, false};
	}

	// for (int i = 1; i < siz * 2; i++)
	// {
	// 	if (__builtin_popcount(i) == 1) cout << "\n";

	// 	cout << segtree[i].gfac << " " << segtree[i].best_pos << "\t";
	// }
	// cout << "\n";

	for (int i = siz - 1; i > 0; i--)
	{
		segtree[i] = comp(segtree[i * 2], segtree[i * 2 + 1]);
	}

	// for (int i = 1; i < siz * 2; i++)
	// {
	// 	if (__builtin_popcount(i) == 1) cout << "\n";

	// 	cout << segtree[i].gfac << " " << segtree[i].best_pos << "\t";
	// }
	// cout << "\n";

	for (int i = siz - 1; i > 0; i--)
	{
		best_pos_comp(segtree[i], segtree[i * 2], segtree[i * 2 + 1]);

	}


	return (int)get_val_pos(segtree[1].best_pos);
}

void update_upwards(ll p)
{
	p += siz;
	while (p > 1)
	{
		p /= 2;
		segtree[p] = comp(segtree[p * 2], segtree[p * 2 + 1]);
		best_pos_comp(segtree[p], segtree[p * 2], segtree[p * 2 + 1]);
	}
}


int updateX(int pos, int val) {

	segtree[pos + siz].gfac = val;
	update_upwards(pos);

	return (int)get_val_pos(segtree[1].best_pos);
}

int updateY(int pos, int val) {
	prices[pos] = val;
	update_upwards(pos);

	return (int)get_val_pos(segtree[1].best_pos);
}

Compilation message

horses.cpp:17:29: warning: missing initializer for member 'node::best_pos' [-Wmissing-field-initializers]
   17 | node segtree[siz * 2] = { 0 };
      |                             ^
horses.cpp:17:29: warning: missing initializer for member 'node::over_mod' [-Wmissing-field-initializers]
# Verdict Execution time Memory Grader output
1 Correct 99 ms 16716 KB Output is correct
2 Correct 90 ms 16716 KB Output is correct
3 Correct 118 ms 16716 KB Output is correct
4 Correct 101 ms 16828 KB Output is correct
5 Correct 107 ms 16700 KB Output is correct
6 Correct 102 ms 16720 KB Output is correct
7 Correct 112 ms 16700 KB Output is correct
8 Correct 108 ms 16700 KB Output is correct
9 Correct 92 ms 16700 KB Output is correct
10 Correct 91 ms 16708 KB Output is correct
11 Correct 122 ms 16720 KB Output is correct
12 Correct 89 ms 16724 KB Output is correct
13 Correct 90 ms 16704 KB Output is correct
14 Correct 123 ms 16712 KB Output is correct
15 Correct 125 ms 16596 KB Output is correct
16 Correct 99 ms 16700 KB Output is correct
17 Correct 90 ms 16724 KB Output is correct
18 Correct 85 ms 16596 KB Output is correct
19 Correct 88 ms 16596 KB Output is correct
20 Correct 89 ms 16720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 16716 KB Output is correct
2 Correct 93 ms 16724 KB Output is correct
3 Correct 91 ms 16704 KB Output is correct
4 Correct 100 ms 16700 KB Output is correct
5 Correct 103 ms 16704 KB Output is correct
6 Correct 105 ms 16608 KB Output is correct
7 Correct 110 ms 16700 KB Output is correct
8 Correct 106 ms 16716 KB Output is correct
9 Correct 108 ms 16716 KB Output is correct
10 Correct 104 ms 16708 KB Output is correct
11 Correct 99 ms 16720 KB Output is correct
12 Correct 95 ms 16708 KB Output is correct
13 Correct 103 ms 16716 KB Output is correct
14 Correct 111 ms 16596 KB Output is correct
15 Correct 99 ms 16704 KB Output is correct
16 Correct 94 ms 16700 KB Output is correct
17 Correct 98 ms 16720 KB Output is correct
18 Correct 108 ms 16716 KB Output is correct
19 Correct 107 ms 16640 KB Output is correct
20 Correct 93 ms 16724 KB Output is correct
21 Correct 102 ms 16704 KB Output is correct
22 Correct 104 ms 16596 KB Output is correct
23 Correct 106 ms 16748 KB Output is correct
24 Correct 107 ms 16740 KB Output is correct
25 Correct 104 ms 16876 KB Output is correct
26 Correct 116 ms 16752 KB Output is correct
27 Correct 115 ms 16740 KB Output is correct
28 Correct 104 ms 16748 KB Output is correct
29 Correct 113 ms 16740 KB Output is correct
30 Correct 98 ms 16756 KB Output is correct
31 Correct 108 ms 16732 KB Output is correct
32 Correct 95 ms 16724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 431 ms 25424 KB Output is correct
2 Correct 437 ms 32364 KB Output is correct
3 Correct 524 ms 29212 KB Output is correct
4 Correct 452 ms 32452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 16716 KB Output is correct
2 Correct 91 ms 16720 KB Output is correct
3 Correct 91 ms 16596 KB Output is correct
4 Correct 95 ms 16680 KB Output is correct
5 Correct 93 ms 16724 KB Output is correct
6 Correct 88 ms 16720 KB Output is correct
7 Correct 87 ms 16700 KB Output is correct
8 Correct 95 ms 16720 KB Output is correct
9 Correct 87 ms 16720 KB Output is correct
10 Correct 90 ms 16704 KB Output is correct
11 Correct 96 ms 16716 KB Output is correct
12 Correct 95 ms 16720 KB Output is correct
13 Correct 91 ms 16720 KB Output is correct
14 Correct 89 ms 16596 KB Output is correct
15 Correct 88 ms 16596 KB Output is correct
16 Correct 90 ms 16700 KB Output is correct
17 Correct 90 ms 16704 KB Output is correct
18 Correct 88 ms 16704 KB Output is correct
19 Correct 109 ms 16704 KB Output is correct
20 Correct 89 ms 16708 KB Output is correct
21 Correct 112 ms 16724 KB Output is correct
22 Correct 88 ms 16708 KB Output is correct
23 Correct 94 ms 16736 KB Output is correct
24 Correct 96 ms 16752 KB Output is correct
25 Correct 95 ms 16724 KB Output is correct
26 Correct 92 ms 16748 KB Output is correct
27 Correct 94 ms 16736 KB Output is correct
28 Correct 101 ms 16724 KB Output is correct
29 Correct 95 ms 16736 KB Output is correct
30 Correct 100 ms 16748 KB Output is correct
31 Correct 96 ms 16736 KB Output is correct
32 Correct 94 ms 16732 KB Output is correct
33 Correct 170 ms 28460 KB Output is correct
34 Correct 164 ms 28468 KB Output is correct
35 Correct 161 ms 31432 KB Output is correct
36 Correct 145 ms 31484 KB Output is correct
37 Correct 153 ms 26688 KB Output is correct
38 Correct 126 ms 27576 KB Output is correct
39 Correct 139 ms 26592 KB Output is correct
40 Correct 129 ms 30480 KB Output is correct
41 Correct 147 ms 26620 KB Output is correct
42 Correct 154 ms 26728 KB Output is correct
43 Correct 119 ms 30948 KB Output is correct
44 Correct 112 ms 30904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 16724 KB Output is correct
2 Correct 95 ms 16724 KB Output is correct
3 Correct 86 ms 16708 KB Output is correct
4 Correct 96 ms 16832 KB Output is correct
5 Correct 88 ms 16708 KB Output is correct
6 Correct 89 ms 16728 KB Output is correct
7 Correct 94 ms 16708 KB Output is correct
8 Correct 90 ms 16704 KB Output is correct
9 Correct 98 ms 16716 KB Output is correct
10 Correct 90 ms 16596 KB Output is correct
11 Correct 93 ms 16708 KB Output is correct
12 Correct 92 ms 16724 KB Output is correct
13 Correct 91 ms 16724 KB Output is correct
14 Correct 90 ms 16720 KB Output is correct
15 Correct 87 ms 16724 KB Output is correct
16 Correct 91 ms 16716 KB Output is correct
17 Correct 91 ms 16708 KB Output is correct
18 Correct 95 ms 16724 KB Output is correct
19 Correct 93 ms 16704 KB Output is correct
20 Correct 94 ms 16716 KB Output is correct
21 Correct 97 ms 16716 KB Output is correct
22 Correct 89 ms 16704 KB Output is correct
23 Correct 92 ms 16740 KB Output is correct
24 Correct 93 ms 16740 KB Output is correct
25 Correct 92 ms 16752 KB Output is correct
26 Correct 100 ms 16764 KB Output is correct
27 Correct 99 ms 16748 KB Output is correct
28 Correct 93 ms 16752 KB Output is correct
29 Correct 96 ms 16744 KB Output is correct
30 Correct 94 ms 16752 KB Output is correct
31 Correct 98 ms 16820 KB Output is correct
32 Correct 93 ms 16740 KB Output is correct
33 Correct 430 ms 29260 KB Output is correct
34 Correct 370 ms 33020 KB Output is correct
35 Correct 467 ms 29256 KB Output is correct
36 Correct 455 ms 32900 KB Output is correct
37 Correct 179 ms 28464 KB Output is correct
38 Correct 174 ms 28636 KB Output is correct
39 Correct 151 ms 31960 KB Output is correct
40 Correct 143 ms 32168 KB Output is correct
41 Correct 174 ms 26640 KB Output is correct
42 Correct 119 ms 27556 KB Output is correct
43 Correct 142 ms 26568 KB Output is correct
44 Correct 136 ms 30520 KB Output is correct
45 Correct 142 ms 26572 KB Output is correct
46 Correct 142 ms 26676 KB Output is correct
47 Correct 115 ms 30900 KB Output is correct
48 Correct 110 ms 30920 KB Output is correct
49 Correct 649 ms 30552 KB Output is correct
50 Correct 662 ms 30580 KB Output is correct
51 Correct 471 ms 33124 KB Output is correct
52 Correct 292 ms 33228 KB Output is correct
53 Correct 671 ms 28816 KB Output is correct
54 Correct 334 ms 29456 KB Output is correct
55 Correct 520 ms 27664 KB Output is correct
56 Correct 405 ms 32348 KB Output is correct
57 Correct 475 ms 28204 KB Output is correct
58 Correct 543 ms 28728 KB Output is correct
59 Correct 107 ms 30920 KB Output is correct