Submission #229607

# Submission time Handle Problem Language Result Execution time Memory
229607 2020-05-05T10:07:07 Z mieszko11b Horses (IOI15_horses) C++14
100 / 100
321 ms 55184 KB
#include "horses.h"
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll MOD = ll(1e9) + 7;

ll pot(ll a, ll b) {
	if(b == 0)
		return 1;
	if(b % 2)
		return pot(a, b - 1) * a % MOD;
	ll tmp = pot(a, b / 2);
	return tmp * tmp % MOD;
}

ll inv(ll x) {
	return pot(x, MOD - 2);
}

struct SegmentTree {
	int lv;
	int d[1100007];
	
	SegmentTree(int n) {
		lv = 2;
		while(lv < n + 2)
			lv *= 2;
	}
	
	void insert(int i, int v) {
		int w = i + lv;
		d[w] = v;
		w /= 2;
		while(w) {
			d[w] = max(d[2 * w], d[2 * w + 1]);
			w /= 2;
		}
	}
	
	int query(int a, int b) {
		if(a > b) return 0;
		int va = a + lv;
		int vb = b + lv;
		int res = max(d[va], d[vb]);
		while(va / 2 != vb/  2) {
			if(va % 2 == 0)
				res = max(res, d[va + 1]);
			if(vb % 2 == 1)
				res = max(res, d[vb - 1]);
			va /= 2;
			vb /= 2;
		}
		return res;
	}
	
	void recalc() {
		for(int i = lv - 1 ; i >= 1 ; i--)
			d[i]  = max(d[2 * i], d[2 * i + 1]);
	}
};

int n;
int x[500007], y[500007];
ll all = 1;
int prv[500007], nxt[500007];
int max_to_nxt[500007];
set<int> S;
SegmentTree ST(500007);

int calc() {
	int i = *prev(S.end());
	ll pro = x[i];
	while(pro <= ll(1e9) && i > 0) {
		i = prv[i];
		pro *= x[i];
	}
	ll pref = all * inv(pro % MOD) % MOD * x[i] % MOD;
	ll local_res = 0, res;
	ll mul = 1;
	while(true) {
		ll a = mul * max_to_nxt[i];
		if(a > local_res) {
			local_res = a;
			res = a % MOD * pref % MOD;
		}
		if(!nxt[i])
			break;
		i = nxt[i];
		mul *= x[i];
	}
	
	return res % MOD;
}

int init(int N, int X[], int Y[]) {
	n = N;
	int lst = -1;
	int maxv = 0;
	for(int i = 0 ; i < n ; i++) {
		x[i] = X[i];
		//~ x1[i] = 1;
		y[i] = Y[i];
		if(x[i] > 1 || i == 0) {
			prv[i] = lst;
			if(lst != -1) {
				nxt[lst] = i;
				max_to_nxt[lst] = maxv;
			}
			maxv = 0;
			all *= x[i];
			all %= MOD;
			//~ x1[i] = inv(x[i]);
			lst = i;
			S.insert(i);
		}
		maxv = max(maxv, y[i]);
		ST.d[ST.lv + i] = y[i];
	}
	max_to_nxt[lst] = maxv;
	ST.recalc();
	
	return calc();
}

void remove_special(int pos) {
	if(pos == 0) return ;
	
	auto it = S.lower_bound(pos);
	int i1 = *prev(it);
	int i2 = 0;
	if(next(it) != S.end())
		i2 = *next(it);
	
	S.erase(pos);
	nxt[i1] = i2;
	if(i2) {
		prv[i2] = i1;
		max_to_nxt[i1] = ST.query(i1, i2 - 1);
	} else
		max_to_nxt[i1] = ST.query(i1, n - 1);
}

void add_special(int pos) {
	if(pos == 0) return ;
	
	auto it = S.lower_bound(pos);
	int i2 = 0;
	if(it != S.end())
		i2 = *it;
	int i1 = *prev(it);
	
	S.insert(pos);
	nxt[i1] = pos;
	nxt[pos] = i2;
	prv[pos] = i1;
	if(i2)
		prv[i2] = pos;
	max_to_nxt[i1] = ST.query(i1, pos - 1);
	if(i2)
		max_to_nxt[pos] = ST.query(pos, i2 - 1);
	else
		max_to_nxt[pos] = ST.query(pos, n - 1);
}

int updateX(int pos, int val) {	
	all = all * inv(x[pos]) % MOD * val % MOD;
	
	if(x[pos] == 1 && val > 1) {
		x[pos] = val;
		add_special(pos);
	} else if(x[pos] > 1 && val == 1) {
		x[pos] = val;
		remove_special(pos);
	} else
		x[pos] = val;
	
	return calc();
}

int updateY(int pos, int val) {
	y[pos] = val;
	ST.insert(pos, val);
	auto it = S.upper_bound(pos);
	int i = *prev(it);
	if(nxt[i])
		max_to_nxt[i] = ST.query(i, nxt[i] - 1);
	else
		max_to_nxt[i] = ST.query(i, n - 1);
	
	return calc();
}

Compilation message

horses.cpp: In function 'int calc()':
horses.cpp:95:13: warning: conversion to 'int' from 'll {aka long long int}' may alter its value [-Wconversion]
  return res % MOD;
         ~~~~^~~~~
horses.cpp:95:15: warning: 'res' may be used uninitialized in this function [-Wmaybe-uninitialized]
  return res % MOD;
               ^~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2432 KB Output is correct
2 Correct 6 ms 2432 KB Output is correct
3 Correct 6 ms 2432 KB Output is correct
4 Correct 6 ms 2432 KB Output is correct
5 Correct 6 ms 2432 KB Output is correct
6 Correct 6 ms 2432 KB Output is correct
7 Correct 6 ms 2432 KB Output is correct
8 Correct 6 ms 2432 KB Output is correct
9 Correct 6 ms 2432 KB Output is correct
10 Correct 6 ms 2432 KB Output is correct
11 Correct 6 ms 2432 KB Output is correct
12 Correct 6 ms 2432 KB Output is correct
13 Correct 7 ms 2432 KB Output is correct
14 Correct 6 ms 2432 KB Output is correct
15 Correct 6 ms 2432 KB Output is correct
16 Correct 7 ms 2432 KB Output is correct
17 Correct 6 ms 2432 KB Output is correct
18 Correct 6 ms 2432 KB Output is correct
19 Correct 6 ms 2432 KB Output is correct
20 Correct 6 ms 2432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2432 KB Output is correct
2 Correct 6 ms 2432 KB Output is correct
3 Correct 6 ms 2432 KB Output is correct
4 Correct 7 ms 2432 KB Output is correct
5 Correct 6 ms 2432 KB Output is correct
6 Correct 6 ms 2432 KB Output is correct
7 Correct 8 ms 2432 KB Output is correct
8 Correct 6 ms 2432 KB Output is correct
9 Correct 6 ms 2432 KB Output is correct
10 Correct 6 ms 2432 KB Output is correct
11 Correct 6 ms 2356 KB Output is correct
12 Correct 6 ms 2432 KB Output is correct
13 Correct 6 ms 2432 KB Output is correct
14 Correct 6 ms 2432 KB Output is correct
15 Correct 6 ms 2432 KB Output is correct
16 Correct 6 ms 2432 KB Output is correct
17 Correct 6 ms 2432 KB Output is correct
18 Correct 6 ms 2432 KB Output is correct
19 Correct 6 ms 2432 KB Output is correct
20 Correct 6 ms 2432 KB Output is correct
21 Correct 6 ms 2432 KB Output is correct
22 Correct 6 ms 2432 KB Output is correct
23 Correct 7 ms 2560 KB Output is correct
24 Correct 7 ms 2560 KB Output is correct
25 Correct 8 ms 2560 KB Output is correct
26 Correct 7 ms 2560 KB Output is correct
27 Correct 7 ms 2432 KB Output is correct
28 Correct 7 ms 2560 KB Output is correct
29 Correct 7 ms 2432 KB Output is correct
30 Correct 7 ms 2560 KB Output is correct
31 Correct 7 ms 2560 KB Output is correct
32 Correct 7 ms 2432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 223 ms 42492 KB Output is correct
2 Correct 321 ms 42556 KB Output is correct
3 Correct 272 ms 42600 KB Output is correct
4 Correct 236 ms 42488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2432 KB Output is correct
2 Correct 6 ms 2432 KB Output is correct
3 Correct 6 ms 2432 KB Output is correct
4 Correct 6 ms 2432 KB Output is correct
5 Correct 6 ms 2432 KB Output is correct
6 Correct 6 ms 2432 KB Output is correct
7 Correct 6 ms 2304 KB Output is correct
8 Correct 6 ms 2432 KB Output is correct
9 Correct 6 ms 2432 KB Output is correct
10 Correct 6 ms 2432 KB Output is correct
11 Correct 6 ms 2432 KB Output is correct
12 Correct 6 ms 2432 KB Output is correct
13 Correct 6 ms 2432 KB Output is correct
14 Correct 7 ms 2432 KB Output is correct
15 Correct 6 ms 2432 KB Output is correct
16 Correct 6 ms 2432 KB Output is correct
17 Correct 6 ms 2432 KB Output is correct
18 Correct 6 ms 2432 KB Output is correct
19 Correct 7 ms 2432 KB Output is correct
20 Correct 6 ms 2432 KB Output is correct
21 Correct 6 ms 2432 KB Output is correct
22 Correct 6 ms 2432 KB Output is correct
23 Correct 7 ms 2560 KB Output is correct
24 Correct 7 ms 2560 KB Output is correct
25 Correct 7 ms 2560 KB Output is correct
26 Correct 8 ms 2560 KB Output is correct
27 Correct 7 ms 2560 KB Output is correct
28 Correct 7 ms 2560 KB Output is correct
29 Correct 7 ms 2432 KB Output is correct
30 Correct 8 ms 2560 KB Output is correct
31 Correct 7 ms 2432 KB Output is correct
32 Correct 7 ms 2560 KB Output is correct
33 Correct 44 ms 22264 KB Output is correct
34 Correct 44 ms 22136 KB Output is correct
35 Correct 186 ms 52472 KB Output is correct
36 Correct 183 ms 52472 KB Output is correct
37 Correct 35 ms 17528 KB Output is correct
38 Correct 92 ms 33144 KB Output is correct
39 Correct 30 ms 14336 KB Output is correct
40 Correct 163 ms 47480 KB Output is correct
41 Correct 29 ms 14592 KB Output is correct
42 Correct 30 ms 14464 KB Output is correct
43 Correct 155 ms 47992 KB Output is correct
44 Correct 161 ms 47992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2432 KB Output is correct
2 Correct 6 ms 2432 KB Output is correct
3 Correct 7 ms 2432 KB Output is correct
4 Correct 6 ms 2432 KB Output is correct
5 Correct 6 ms 2432 KB Output is correct
6 Correct 7 ms 2432 KB Output is correct
7 Correct 6 ms 2432 KB Output is correct
8 Correct 6 ms 2432 KB Output is correct
9 Correct 6 ms 2432 KB Output is correct
10 Correct 6 ms 2432 KB Output is correct
11 Correct 6 ms 2432 KB Output is correct
12 Correct 6 ms 2432 KB Output is correct
13 Correct 6 ms 2432 KB Output is correct
14 Correct 6 ms 2432 KB Output is correct
15 Correct 6 ms 2432 KB Output is correct
16 Correct 6 ms 2432 KB Output is correct
17 Correct 6 ms 2432 KB Output is correct
18 Correct 6 ms 2432 KB Output is correct
19 Correct 6 ms 2432 KB Output is correct
20 Correct 6 ms 2432 KB Output is correct
21 Correct 6 ms 2432 KB Output is correct
22 Correct 6 ms 2432 KB Output is correct
23 Correct 7 ms 2560 KB Output is correct
24 Correct 9 ms 2560 KB Output is correct
25 Correct 8 ms 2688 KB Output is correct
26 Correct 8 ms 2560 KB Output is correct
27 Correct 7 ms 2432 KB Output is correct
28 Correct 7 ms 2560 KB Output is correct
29 Correct 7 ms 2432 KB Output is correct
30 Correct 7 ms 2560 KB Output is correct
31 Correct 7 ms 2432 KB Output is correct
32 Correct 7 ms 2432 KB Output is correct
33 Correct 232 ms 46356 KB Output is correct
34 Correct 313 ms 55184 KB Output is correct
35 Correct 288 ms 46328 KB Output is correct
36 Correct 237 ms 50168 KB Output is correct
37 Correct 45 ms 22264 KB Output is correct
38 Correct 44 ms 22136 KB Output is correct
39 Correct 182 ms 52472 KB Output is correct
40 Correct 182 ms 52600 KB Output is correct
41 Correct 37 ms 17536 KB Output is correct
42 Correct 92 ms 33144 KB Output is correct
43 Correct 30 ms 14336 KB Output is correct
44 Correct 168 ms 47480 KB Output is correct
45 Correct 30 ms 14592 KB Output is correct
46 Correct 31 ms 14464 KB Output is correct
47 Correct 156 ms 47904 KB Output is correct
48 Correct 156 ms 47992 KB Output is correct
49 Correct 151 ms 25208 KB Output is correct
50 Correct 149 ms 25208 KB Output is correct
51 Correct 268 ms 54456 KB Output is correct
52 Correct 257 ms 54008 KB Output is correct
53 Correct 150 ms 20600 KB Output is correct
54 Correct 187 ms 37112 KB Output is correct
55 Correct 107 ms 15480 KB Output is correct
56 Correct 280 ms 49400 KB Output is correct
57 Correct 98 ms 16248 KB Output is correct
58 Correct 115 ms 16504 KB Output is correct
59 Correct 156 ms 47992 KB Output is correct