Submission #239784

# Submission time Handle Problem Language Result Execution time Memory
239784 2020-06-17T09:44:10 Z abacaba Horses (IOI15_horses) C++14
100 / 100
1018 ms 64876 KB
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
using namespace std;

#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define emb emplace_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define pii pair<int, int>
#define ld long double
#define p__int128 pair<long long, long long>

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <typename T> inline T range(T l, T r) {
    return uniform_int_distribution<T>(l, r)(rng);
}

inline void setin(string s) {
    freopen(s.c_str(), "r", stdin);
}

inline void setout(string s) {
    freopen(s.c_str(), "w", stdout);
}

template <typename T> void Min(T &a, T b) {
    a = min(a, b);
}

template <typename T> void Max(T &a, T b) {
    a = max(a, b);
}

const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 1e6 + 15;

int n, x[N], y[N];
set <int> non_zero_x;

struct segment_tree {
    int t[N << 2];
    segment_tree() {
        memset(t, 0, sizeof(t));
    }
    void update(int v, int tl, int tr, int pos, int val) {
        if(tl == tr)
            t[v] = val;
        else {
            int mid = tl + tr >> 1;
            if(pos <= mid)
                update(v << 1, tl, mid, pos, val);
            else
                update(v << 1 | 1, mid + 1, tr, pos, val);
            t[v] = max(t[v << 1], t[v << 1 | 1]);
        }
    }
    int get(int v, int tl, int tr, int l, int r) {
        if(tl > r || tr < l || l > r)
            return 0;
        if(tl >= l && tr <= r)
            return t[v];
        int mid = tl + tr >> 1;
        return max(get(v << 1, tl, mid, l, r), get(v << 1 | 1, mid + 1, tr, l, r));
    }
} t;

inline int mt(__int128 a, __int128 b) {
    a %= mod, b %= mod;
    return (__int128)a * b % mod;
}

inline int binpow(__int128 n, __int128 k) {
    int res = 1;
    while(k) {
        if(k & 1)
            res = mt(res, n);
        k >>= 1;
        n = mt(n, n);
    }
    return res;
}

inline int dv(__int128 a, __int128 b) {
    return mt(a, binpow(b, mod - 2));
}

struct fenwick {
    int t[N];
    fenwick() {
        fill(t, t + N, 1);
    }
    inline void inc(int i, int val) {
        for(; i <= n; i = (i | (i + 1)))
            t[i] = mt(t[i], val);
    }
    inline int get(int i) {
        int res = 1;
        for(; i > 0; i = (i & (i + 1)) - 1)
            res = mt(res, t[i]);
        return res;
    }
} fenw;

inline int calc_ans() {
    // for(int j : non_zero_x)
    //     assert(x[j] != 1);

    vector <int> now = {};

    __int128 prod = 1;
    for(set <int>::reverse_iterator it = non_zero_x.rbegin(); it != non_zero_x.rend(); ++it) {
        prod *= x[*it];
        now.pb(*it);
        if(prod > (__int128)1000000000000000)
            break;
    }

    reverse(now.begin(), now.end());

    if(now.empty())
        return t.get(1, 1, n, 1, n);
    prod = 1;
    __int128 ans = t.get(1, 1, n, 1, n);
    for(int j = 0; j < now.size(); ++j) {
        prod *= x[now[j]];
        Max(ans, t.get(1, 1, n, now[j], n) * prod);
    }
    return mt(ans, fenw.get(now[0] - 1));
}

int init(int N, int X[], int Y[]) {
    n = N;
    for(int i = 0; i < n; ++i) {
        x[i + 1] = X[i], y[i + 1] = Y[i];
        if(x[i + 1] != 1)
            non_zero_x.insert(i + 1);

        t.update(1, 1, n, i + 1, y[i + 1]);
        fenw.inc(i + 1, x[i + 1]);
    }
    return calc_ans();
}

int updateX(int pos, int val) {
    ++pos;
    if(non_zero_x.find(pos) != non_zero_x.end())
        non_zero_x.erase(pos);
 
    fenw.inc(pos, binpow(x[pos], mod - 2));
 
    x[pos] = val;
 
    if(val != 1)
        non_zero_x.insert(pos);
    fenw.inc(pos, x[pos]);
 
    return calc_ans();
}

int updateY(int pos, int val) {
    y[++pos] = val;
    t.update(1, 1, n, pos, y[pos]);
    return calc_ans();
}

Compilation message

horses.cpp: In member function 'void segment_tree::update(int, int, int, int, int)':
horses.cpp:77:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
             int mid = tl + tr >> 1;
                       ~~~^~~~
horses.cpp: In member function 'int segment_tree::get(int, int, int, int, int)':
horses.cpp:90:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
         int mid = tl + tr >> 1;
                   ~~~^~~~
horses.cpp: In function 'int mt(__int128, __int128)':
horses.cpp:97:28: warning: conversion to 'int' from '__int128' may alter its value [-Wconversion]
     return (__int128)a * b % mod;
            ~~~~~~~~~~~~~~~~^~~~~
horses.cpp: In function 'int binpow(__int128, __int128)':
horses.cpp:100:41: warning: declaration of 'n' shadows a global declaration [-Wshadow]
 inline int binpow(__int128 n, __int128 k) {
                                         ^
horses.cpp:65:5: note: shadowed declaration is here
 int n, x[N], y[N];
     ^
horses.cpp: In function 'int calc_ans()':
horses.cpp:152:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j = 0; j < now.size(); ++j) {
                    ~~^~~~~~~~~~~~
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:159:33: warning: declaration of 'N' shadows a global declaration [-Wshadow]
 int init(int N, int X[], int Y[]) {
                                 ^
horses.cpp:63:11: note: shadowed declaration is here
 const int N = 1e6 + 15;
           ^
# Verdict Execution time Memory Grader output
1 Correct 14 ms 19968 KB Output is correct
2 Correct 14 ms 19968 KB Output is correct
3 Correct 18 ms 19968 KB Output is correct
4 Correct 14 ms 19968 KB Output is correct
5 Correct 14 ms 19968 KB Output is correct
6 Correct 14 ms 19840 KB Output is correct
7 Correct 15 ms 19840 KB Output is correct
8 Correct 15 ms 19968 KB Output is correct
9 Correct 17 ms 19968 KB Output is correct
10 Correct 16 ms 19840 KB Output is correct
11 Correct 16 ms 19968 KB Output is correct
12 Correct 16 ms 19840 KB Output is correct
13 Correct 15 ms 19840 KB Output is correct
14 Correct 15 ms 19968 KB Output is correct
15 Correct 17 ms 19968 KB Output is correct
16 Correct 17 ms 19968 KB Output is correct
17 Correct 17 ms 19968 KB Output is correct
18 Correct 14 ms 19968 KB Output is correct
19 Correct 14 ms 19968 KB Output is correct
20 Correct 15 ms 19968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 19968 KB Output is correct
2 Correct 14 ms 19840 KB Output is correct
3 Correct 14 ms 19840 KB Output is correct
4 Correct 14 ms 19968 KB Output is correct
5 Correct 15 ms 19968 KB Output is correct
6 Correct 15 ms 19968 KB Output is correct
7 Correct 15 ms 19968 KB Output is correct
8 Correct 14 ms 19968 KB Output is correct
9 Correct 14 ms 19840 KB Output is correct
10 Correct 15 ms 19968 KB Output is correct
11 Correct 15 ms 19968 KB Output is correct
12 Correct 17 ms 19968 KB Output is correct
13 Correct 16 ms 19968 KB Output is correct
14 Correct 18 ms 19968 KB Output is correct
15 Correct 16 ms 19840 KB Output is correct
16 Correct 16 ms 19968 KB Output is correct
17 Correct 15 ms 19968 KB Output is correct
18 Correct 16 ms 19968 KB Output is correct
19 Correct 15 ms 19968 KB Output is correct
20 Correct 14 ms 19968 KB Output is correct
21 Correct 15 ms 19840 KB Output is correct
22 Correct 16 ms 19968 KB Output is correct
23 Correct 18 ms 19968 KB Output is correct
24 Correct 16 ms 19968 KB Output is correct
25 Correct 16 ms 19968 KB Output is correct
26 Correct 17 ms 20096 KB Output is correct
27 Correct 19 ms 19968 KB Output is correct
28 Correct 18 ms 20096 KB Output is correct
29 Correct 16 ms 19968 KB Output is correct
30 Correct 18 ms 20096 KB Output is correct
31 Correct 16 ms 19968 KB Output is correct
32 Correct 20 ms 19968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 996 ms 53408 KB Output is correct
2 Correct 516 ms 53372 KB Output is correct
3 Correct 527 ms 52856 KB Output is correct
4 Correct 941 ms 53544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 19968 KB Output is correct
2 Correct 14 ms 19840 KB Output is correct
3 Correct 14 ms 19840 KB Output is correct
4 Correct 15 ms 19968 KB Output is correct
5 Correct 15 ms 19840 KB Output is correct
6 Correct 14 ms 19968 KB Output is correct
7 Correct 17 ms 19968 KB Output is correct
8 Correct 15 ms 19968 KB Output is correct
9 Correct 15 ms 19968 KB Output is correct
10 Correct 14 ms 19840 KB Output is correct
11 Correct 14 ms 19840 KB Output is correct
12 Correct 15 ms 19968 KB Output is correct
13 Correct 14 ms 19840 KB Output is correct
14 Correct 17 ms 19840 KB Output is correct
15 Correct 14 ms 19968 KB Output is correct
16 Correct 14 ms 19840 KB Output is correct
17 Correct 17 ms 19968 KB Output is correct
18 Correct 15 ms 19840 KB Output is correct
19 Correct 16 ms 19840 KB Output is correct
20 Correct 14 ms 19840 KB Output is correct
21 Correct 14 ms 19968 KB Output is correct
22 Correct 15 ms 19840 KB Output is correct
23 Correct 16 ms 20020 KB Output is correct
24 Correct 16 ms 19968 KB Output is correct
25 Correct 17 ms 19968 KB Output is correct
26 Correct 16 ms 20096 KB Output is correct
27 Correct 19 ms 19968 KB Output is correct
28 Correct 17 ms 19968 KB Output is correct
29 Correct 16 ms 19968 KB Output is correct
30 Correct 17 ms 19968 KB Output is correct
31 Correct 16 ms 19968 KB Output is correct
32 Correct 18 ms 19968 KB Output is correct
33 Correct 151 ms 31864 KB Output is correct
34 Correct 149 ms 31864 KB Output is correct
35 Correct 324 ms 62328 KB Output is correct
36 Correct 305 ms 62200 KB Output is correct
37 Correct 216 ms 30072 KB Output is correct
38 Correct 228 ms 42872 KB Output is correct
39 Correct 144 ms 29816 KB Output is correct
40 Correct 294 ms 57336 KB Output is correct
41 Correct 159 ms 29944 KB Output is correct
42 Correct 172 ms 29944 KB Output is correct
43 Correct 276 ms 57720 KB Output is correct
44 Correct 274 ms 57592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 19840 KB Output is correct
2 Correct 15 ms 19968 KB Output is correct
3 Correct 14 ms 19968 KB Output is correct
4 Correct 15 ms 19968 KB Output is correct
5 Correct 14 ms 19968 KB Output is correct
6 Correct 14 ms 19968 KB Output is correct
7 Correct 15 ms 19968 KB Output is correct
8 Correct 14 ms 19840 KB Output is correct
9 Correct 14 ms 19968 KB Output is correct
10 Correct 14 ms 19968 KB Output is correct
11 Correct 14 ms 19968 KB Output is correct
12 Correct 16 ms 19968 KB Output is correct
13 Correct 14 ms 19840 KB Output is correct
14 Correct 14 ms 19968 KB Output is correct
15 Correct 15 ms 19968 KB Output is correct
16 Correct 14 ms 19968 KB Output is correct
17 Correct 14 ms 19968 KB Output is correct
18 Correct 15 ms 19968 KB Output is correct
19 Correct 16 ms 19840 KB Output is correct
20 Correct 14 ms 19968 KB Output is correct
21 Correct 16 ms 19968 KB Output is correct
22 Correct 15 ms 19968 KB Output is correct
23 Correct 15 ms 19968 KB Output is correct
24 Correct 16 ms 19968 KB Output is correct
25 Correct 16 ms 19968 KB Output is correct
26 Correct 16 ms 19968 KB Output is correct
27 Correct 19 ms 19968 KB Output is correct
28 Correct 17 ms 19968 KB Output is correct
29 Correct 16 ms 19968 KB Output is correct
30 Correct 16 ms 19968 KB Output is correct
31 Correct 17 ms 19968 KB Output is correct
32 Correct 18 ms 19968 KB Output is correct
33 Correct 987 ms 56288 KB Output is correct
34 Correct 528 ms 64876 KB Output is correct
35 Correct 542 ms 56056 KB Output is correct
36 Correct 936 ms 59896 KB Output is correct
37 Correct 149 ms 31864 KB Output is correct
38 Correct 145 ms 31864 KB Output is correct
39 Correct 318 ms 62328 KB Output is correct
40 Correct 306 ms 62204 KB Output is correct
41 Correct 201 ms 30072 KB Output is correct
42 Correct 224 ms 42872 KB Output is correct
43 Correct 138 ms 29816 KB Output is correct
44 Correct 290 ms 57208 KB Output is correct
45 Correct 149 ms 29944 KB Output is correct
46 Correct 175 ms 29944 KB Output is correct
47 Correct 275 ms 57596 KB Output is correct
48 Correct 275 ms 57592 KB Output is correct
49 Correct 322 ms 34936 KB Output is correct
50 Correct 287 ms 34936 KB Output is correct
51 Correct 512 ms 64124 KB Output is correct
52 Correct 430 ms 63768 KB Output is correct
53 Correct 1018 ms 33272 KB Output is correct
54 Correct 592 ms 46840 KB Output is correct
55 Correct 282 ms 30968 KB Output is correct
56 Correct 446 ms 59128 KB Output is correct
57 Correct 455 ms 31736 KB Output is correct
58 Correct 620 ms 31992 KB Output is correct
59 Correct 281 ms 57592 KB Output is correct