Submission #852978

# Submission time Handle Problem Language Result Execution time Memory
852978 2023-09-23T09:45:51 Z aykhn Horses (IOI15_horses) C++14
100 / 100
960 ms 89128 KB
#include "horses.h"
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define pb push_back
#define ins insert
#define infll 0x3F3F3F3F3F3F3F3F
#define inf 0x3F3F3F3F
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mpr make_pair
#define all(v) v.begin(), v.end()
#define fi first
#define se second

const ll mod = 1e9 + 7;

int n;
vector<ll> a, b;
set<ll> s;
multiset<ll> ms;

ll mult(ll a, ll b)
{
	return (a * b) % mod;
}

struct SegTree
{
	int sz;
	vector<ll> st;
	void build(int l, int r, int x)
	{
		if (l + 1 == r)
		{
			if (l < n) st[x] = a[l];
			return;
		}
		int mid = (l + r) >> 1;
		build(l, mid, 2*x + 1);
		build(mid, r, 2*x + 2);
		st[x] = mult(st[2*x + 1], st[2*x + 2]);
	}
	void build(int n)
	{
		sz = 1;
		while (sz < n) sz <<= 1;
		st.assign(sz << 1, 1);
		build(0, sz, 0);
	}
	void make(int ind, ll val, int l, int r, int x)
	{
		if (l + 1 == r)
		{
			st[x] = val;
			return;
		}
		int mid = (l + r) >> 1;
		if (ind < mid) make(ind, val, l, mid, 2*x + 1);
		else make(ind, val, mid, r, 2*x + 2);
		st[x] = mult(st[2*x + 1], st[2*x + 2]);
	}
	void make(int ind, ll val)
	{
		make(ind, val, 0, sz, 0);
	}
	ll get(int lx, int rx, int l, int r, int x)
	{
		if (l >= rx || r <= lx) return 1;
		if (l >= lx && r <= rx) return st[x];
		int mid = (l + r) >> 1;
		return mult(get(lx, rx, l, mid, 2*x + 1), get(lx, rx, mid, r, 2*x + 2));
	}
	ll get(int l, int r)
	{
		return get(l, r + 1, 0, sz, 0);
	}
};
struct SegTreeMX
{
	int sz;
	vector<ll> st;
	void build(int l, int r, int x)
	{
		if (l + 1 == r)
		{
			if (l < n) st[x] = b[l];
			return;
		}
		int mid = (l + r) >> 1;
		build(l, mid, 2*x + 1);
		build(mid, r, 2*x + 2);
		st[x] = max(st[2*x + 1], st[2*x + 2]);
	}
	void build(int n)
	{
		sz = 1;
		while (sz < n) sz <<= 1;
		st.assign(sz << 1, 1);
		build(0, sz, 0);
	}
	void make(int ind, ll val, int l, int r, int x)
	{
		if (l + 1 == r)
		{
			st[x] = val;
			return;
		}
		int mid = (l + r) >> 1;
		if (ind < mid) make(ind, val, l, mid, 2*x + 1);
		else make(ind, val, mid, r, 2*x + 2);
		st[x] = max(st[2*x + 1], st[2*x + 2]);
	}
	void make(int ind, ll val)
	{
		make(ind, val, 0, sz, 0);
	}
	ll get(int lx, int rx, int l, int r, int x)
	{
		if (l >= rx || r <= lx) return 1;
		if (l >= lx && r <= rx) return st[x];
		int mid = (l + r) >> 1;
		return max(get(lx, rx, l, mid, 2*x + 1), get(lx, rx, mid, r, 2*x + 2));
	}
	ll get(int l, int r)
	{
		return get(l, r + 1, 0, sz, 0);
	}
};

SegTree st;
SegTreeMX stmx;

int getans()
{
	ll ans = *ms.rbegin();
	if (s.empty()) return ans;
	vector<int> v;
	int k = 1;
	for (auto it = s.rbegin(); k <= 30 && it != s.rend(); it++, k++) v.pb(*it);
	reverse(all(v));
	ll mxb = stmx.get(v[0], (1 == v.size() ? n - 1 : v[1] - 1));
	ll x = 1;
	ll INF = 1e15;
	int f = 0;
	int ind = 0;
	for (int i = 1; i < v.size(); i++)
	{
		if (a[v[i]] >= INF/x) f = 1;
		if (!f) x *= a[v[i]];
		ll bb = stmx.get(v[i], n - 1);
		if (!f && x >= INF/bb) f = 1;
		if (!f) x *= bb;
		if (f || x > mxb) 
		{
			ind = i;
			mxb = bb;
			f = 0;
			x = 1;
		}
		else x /= bb;
	}
	int szs = s.size();
	int szv = v.size();
	if (szs - (szv - ind) >= 30) return mult(st.get(0, v[ind]), stmx.get(v[ind], n - 1));
	f = 0;
	x = 1;
	for (auto it = s.begin(); it != s.end() && *it <= v[ind]; it++)
	{
		if (a[*it] >= INF/x) f = 1;
		if (!f) x *= a[*it];
	}
	ll bb = stmx.get(v[ind], n - 1);
	if (bb >= INF/x) f = 1;
	if (!f) x *= bb;
	if (f || x > ans) return mult(st.get(0, v[ind]), stmx.get(v[ind], n - 1));
	else return ans;
}

int init(int N, int X[], int Y[]) 
{
	n = N;
	for (int i = 0; i < N; i++) 
	{
		a.pb(X[i]), b.pb(Y[i]);
		if (X[i] > 1) s.ins(i);
		ms.ins(Y[i]);
	}
	st.build(n + 1000);
	stmx.build(n + 1000);
	return getans();
}

int updateX(int pos, int val) 
{	
	if (a[pos] > 1) s.erase(pos);
	a[pos] = val;
	if (a[pos] > 1) s.ins(pos);
	st.make(pos, val);
	return getans();
}

int updateY(int pos, int val) 
{
	ms.erase(ms.lower_bound(b[pos]));
	ms.ins(val);
	b[pos] = val;
	stmx.make(pos, val);
	return getans();
}

// ans will always be max i such that P [1, i] * y[i] is max
// comparing 2 answers (i < j)

// P[1, i] * y[i] < P[1, j] * y[j];
// y[i] < P[1, j] * y[j] / P[1, i];
// y[i] < y[j] * P[i + 1, j];
// if (num>1(j - i) + (y[j] > 1) >= 30) true;
// else calculate

Compilation message

horses.cpp: In function 'll mult(ll, ll)':
horses.cpp:25:18: warning: declaration of 'b' shadows a global declaration [-Wshadow]
   25 | ll mult(ll a, ll b)
      |               ~~~^
horses.cpp:21:15: note: shadowed declaration is here
   21 | vector<ll> a, b;
      |               ^
horses.cpp:25:12: warning: declaration of 'a' shadows a global declaration [-Wshadow]
   25 | ll mult(ll a, ll b)
      |         ~~~^
horses.cpp:21:12: note: shadowed declaration is here
   21 | vector<ll> a, b;
      |            ^
horses.cpp: In member function 'void SegTree::build(int)':
horses.cpp:46:17: warning: declaration of 'n' shadows a global declaration [-Wshadow]
   46 |  void build(int n)
      |             ~~~~^
horses.cpp:20:5: note: shadowed declaration is here
   20 | int n;
      |     ^
horses.cpp: In member function 'void SegTreeMX::build(int)':
horses.cpp:97:17: warning: declaration of 'n' shadows a global declaration [-Wshadow]
   97 |  void build(int n)
      |             ~~~~^
horses.cpp:20:5: note: shadowed declaration is here
   20 | int n;
      |     ^
horses.cpp: In function 'int getans()':
horses.cpp:139:24: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  139 |  if (s.empty()) return ans;
      |                        ^~~
horses.cpp:142:72: warning: conversion from 'long long int' to 'std::vector<int>::value_type' {aka 'int'} may change value [-Wconversion]
  142 |  for (auto it = s.rbegin(); k <= 30 && it != s.rend(); it++, k++) v.pb(*it);
      |                                                                        ^~~
horses.cpp:149:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  149 |  for (int i = 1; i < v.size(); i++)
      |                  ~~^~~~~~~~~~
horses.cpp:165:18: warning: conversion from 'std::set<long long int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  165 |  int szs = s.size();
      |            ~~~~~~^~
horses.cpp:166:18: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  166 |  int szv = v.size();
      |            ~~~~~~^~
horses.cpp:167:42: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  167 |  if (szs - (szv - ind) >= 30) return mult(st.get(0, v[ind]), stmx.get(v[ind], n - 1));
      |                                      ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
horses.cpp:178:31: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  178 |  if (f || x > ans) return mult(st.get(0, v[ind]), stmx.get(v[ind], n - 1));
      |                           ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
horses.cpp:179:14: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  179 |  else return ans;
      |              ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 600 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 3 ms 348 KB Output is correct
24 Correct 3 ms 600 KB Output is correct
25 Correct 3 ms 600 KB Output is correct
26 Correct 3 ms 604 KB Output is correct
27 Correct 3 ms 604 KB Output is correct
28 Correct 5 ms 600 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 3 ms 600 KB Output is correct
31 Correct 2 ms 604 KB Output is correct
32 Correct 3 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 546 ms 78040 KB Output is correct
2 Correct 684 ms 89120 KB Output is correct
3 Correct 689 ms 80480 KB Output is correct
4 Correct 930 ms 84256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 3 ms 348 KB Output is correct
24 Correct 3 ms 344 KB Output is correct
25 Correct 4 ms 600 KB Output is correct
26 Correct 3 ms 600 KB Output is correct
27 Correct 3 ms 600 KB Output is correct
28 Correct 3 ms 600 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 3 ms 600 KB Output is correct
31 Correct 2 ms 600 KB Output is correct
32 Correct 3 ms 600 KB Output is correct
33 Correct 350 ms 56060 KB Output is correct
34 Correct 328 ms 56096 KB Output is correct
35 Correct 300 ms 86300 KB Output is correct
36 Correct 318 ms 86308 KB Output is correct
37 Correct 198 ms 54568 KB Output is correct
38 Correct 367 ms 66936 KB Output is correct
39 Correct 149 ms 54092 KB Output is correct
40 Correct 323 ms 81544 KB Output is correct
41 Correct 169 ms 54056 KB Output is correct
42 Correct 165 ms 54052 KB Output is correct
43 Correct 262 ms 81800 KB Output is correct
44 Correct 286 ms 81740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 356 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 4 ms 344 KB Output is correct
24 Correct 3 ms 856 KB Output is correct
25 Correct 3 ms 600 KB Output is correct
26 Correct 3 ms 600 KB Output is correct
27 Correct 3 ms 856 KB Output is correct
28 Correct 4 ms 600 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 3 ms 604 KB Output is correct
31 Correct 3 ms 348 KB Output is correct
32 Correct 3 ms 600 KB Output is correct
33 Correct 543 ms 80236 KB Output is correct
34 Correct 653 ms 89128 KB Output is correct
35 Correct 631 ms 80420 KB Output is correct
36 Correct 960 ms 84772 KB Output is correct
37 Correct 358 ms 56220 KB Output is correct
38 Correct 355 ms 56032 KB Output is correct
39 Correct 292 ms 86448 KB Output is correct
40 Correct 292 ms 86304 KB Output is correct
41 Correct 156 ms 54396 KB Output is correct
42 Correct 386 ms 67204 KB Output is correct
43 Correct 138 ms 54144 KB Output is correct
44 Correct 290 ms 81740 KB Output is correct
45 Correct 187 ms 54156 KB Output is correct
46 Correct 153 ms 54528 KB Output is correct
47 Correct 251 ms 81692 KB Output is correct
48 Correct 249 ms 81700 KB Output is correct
49 Correct 780 ms 59084 KB Output is correct
50 Correct 692 ms 59260 KB Output is correct
51 Correct 559 ms 88352 KB Output is correct
52 Correct 536 ms 87792 KB Output is correct
53 Correct 583 ms 57656 KB Output is correct
54 Correct 682 ms 71020 KB Output is correct
55 Correct 226 ms 55328 KB Output is correct
56 Correct 749 ms 83384 KB Output is correct
57 Correct 375 ms 55692 KB Output is correct
58 Correct 426 ms 56284 KB Output is correct
59 Correct 362 ms 81864 KB Output is correct