Submission #697245

#TimeUsernameProblemLanguageResultExecution timeMemory
697245mjhmjh1104Fish (IOI08_fish)C++17
100 / 100
606 ms38360 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...