Submission #697124

# Submission time Handle Problem Language Result Execution time Memory
697124 2023-02-08T13:06:42 Z mjhmjh1104 Fish (IOI08_fish) C++17
58 / 100
690 ms 22484 KB
#include <cstdio>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;

int M;

int tree[1048576];

int query(int i, int b, int e, int l, int r) {
    if (r < l || r < b || e < l) return 1;
    if (l <= b && e <= r) return tree[i];
    int m = (b + e) / 2;
    return 1LL * query(i * 2 + 1, b, m, l, r) * query(i * 2 + 2, m + 1, e, l, r) % M;
}

int update(int i, int b, int e, int p, int v) {
    if (p < b || e < p) return tree[i];
    if (b == e) return tree[i] = v;
    int m = (b + e) / 2;
    return tree[i] = 1LL * update(i * 2 + 1, b, m, p, v) * update(i * 2 + 2, m + 1, e, p, v) % M;
}

int n, k, cnt[500006], cnt2[500006], t, fi[500006], lt[500006], rt[500006], nx[500006], tmp[500006], pos[500006];
bool visited[500006];
vector<pair<int, int>> v, w;
vector<int> u;

int main() {
    scanf("%d%d%d", &n, &k, &M);
    for (int i = 0; i < n; i++) {
        int x, y;
        scanf("%d%d", &x, &y);
        y--;
        v.push_back({ x, y });
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < k; i++) tmp[i] = fi[i] = -1;
    for (int i = 0; i < n; i++) nx[i] = -1;
    for (int i = 0; i < n; i++) {
        if (tmp[v[i].second] != -1) nx[tmp[v[i].second]] = i;
        tmp[v[i].second] = i;
    }
    for (int i = 0; i < k; i++) w.push_back({ tmp[i], i });
    sort(w.begin(), w.end());
    for (int i = 0; i < k; i++) pos[w[i].second] = i;
    for (auto &[ i, j ]: v) j = pos[j];
    for (int i = 0; i < n; i++) if (fi[v[i].second] == -1) fi[v[i].second] = i;
    u.resize(k);
    for (auto &[ i, j ]: v) u[j] = i;
    for (int i = 0; i < k; i++) tmp[i] = -1;
    t = 0;
    for (int i = 0; i < n; i++) {
        while (t < n && v[t].first * 2 <= v[i].first) {
            tmp[v[t].second] = t;
            cnt[v[t++].second]++;
        }
        rt[i] = t;
        if (nx[i] == -1) {
            int c = tmp[v[i].second];
            if (c == -1) c = fi[i];
            else c = nx[c];
            c = v[c].first * 2;
            lt[i] = lower_bound(u.begin(), u.end(), c) - u.begin() - 1;
        }
    }
    for (int i = 0; i < k; i++) cnt[i] = 0;
    t = 0;
    for (int i = 0; i < n; i++) {
        while (t < n && v[t].first * 2 <= v[i].first) cnt[v[t++].second]++;
        rt[i] = t;
    }
    int res = 0;
    for (int i = 0; i < k; i++) cnt2[i] = cnt[i];
    for (int i = 0; i < k; i++) tree[524287 + i] = (cnt[i] + 1) % M;
    for (int i = 524286; i >= 0; i--) tree[i] = 1LL * tree[i * 2 + 1] * tree[i * 2 + 2] % M;
    for (int i = n - 1; i >= 0; i--) {
        while (t > rt[i]) {
            --t;
            cnt[v[t].second]--;
            update(0, 0, 524287, v[t].second, (cnt[v[t].second] + 1) % M);
        }
        if (!visited[v[i].second]) {
            visited[v[i].second] = true;
            int curr = 1LL * query(0, 0, 524287, 0, v[i].second - 1) * query(0, 0, 524287, v[i].second + 1, lt[i]) % M;
            res = (res + curr) % M;
        }
    }
    for (int i = 0; i < k; i++) cnt[i] = 0;
    t = 0;
    for (int i = 0; i < n; i++) {
        while (t < n && v[t].first * 2 <= v[i].first) cnt[v[t++].second]++;
        rt[i] = t;
    }
    for (int i = 0; i < k; i++) tree[524287 + i] = (cnt[i] + 1) % M, visited[i] = false;
    for (int i = 524286; i >= 0; i--) tree[i] = 1LL * tree[i * 2 + 1] * tree[i * 2 + 2] % M;
    for (int i = n - 1; i >= 0; i--) {
        while (t > rt[i]) {
            --t;
            cnt[v[t].second]--;
            cnt[v[t].second] = max(cnt[v[t].second], 0);
            update(0, 0, 524287, v[t].second, (cnt[v[t].second] + 1) % M);
        }
        if (!visited[v[i].second]) {
            visited[v[i].second] = true;
            int curr = 1LL * query(0, 0, 524287, 0, v[i].second - 1) * query(0, 0, 524287, v[i].second + 1, k - 1) % M;
            curr = 1LL * curr * cnt[v[i].second] % M;
            res = (res + curr) % M;
            update(0, 0, 524287, v[i].second, 1);
            cnt[v[i].second] = 0;
        }
    }
    printf("%d", res);
}

Compilation message

fish.cpp: In function 'int main()':
fish.cpp:31:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |     scanf("%d%d%d", &n, &k, &M);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
fish.cpp:34:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |         scanf("%d%d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 2348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 2388 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2388 KB Output is correct
2 Correct 7 ms 2344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 2352 KB Output is correct
2 Correct 289 ms 12252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 2388 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 2564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 125 ms 6916 KB Output is correct
2 Correct 179 ms 9644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 2516 KB Output is correct
2 Incorrect 12 ms 2644 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 187 ms 8672 KB Output is correct
2 Correct 337 ms 17072 KB Output is correct
3 Correct 291 ms 17196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 348 ms 12876 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 161 ms 10300 KB Output is correct
2 Correct 302 ms 14368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 293 ms 12400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 316 ms 13472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 269 ms 13012 KB Output is correct
2 Correct 285 ms 18280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 507 ms 19792 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 530 ms 19760 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 690 ms 22484 KB Output isn't correct
2 Halted 0 ms 0 KB -