Submission #285491

# Submission time Handle Problem Language Result Execution time Memory
285491 2020-08-29T07:11:37 Z kevlee Horses (IOI15_horses) C++17
100 / 100
730 ms 68728 KB
#include "horses.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mod 1000000007
#define h1 7897897897897897
#define h2 7897466719774591
#define b1 98762051
#define b2 98765431
#define inf 1000000000
#define pi 3.1415926535897932384626
#define LMAX 9223372036854775807
#define ll long long
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vp vector<pii>
#define SET(a, b) memset(a, b, sizeof(a));
#define all(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
int n, t[40];
ll x[500005], y[500005], bit[500005];
pll seg[2000005];
set <int> s;
void build(int id, int l, int r) {
  if (l == r) {
    seg[id] = {y[l], l};
    return;
  }
  int mid = (l + r) >> 1;
  build(id * 2, l, mid);
  build(id * 2 + 1, mid + 1, r);
  if (seg[id * 2 + 1].fi == max(seg[id * 2].fi, seg[id * 2 + 1].fi)) seg[id] = seg[id * 2 + 1];
  else seg[id] = seg[id * 2];
}
void point_update(int id, int l, int r, int pos) {
  if (l == r) {
    seg[id] = {y[l], l};
    return;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid) point_update(id * 2, l, mid, pos);
  else point_update(id * 2 + 1, mid + 1, r, pos);
  if (seg[id * 2 + 1].fi == max(seg[id * 2].fi, seg[id * 2 + 1].fi)) seg[id] = seg[id * 2 + 1];
  else seg[id] = seg[id * 2];
}
pll range_max(int id, int l, int r, int lq, int rq) {
  if (lq > r || rq < l) return {0, -1};
  if (lq <= l && r <= rq) return seg[id];
  int mid = (l + r) >> 1;
  pll L = range_max(id * 2, l, mid, lq, rq);
  pll R = range_max(id * 2 + 1, mid + 1, r, lq, rq);
  if (R.fi >= L.fi) return R;
  return L;
}
ll p(ll b, ll x) {
  if (x == 0) return 1;
  if (x == 1) return b % mod;
  ll tmp = p(b, x/2);
  if (x % 2 == 0) return tmp * tmp % mod;
  return (tmp * tmp) % mod * b % mod;
}
void update(int x, ll val) {
  for (x; x <= n; x += (x&(-x))) {
    bit[x] = bit[x] * val % mod;
  }
}
ll query(int x) {
  ll ret = 1;
  for (x; x > 0; x -= (x&(-x))) {
    ret = ret * bit[x] % mod;
  }
  return ret;
}
int calc(int pos) {
  ll ret = query(pos) * y[pos] % mod;
  return ret;
}
int solve() {
  int cur = 31;
  for (auto it = s.rbegin(); it != s.rend(); it++) {
    t[cur] = *it;
    cur--;
    if (cur < 0) break;
  }
  pll p = range_max(1, 1, n, 1, n);
  int bestpos = p.se;
  ll prod = 1;
  FOR(i, cur + 1, 31) {
    if (t[i] <= bestpos) continue;
    prod *= x[t[i]];
    if (prod > y[bestpos]) {
      bestpos = t[i];
      prod = 1;
    }
    ll required = y[bestpos] / prod + 1;
    pll res = range_max(1, 1, n, t[i], n);
    if (res.fi >= required) {
      bestpos = res.se;
      prod = 1;
    }
  }
  //cout << bestpos << endl;
  return calc(bestpos);
}
int init(int N, int X[], int Y[]) {
	n = N;
  FOR(i, 0, n-1) {
    x[i + 1] = X[i];
    y[i + 1] = Y[i];
    bit[i + 1] = 1;
  }
  FOR(i, 1, n) {
    update(i, x[i]);
    if (x[i] > 1) s.insert(i);
  }
  build(1, 1, n);
  return solve();
}

int updateX(int pos, int val) {	
  pos++;
	ll mul = 1LL * val * p(x[pos], mod - 2) % mod;
  update(pos, mul);
  if (x[pos] == 1 && val > 1) {
    s.insert(pos);
  } else if (x[pos] > 1 && val == 1) {
    s.erase(pos);
  }
  x[pos] = val;
  return solve();
}

int updateY(int pos, int val) {
  pos++;
	y[pos] = val;
  point_update(1, 1, n, pos);
  return solve();
}

Compilation message

horses.cpp: In function 'long long int p(long long int, long long int)':
horses.cpp:60:16: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   60 | ll p(ll b, ll x) {
      |                ^
horses.cpp:26:4: note: shadowed declaration is here
   26 | ll x[500005], y[500005], bit[500005];
      |    ^
horses.cpp: In function 'void update(int, long long int)':
horses.cpp:67:26: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   67 | void update(int x, ll val) {
      |                          ^
horses.cpp:26:4: note: shadowed declaration is here
   26 | ll x[500005], y[500005], bit[500005];
      |    ^
horses.cpp:68:8: warning: statement has no effect [-Wunused-value]
   68 |   for (x; x <= n; x += (x&(-x))) {
      |        ^
horses.cpp: In function 'long long int query(int)':
horses.cpp:72:15: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   72 | ll query(int x) {
      |               ^
horses.cpp:26:4: note: shadowed declaration is here
   26 | ll x[500005], y[500005], bit[500005];
      |    ^
horses.cpp:74:8: warning: statement has no effect [-Wunused-value]
   74 |   for (x; x > 0; x -= (x&(-x))) {
      |        ^
horses.cpp: In function 'int calc(int)':
horses.cpp:81:10: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   81 |   return ret;
      |          ^~~
horses.cpp: In function 'int solve()':
horses.cpp:15:12: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   15 | #define se second
      |            ^
horses.cpp:91:19: note: in expansion of macro 'se'
   91 |   int bestpos = p.se;
      |                   ^~
horses.cpp:15:12: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   15 | #define se second
      |            ^
horses.cpp:103:21: note: in expansion of macro 'se'
  103 |       bestpos = res.se;
      |                     ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 288 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 0 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 512 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 4 ms 384 KB Output is correct
28 Correct 2 ms 512 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 3 ms 384 KB Output is correct
32 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 720 ms 60676 KB Output is correct
2 Correct 303 ms 61176 KB Output is correct
3 Correct 452 ms 60664 KB Output is correct
4 Correct 324 ms 61176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 4 ms 384 KB Output is correct
28 Correct 2 ms 512 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 3 ms 384 KB Output is correct
32 Correct 4 ms 384 KB Output is correct
33 Correct 80 ms 36600 KB Output is correct
34 Correct 75 ms 36600 KB Output is correct
35 Correct 216 ms 66936 KB Output is correct
36 Correct 203 ms 66964 KB Output is correct
37 Correct 123 ms 34748 KB Output is correct
38 Correct 124 ms 47480 KB Output is correct
39 Correct 50 ms 34424 KB Output is correct
40 Correct 184 ms 61944 KB Output is correct
41 Correct 79 ms 34552 KB Output is correct
42 Correct 102 ms 34552 KB Output is correct
43 Correct 180 ms 62328 KB Output is correct
44 Correct 177 ms 62328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 2 ms 512 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 3 ms 384 KB Output is correct
32 Correct 4 ms 384 KB Output is correct
33 Correct 656 ms 60792 KB Output is correct
34 Correct 327 ms 61524 KB Output is correct
35 Correct 424 ms 60664 KB Output is correct
36 Correct 356 ms 61688 KB Output is correct
37 Correct 82 ms 36600 KB Output is correct
38 Correct 76 ms 36604 KB Output is correct
39 Correct 214 ms 66936 KB Output is correct
40 Correct 205 ms 66808 KB Output is correct
41 Correct 124 ms 34680 KB Output is correct
42 Correct 118 ms 47608 KB Output is correct
43 Correct 51 ms 34424 KB Output is correct
44 Correct 184 ms 61944 KB Output is correct
45 Correct 79 ms 34552 KB Output is correct
46 Correct 103 ms 34552 KB Output is correct
47 Correct 176 ms 62212 KB Output is correct
48 Correct 183 ms 62328 KB Output is correct
49 Correct 265 ms 39576 KB Output is correct
50 Correct 245 ms 39512 KB Output is correct
51 Correct 375 ms 68728 KB Output is correct
52 Correct 279 ms 68216 KB Output is correct
53 Correct 730 ms 37964 KB Output is correct
54 Correct 242 ms 51404 KB Output is correct
55 Correct 108 ms 35576 KB Output is correct
56 Correct 277 ms 63692 KB Output is correct
57 Correct 394 ms 36344 KB Output is correct
58 Correct 638 ms 36860 KB Output is correct
59 Correct 181 ms 62272 KB Output is correct