Submission #447984

# Submission time Handle Problem Language Result Execution time Memory
447984 2021-07-28T11:24:38 Z parsabahrami Fish (IOI08_fish) C++17
50 / 100
930 ms 40672 KB
/* I do it for the glory */
#include <bits/stdc++.h>
 
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
 
#define SZ(x) (int) x.size()
#define F first
#define S second
#define lc id << 1
#define rc lc | 1

const int N = 5e5 + 10;
int lst[N], seg[N << 2], ord[N], A[N], g[N], k, mod, n;
vector<int> vec[N], tmp;

void upd(int p, int x, int id = 1, int l = 1, int r = n + 1) { 
    if (r - l < 2) { seg[id] = (seg[id] + x + mod) % mod; return; }
    int md = (l + r) >> 1;
    p < md ? upd(p, x, lc, l, md) : upd(p, x, rc, md, r);
    seg[id] = 1ll * seg[lc] * seg[rc] % mod;
}

int get(int ql, int qr, int id = 1, int l = 1, int r = n + 1) {
    if (qr <= l || r <= ql || ql >= qr) return 1;
    if (ql <= l && r <= qr) return seg[id];
    int md = (l + r) >> 1;
    return 1ll * get(ql, qr, lc, l, md) * get(ql, qr, rc, md, r) % mod;
}

int main() {
    scanf("%d%d%d", &n, &k, &mod);
    for (int i = 1; i <= n; i++) {
        scanf("%d%d", &A[i], &g[i]), ord[i] = i;
    }
    sort(ord + 1, ord + n + 1, [&] (int i, int j) { return A[i] < A[j]; });
    for (int _ = n; _; _--) {
        int i = ord[_]; 
        vec[g[i]].push_back(_); tmp.push_back(A[i]);
        if (!lst[g[i]]) lst[g[i]] = _;
    }
    for (int i = 1; i <= k; i++) 
        reverse(begin(vec[i]), end(vec[i]));
    reverse(begin(tmp), end(tmp));
    int ret = 0;
    fill(seg, seg + N * 4, 1);
    for (int j = 1, pt = 0; j <= n; j++) {
        int i = ord[j];
        while (A[ord[pt + 1]] * 2 <= A[i]) pt++, upd(lst[g[ord[pt]]], 1);
        if (j != lst[g[i]]) continue;
        auto it = upper_bound(vec[g[i]].begin(), vec[g[i]].end(), pt);
        assert(it != vec[g[i]].end());
        upd(j, -1);
        ret = (ret + get(1, j + 1)) % mod;
        upd(j, 1);
        int nxt = upper_bound(begin(tmp), end(tmp), 2 * A[ord[*it]]) - begin(tmp) + 1;
        ret = (ret + 1ll * get(j + 1, nxt) * get(1, j) % mod) % mod;
    }
    printf("%d\n", ret);
    return 0;
}

Compilation message

fish.cpp: In function 'int main()':
fish.cpp:34:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |     scanf("%d%d%d", &n, &k, &mod);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fish.cpp:36:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |         scanf("%d%d", &A[i], &g[i]), ord[i] = i;
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19916 KB Output is correct
2 Correct 11 ms 19928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19916 KB Output is correct
2 Incorrect 292 ms 30160 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 11 ms 19916 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 19924 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 114 ms 24412 KB Output is correct
2 Correct 176 ms 28868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19916 KB Output is correct
2 Correct 17 ms 20004 KB Output is correct
3 Incorrect 15 ms 20024 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 177 ms 26500 KB Output is correct
2 Correct 388 ms 37092 KB Output is correct
3 Correct 399 ms 37812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 283 ms 30892 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 162 ms 26484 KB Output is correct
2 Correct 329 ms 30812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 318 ms 29976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 398 ms 31164 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 304 ms 29220 KB Output is correct
2 Correct 493 ms 40672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 767 ms 32948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 647 ms 33684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 930 ms 35120 KB Output isn't correct
2 Halted 0 ms 0 KB -