Submission #697245

# Submission time Handle Problem Language Result Execution time Memory
697245 2023-02-09T00:41:02 Z mjhmjh1104 Fish (IOI08_fish) C++17
100 / 100
606 ms 38360 KB
#include <set>
#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[v[i].second];
            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 (nx[i] == -1) {
            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;
            res = (res + 1LL * query(0, 0, 524287, 0, v[i].second - 1) * cnt[v[i].second] % M) % M;
        }
    }
    printf("%d", res);
}

Compilation message

fish.cpp: In function 'int main()':
fish.cpp:32:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |     scanf("%d%d%d", &n, &k, &M);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
fish.cpp:35:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |         scanf("%d%d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2388 KB Output is correct
2 Correct 4 ms 2416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2388 KB Output is correct
2 Correct 223 ms 16288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 92 ms 8096 KB Output is correct
2 Correct 125 ms 9504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2516 KB Output is correct
2 Correct 7 ms 2644 KB Output is correct
3 Correct 7 ms 2548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 137 ms 11328 KB Output is correct
2 Correct 257 ms 16404 KB Output is correct
3 Correct 260 ms 16456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 245 ms 17156 KB Output is correct
2 Correct 227 ms 17468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 12772 KB Output is correct
2 Correct 239 ms 17664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 240 ms 16704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 260 ms 19160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 200 ms 17244 KB Output is correct
2 Correct 223 ms 24108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 371 ms 24884 KB Output is correct
2 Correct 286 ms 19776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 343 ms 23020 KB Output is correct
2 Correct 335 ms 24076 KB Output is correct
3 Correct 391 ms 27128 KB Output is correct
4 Correct 356 ms 23980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 460 ms 28532 KB Output is correct
2 Correct 606 ms 37440 KB Output is correct
3 Correct 451 ms 38360 KB Output is correct
4 Correct 591 ms 34668 KB Output is correct