Submission #785533

# Submission time Handle Problem Language Result Execution time Memory
785533 2023-07-17T10:03:32 Z NothingXD Horses (IOI15_horses) C++17
100 / 100
175 ms 30184 KB
#include "horses.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef complex<ld> point;

typedef __uint128_t L;
struct FastMod {
	ull b, m;
	FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
	ull reduce(ull a) {
		ull q = (ull)((L(m) * a) >> 64);
		ull r = a - q * b; // can be proven that 0 <= r < 2*b
		return r >= b ? r - b : r;
	}
};
FastMod FM(1e9+7);


void debug_out(){cerr << endl;}

template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T){
	cout << H << " ";
	debug_out(T...);
}

#define debug(...) cerr << "(" << #__VA_ARGS__ << "): ", debug_out(__VA_ARGS__)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)

const int maxn = 5e5 + 10;
const int mod = 1e9 + 7;

int n;
ll x[maxn], y[maxn];
ll dp[maxn << 2][2];
bool of[maxn << 2];

int find(int v, int l, int r, int tmp){
	if (!of[v] && 1ll * tmp * dp[v][1] < mod) return -1;
	if (l + 1 == r) return l;
	int mid = (l + r) >> 1;
	int lc = v << 1;
	int rc = lc | 1;
	int res = find(rc, mid, r, tmp);
	if (res == -1){
		tmp *= dp[rc][1];
		res = find(lc, l, mid, tmp);
	}
	return res;
}

void build2(int v, int l, int r){
	if (l + 1 == r){
		dp[v][0] = x[l] * y[l];
		dp[v][1] = x[l];
		return;
	}
	int mid = (l + r) >> 1;
	int lc = v << 1;
	int rc = lc | 1;
	build2(lc, l, mid);
	build2(rc, mid, r);
	dp[v][0] = max(dp[lc][0], dp[lc][1] * dp[rc][0]);
	dp[v][1] = dp[lc][1] * dp[rc][1];
	if (dp[v][1] >= mod || of[lc] || of[rc]) of[v] = true;
	dp[v][1] = FM.reduce(dp[v][1]);
}

void change2(int v, int l, int r, int idx){
	if (l + 1 == r){
		dp[v][0] = x[l] * y[l];
		dp[v][1] = x[l];
		return;
	}
	int mid = (l + r) >> 1;
	int lc = v << 1;
	int rc = lc | 1;
	if (idx < mid) change2(lc, l, mid, idx);
	else change2(rc, mid, r, idx);
	dp[v][0] = max(dp[lc][0], dp[lc][1] * dp[rc][0]);
	dp[v][1] = dp[lc][1] * dp[rc][1];
	if (dp[v][1] >= mod || of[lc] || of[rc]) of[v] = true;
	else of[v] = false;
	dp[v][1] = FM.reduce(dp[v][1]);
}

pll get2(int v, int l, int r, int ql, int qr){
	if (ql <= l && r <= qr) return MP(dp[v][0], dp[v][1]);
	int mid = (l + r) >> 1;
	int lc = v << 1;
	int rc = lc | 1;
	pll a = {0, 1};
	pll b = {0, 1};
	if (qr > l && ql < mid) a = get2(lc, l, mid, ql, qr);
	if (qr > mid && ql < r) b = get2(rc, mid, r, ql, qr);
	return MP(max(a.F, a.S * b.F), FM.reduce(a.S * b.S));
}

int Calc(){
	int idx = find(1, 0, n, 1);
	if (idx == -1) idx = 0;
	assert(n == 1 || idx < n-1);
	int tmp = get2(1, 0, n, 0, idx+1).S;
	ll tmp2 = FM.reduce(max(y[idx], get2(1, 0, n, idx+1, n).F));
	return FM.reduce(1ll * tmp * tmp2);
}

int init(int N, int X[], int Y[]) {
	n = N;
	for (int i = 0; i < n; i++){
		x[i] = X[i];
		y[i] = Y[i];
	}
	build2(1, 0, n);
	return Calc();
}

int updateX(int pos, int val) {
	x[pos] = val;
	change2(1, 0, n, pos);
	return Calc();
}

int updateY(int pos, int val) {
	y[pos] = val;
	change2(1, 0, n, pos);
	return Calc();
}

Compilation message

horses.cpp: In constructor 'FastMod::FastMod(ull)':
horses.cpp:16:14: warning: declaration of 'b' shadows a member of 'FastMod' [-Wshadow]
   16 |  FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
      |          ~~~~^
horses.cpp:15:6: note: shadowed declaration is here
   15 |  ull b, m;
      |      ^
horses.cpp: In constructor 'FastMod::FastMod(ull)':
horses.cpp:16:14: warning: declaration of 'b' shadows a member of 'FastMod' [-Wshadow]
   16 |  FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
      |          ~~~~^
horses.cpp:15:6: note: shadowed declaration is here
   15 |  ull b, m;
      |      ^
horses.cpp: In constructor 'FastMod::FastMod(ull)':
horses.cpp:16:14: warning: declaration of 'b' shadows a member of 'FastMod' [-Wshadow]
   16 |  FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
      |          ~~~~^
horses.cpp:15:6: note: shadowed declaration is here
   15 |  ull b, m;
      |      ^
horses.cpp: In function 'int find(int, int, int, int)':
horses.cpp:56:7: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
   56 |   tmp *= dp[rc][1];
      |   ~~~~^~~~~~~~~~~~
horses.cpp: In function 'int Calc()':
horses.cpp:36:11: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   36 | #define S second
      |           ^
horses.cpp:113:36: note: in expansion of macro 'S'
  113 |  int tmp = get2(1, 0, n, 0, idx+1).S;
      |                                    ^
horses.cpp:115:18: warning: conversion from 'ull' {aka 'long long unsigned int'} to 'int' may change value [-Wconversion]
  115 |  return FM.reduce(1ll * tmp * tmp2);
      |         ~~~~~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 312 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 312 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 308 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 224 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 304 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 308 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 316 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 308 KB Output is correct
18 Correct 1 ms 312 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 324 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 416 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 29636 KB Output is correct
2 Correct 159 ms 30160 KB Output is correct
3 Correct 111 ms 30036 KB Output is correct
4 Correct 129 ms 30080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 312 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 324 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 328 KB Output is correct
33 Correct 40 ms 29132 KB Output is correct
34 Correct 41 ms 29088 KB Output is correct
35 Correct 61 ms 29132 KB Output is correct
36 Correct 66 ms 29176 KB Output is correct
37 Correct 39 ms 29120 KB Output is correct
38 Correct 33 ms 28680 KB Output is correct
39 Correct 30 ms 28660 KB Output is correct
40 Correct 44 ms 29164 KB Output is correct
41 Correct 28 ms 29040 KB Output is correct
42 Correct 29 ms 28648 KB Output is correct
43 Correct 39 ms 29012 KB Output is correct
44 Correct 40 ms 29048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 308 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 312 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 312 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 324 KB Output is correct
24 Correct 1 ms 324 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 320 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 86 ms 29604 KB Output is correct
34 Correct 175 ms 30080 KB Output is correct
35 Correct 111 ms 30096 KB Output is correct
36 Correct 137 ms 30028 KB Output is correct
37 Correct 45 ms 29176 KB Output is correct
38 Correct 41 ms 29156 KB Output is correct
39 Correct 63 ms 29188 KB Output is correct
40 Correct 61 ms 29132 KB Output is correct
41 Correct 31 ms 29196 KB Output is correct
42 Correct 32 ms 28668 KB Output is correct
43 Correct 32 ms 28688 KB Output is correct
44 Correct 43 ms 29164 KB Output is correct
45 Correct 28 ms 29004 KB Output is correct
46 Correct 29 ms 28736 KB Output is correct
47 Correct 40 ms 29016 KB Output is correct
48 Correct 39 ms 29044 KB Output is correct
49 Correct 116 ms 30028 KB Output is correct
50 Correct 129 ms 30184 KB Output is correct
51 Correct 123 ms 30028 KB Output is correct
52 Correct 114 ms 30100 KB Output is correct
53 Correct 115 ms 30000 KB Output is correct
54 Correct 92 ms 29696 KB Output is correct
55 Correct 72 ms 28872 KB Output is correct
56 Correct 108 ms 30028 KB Output is correct
57 Correct 74 ms 29772 KB Output is correct
58 Correct 77 ms 29604 KB Output is correct
59 Correct 40 ms 29112 KB Output is correct