답안 #537037

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
537037 2022-03-14T09:57:21 Z timreizin 말 (IOI15_horses) C++17
100 / 100
271 ms 101324 KB
#include "horses.h"
#include <vector>

using namespace std;

using ll = long long;
using lll = __int128;

const ll MOD = 1e9 + 7;

struct SegTree
{

    int n;
    vector<lll> tree, res;
    vector<ll> moded;

    ll binPow(ll a, ll n)
    {
        ll res = 1;
        while (n)
        {
            if (n & 1) res = (res * a) % MOD;
            a = (a * a) % MOD;
            n >>= 1;
        }
        return res;
    }

    void build(int v, int l, int r, vector<ll> &x, vector<ll> &y)
    {
        if (l > r) return;
        if (l == r)
        {
            tree[v] = x[l];
            res[v] = x[l] * y[l];
            moded[v] = x[l];
            return;
        }
        int m = (l + r) >> 1;
        build(v << 1, l, m, x, y);
        build(v << 1 | 1, m + 1, r, x, y);
        tree[v] = min((lll)1e18 + 1, tree[v << 1] * tree[v << 1 | 1]);
        res[v] = max(res[v << 1], res[v << 1 | 1] * tree[v << 1]);
        moded[v] = moded[v << 1] * moded[v << 1 | 1] % MOD;
    }

    SegTree()
    {}

    SegTree(vector<ll> &x, vector<ll> &y) : n((int)x.size())
    {
        tree.resize(4 * n + 1);
        res.resize(4 * n + 1);
        moded.resize(4 * n + 1);
        build(1, 0, n - 1, x, y);
    }

    void updateX(int p, ll val, int v, int l, int r)
    {
        if (l > r) return;
        if (l == r)
        {
            res[v] = res[v] / tree[v] * val;
            moded[v] = moded[v] * binPow(tree[v], MOD - 2) % MOD * val % MOD;
            tree[v] = val;
            return;
        }
        int m = (l + r) >> 1;
        if (p <= m) updateX(p, val,v << 1, l, m);
        else updateX(p, val,v << 1 | 1, m + 1, r);
        tree[v] = min((lll)1e18 + 1, tree[v << 1] * tree[v << 1 | 1]);
        res[v] = max(res[v << 1], res[v << 1 | 1] * tree[v << 1]);
        moded[v] = moded[v << 1] * moded[v << 1 | 1] % MOD;
    }

    void updateY(int p, ll val, int v, int l, int r)
    {
        if (l > r) return;
        if (l == r)
        {
            res[v] = val * tree[v];
            return;
        }
        int m = (l + r) >> 1;
        if (p <= m) updateY(p, val,v << 1, l, m);
        else updateY(p, val,v << 1 | 1, m + 1, r);
        res[v] = max(res[v << 1], res[v << 1 | 1] * tree[v << 1]);
    }

    ll getModed(int a, int b, int v, int l, int r)
    {
        if (a > r || b < l) return 1;
        if (a == l && b == r) return moded[v];
        int m = (l + r) >> 1;
        return getModed(a, min(b, m), v << 1, l, m) * getModed(max(a, m + 1), b, v << 1 | 1, m + 1, r) % MOD;
    }

    int search(int v, int l, int r, lll mul = 1)
    {
        if (mul * tree[v] < 1e9) return -1;
        if (l == r) return l;
        int m = (l + r) >> 1;
        int ret = search(v << 1 | 1, m + 1, r, mul);
        if (ret != -1) return ret;
        return search(v << 1, l, m, mul * tree[v << 1 | 1]);
    }

    pair<lll, lll> get(int a, int b, int v, int l, int r, lll mul = 1)
    {
        if (a > r || b < l) return {0, mul};
        if (a == l && b == r) return {mul * res[v], mul * tree[v]};
        int m = (l + r) >> 1;
        pair<lll, lll> ret1 = get(a, min(b, m), v << 1, l, m, mul);
        pair<lll, lll> ret2 = get(max(a, m + 1), b, v << 1 | 1, m + 1, r, ret1.second);
        return {max(ret1.first, ret2.first), ret2.second};
    }

    int get()
    {
        int ind = max(0, search(1, 0, n - 1));
        ll res = 1;
        if (ind != 0) res = getModed(0, ind - 1, 1, 0, n - 1);
        lll ret = get(ind, n - 1, 1, 0, n - 1).first % MOD;
        return (res * ret) % MOD;
    }

    int update(bool w, int p, ll val)
    {
        if (w) updateY(p, val, 1, 0, n - 1);
        else updateX(p, val, 1, 0, n - 1);
        return get();
    }

};

vector<ll> pref;
SegTree st;

int init(int n, int X[], int Y[])
{
    vector<ll> x(n), y(n);
    for (int i = 0; i < n; ++i)
    {
        x[i] = X[i];
        y[i] = Y[i];
    }
    st = SegTree(x, y);
	return st.get();
}

int updateX(int pos, int val)
{
	return st.update(0, pos, val);
}

int updateY(int pos, int val)
{
	return st.update(1, pos, val);
}

Compilation message

horses.cpp: In member function 'll SegTree::binPow(ll, ll)':
horses.cpp:18:24: warning: declaration of 'n' shadows a member of 'SegTree' [-Wshadow]
   18 |     ll binPow(ll a, ll n)
      |                     ~~~^
horses.cpp:14:9: note: shadowed declaration is here
   14 |     int n;
      |         ^
horses.cpp:20:12: warning: declaration of 'res' shadows a member of 'SegTree' [-Wshadow]
   20 |         ll res = 1;
      |            ^~~
horses.cpp:15:23: note: shadowed declaration is here
   15 |     vector<lll> tree, res;
      |                       ^~~
horses.cpp: In member function 'void SegTree::updateX(int, ll, int, int, int)':
horses.cpp:65:58: warning: conversion from '__gnu_cxx::__alloc_traits<std::allocator<__int128>, __int128>::value_type' {aka '__int128'} to 'll' {aka 'long long int'} may change value [-Wconversion]
   65 |             moded[v] = moded[v] * binPow(tree[v], MOD - 2) % MOD * val % MOD;
      |                                                          ^
horses.cpp: In member function 'int SegTree::search(int, int, int, lll)':
horses.cpp:101:17: warning: conversion from 'lll' {aka '__int128'} to 'double' may change value [-Wconversion]
  101 |         if (mul * tree[v] < 1e9) return -1;
horses.cpp: In member function 'int SegTree::get()':
horses.cpp:122:12: warning: declaration of 'res' shadows a member of 'SegTree' [-Wshadow]
  122 |         ll res = 1;
      |            ^~~
horses.cpp:15:23: note: shadowed declaration is here
   15 |     vector<lll> tree, res;
      |                       ^~~
horses.cpp:125:28: warning: conversion from 'lll' {aka '__int128'} to 'int' may change value [-Wconversion]
  125 |         return (res * ret) % MOD;
      |                ~~~~~~~~~~~~^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 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 468 KB Output is correct
25 Correct 1 ms 432 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 90312 KB Output is correct
2 Correct 269 ms 90328 KB Output is correct
3 Correct 188 ms 92364 KB Output is correct
4 Correct 245 ms 96328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 288 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 488 KB Output is correct
25 Correct 1 ms 432 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 432 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 80 ms 94184 KB Output is correct
34 Correct 82 ms 94196 KB Output is correct
35 Correct 101 ms 101112 KB Output is correct
36 Correct 118 ms 101016 KB Output is correct
37 Correct 73 ms 92400 KB Output is correct
38 Correct 70 ms 93268 KB Output is correct
39 Correct 62 ms 92364 KB Output is correct
40 Correct 97 ms 96108 KB Output is correct
41 Correct 65 ms 92340 KB Output is correct
42 Correct 60 ms 92280 KB Output is correct
43 Correct 73 ms 96680 KB Output is correct
44 Correct 74 ms 96764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 432 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 428 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
31 Correct 1 ms 488 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 153 ms 92372 KB Output is correct
34 Correct 230 ms 101200 KB Output is correct
35 Correct 208 ms 92580 KB Output is correct
36 Correct 235 ms 96448 KB Output is correct
37 Correct 77 ms 94240 KB Output is correct
38 Correct 90 ms 94252 KB Output is correct
39 Correct 105 ms 101184 KB Output is correct
40 Correct 97 ms 101024 KB Output is correct
41 Correct 83 ms 92296 KB Output is correct
42 Correct 76 ms 93252 KB Output is correct
43 Correct 58 ms 92364 KB Output is correct
44 Correct 89 ms 96244 KB Output is correct
45 Correct 61 ms 92260 KB Output is correct
46 Correct 60 ms 92340 KB Output is correct
47 Correct 89 ms 96608 KB Output is correct
48 Correct 87 ms 96636 KB Output is correct
49 Correct 217 ms 94188 KB Output is correct
50 Correct 271 ms 94272 KB Output is correct
51 Correct 198 ms 101324 KB Output is correct
52 Correct 185 ms 101240 KB Output is correct
53 Correct 205 ms 92480 KB Output is correct
54 Correct 178 ms 93360 KB Output is correct
55 Correct 114 ms 92412 KB Output is correct
56 Correct 207 ms 96196 KB Output is correct
57 Correct 83 ms 92492 KB Output is correct
58 Correct 130 ms 92360 KB Output is correct
59 Correct 78 ms 96588 KB Output is correct