QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#647345 | #7434. 冷たい部屋、一人 | guosoun | WA | 162ms | 5368kb | C++17 | 5.2kb | 2024-10-17 13:29:34 | 2024-10-17 13:29:35 |
Judging History
answer
#include <bits/stdc++.h>
// #include "../cpp-dump/cpp-dump.hpp"
#define cpp_dump(...) void()
using ll = long long;
template <int B>
struct array {
std::vector<int> a, b;
array(int n) : a(n), b((n - 1) / B + 1) {}
void add(int x) { a[x] += 1, b[x / B] += 1; }
int query(int x) {
int ret = 0;
for (int i = x; i / B == x / B && i < (int)a.size(); i++) ret += a[i];
for (int i = x / B + 1; i < (int)b.size(); i++) ret += b[i];
return ret;
}
};
struct list {
ll ans;
std::vector<int> f;
std::vector<std::pair<int*, int>> st1;
std::vector<ll> st2;
list(int n) : ans(0), f(n) { std::iota(f.begin(), f.end(), 0); }
std::tuple<int, int, int, int> link(int i) {
int s = f[i], t = f[i + 1];
st2.emplace_back(ans);
ans += (ll)(i - s + 1) * (t - i);
st1.emplace_back(&f[s], f[s]), st1.emplace_back(&f[t], f[t]);
f[s] = t, f[t] = s;
return {s, i, i + 1, t};
}
void undo() {
*st1.back().first = st1.back().second, st1.pop_back();
*st1.back().first = st1.back().second, st1.pop_back();
ans = st2.back(), st2.pop_back();
}
};
template<class T, class cmp>
struct rmq {
int n;
std::vector<T> a;
std::array<std::vector<T>, 20> bl;
rmq(std::vector<T> v) {
n = v.size(), a = v;
bl.fill(std::vector<T>(n));
for (int i = 0; i < n; i++) bl[0][i] = a[i];
for (int i = 1; i < 20; i++)
for (int j = 0; j + (1 << i) - 1 < n; j++)
bl[i][j] = std::min(bl[i - 1][j], bl[i - 1][j + (1 << (i - 1))], cmp());
}
T query(int l, int r) {
int k = std::__lg(r - l + 1);
return std::min(bl[k][l], bl[k][r - (1 << k) + 1], cmp());
}
};
int main() {
const int B = 0;
std::cin.tie(0)->sync_with_stdio(0);
int n, m;
std::cin >> n >> m;
std::vector<int> a(n + 1), b(n + 1);
std::vector<std::vector<int>> pos(n + 1);
std::vector<std::tuple<int, int, ll*>> q(m);
std::vector<ll> ans;
ans.reserve(m);
for (int i = 1; i <= n; i++) std::cin >> a[i], pos[a[i]].emplace_back(i);
for (int i = 1; i <= n; i++) std::cin >> b[i];
for (auto &[l, r, it] : q) {
std::cin >> l >> r;
ans.push_back(0);
it = &ans.back();
}
rmq<int, std::less<int>> bmn(b);
rmq<int, std::greater<int>> bmx(b);
std::vector<std::pair<int, int>> e;
for (int i = 1; i < n; i++) e.emplace_back(std::max(b[i], b[i + 1]), i);
std::sort(e.begin(), e.end());
std::sort(q.begin(), q.end(), [](auto x, auto y) {
return std::get<1>(x) < std::get<1>(y);
});
auto it = q.begin();
list l(n + 1);
array<400> cnt(n + 1);
for (auto [v, i] : e) {
while (it != q.end() && std::get<1>(*it) < v) {
*std::get<2>(*it) += cnt.query(std::get<0>(*it)), it++;
}
auto [l1, r1, l2, r2] = l.link(i);
if (r1 - l1 > r2 - l2) std::swap(l1, l2), std::swap(r1, r2);
for (int i = l1; i <= r1; i++) {
if (pos[a[i]].size() > B) continue;
for (int j : pos[a[i]])
if (l2 <= j && j <= r2) cnt.add(bmn.query(std::min(i, j), std::max(i, j)));
}
}
while (it != q.end()) {
*std::get<2>(*it) += cnt.query(std::get<0>(*it)), it++;
}
for (int i = 1; i <= n; i++) {
if (pos[i].size() <= B) continue;
// if (i != 1) continue;
std::vector<int> val{1, n};
std::vector<std::tuple<int, int, int>> e;
for (int j = 0; j + 1 < (int)pos[i].size(); j++) {
int l = bmn.query(pos[i][j], pos[i][j + 1]), r = bmx.query(pos[i][j], pos[i][j + 1]);
val.push_back(l), val.push_back(r), e.emplace_back(j, l, r);
}
std::sort(val.begin(), val.end());
val.erase(std::unique(val.begin(), val.end()), val.end());
std::vector<int> fi(n + 1, -1), la(n + 1, -1);
for (int i = 0; i < (int)val.size(); i++) fi[val[i]] = la[val[i]] = i;
for (int i = n; i >= 1; i--) if (fi[i] == -1) fi[i] = fi[i + 1];
for (int i = 1; i <= n; i++) if (la[i] == -1) la[i] = la[i - 1];
int B = 400;
std::vector<std::vector<std::tuple<int, int, ll*>>> qb((val.size() - 1) / B + 1);
cpp_dump(val);
for (auto [l, r, it] : q) {
l = fi[l], r = la[r];
qb[l / B].emplace_back(l, r, it);
}
std::vector<std::vector<std::pair<int, int>>> eb(val.size());
for (auto [i, l, r] : e) {
l = fi[l], r = la[r];
eb[l].emplace_back(r, i);
eb[r].emplace_back(l, i);
}
list li(val.size());
for (int i = 0; i < (int)qb.size(); i++) {
auto &cq = qb[i];
std::sort(cq.begin(), cq.end(), [](auto x, auto y) {
return std::get<1>(x) < std::get<1>(y);
});
int l = (i + 1) * B, r = l - 1;
auto add = [&](int l, int r, int i) {
int ret = 0;
for (auto [j, v] : eb[i])
if (l <= j && j <= r) li.link(v), ret++;
return ret;
};
for (auto [ql, qr, it] : cq) {
if (qr < (i + 1) * B) {
int l = ql, r = l - 1, c = 0;
while (r < qr) ++r, c += add(l, r, r);
*it += li.ans;
while (c--) li.undo();
continue;
}
int c = 0;
while (r < qr) ++r, add(l, r, r);
while (l > ql) --l, c += add(l, r, l);
*it += li.ans;
while (c--) li.undo();
l = (i + 1) * B;
}
}
}
for (int i = 0; i < m; i++) std::cout << ans[i] << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3872kb
input:
100 100 4 1 5 1 2 1 7 5 3 7 2 3 6 6 5 3 2 2 4 1 6 5 6 2 2 2 7 6 1 3 6 3 5 6 7 6 1 2 3 3 4 2 1 1 5 4 4 3 6 7 1 1 6 1 5 6 2 3 7 4 2 4 6 7 7 3 5 3 7 2 3 3 5 1 4 7 1 2 2 5 2 2 4 3 4 7 2 7 7 3 7 3 6 6 5 4 5 4 7 6 93 52 12 70 25 36 18 37 27 99 68 40 84 3 76 57 60 19 33 41 92 87 58 13 15 43 28 63 64 59 31 ...
output:
1 0 13 71 1 1 3 7 0 0 2 1 3 20 12 6 61 24 1 0 0 2 3 19 0 0 6 2 0 0 4 1 135 0 19 1 1 29 14 39 1 0 1 7 7 0 12 3 0 1 0 1 1 5 0 28 14 19 2 1 0 0 6 5 0 0 2 7 5 1 2 2 0 1 11 1 0 1 0 10 0 0 5 1 33 1 17 2 1 22 20 1 2 0 0 16 0 1 1 15
result:
ok 100 numbers
Test #2:
score: 0
Accepted
time: 1ms
memory: 3836kb
input:
100 100 8 8 2 8 8 12 11 8 5 5 5 1 7 6 6 11 3 13 1 12 2 2 4 1 11 13 10 7 1 2 4 3 1 12 1 5 13 8 1 5 12 5 12 4 6 3 5 4 8 3 4 1 4 3 9 2 11 9 4 8 12 3 5 13 13 1 9 12 2 13 8 13 4 13 12 5 12 13 2 6 4 4 1 6 6 9 12 7 4 3 10 7 1 7 7 10 4 12 3 9 96 87 12 73 74 78 99 76 7 77 54 88 90 86 95 94 31 83 27 11 66 91 ...
output:
0 6 2 0 0 1 16 1 1 10 7 9 2 8 2 1 3 5 0 7 2 0 2 0 1 0 7 0 0 1 108 0 0 0 6 4 1 0 2 13 0 3 0 10 0 0 1 21 0 18 0 11 0 8 13 9 0 0 0 11 12 2 1 0 2 16 1 7 0 16 0 2 3 7 0 2 10 1 4 2 6 0 0 6 3 0 0 0 5 7 0 0 6 0 7 0 4 0 3 0
result:
ok 100 numbers
Test #3:
score: 0
Accepted
time: 1ms
memory: 3672kb
input:
100 100 13 4 2 1 1 2 1 39 20 1 1 1 33 1 1 1 9 37 21 7 14 58 1 3 19 29 40 56 2 1 3 1 42 1 1 13 1 3 5 4 49 1 1 8 8 3 1 14 1 1 2 6 17 3 2 2 2 9 3 1 17 90 1 1 1 1 8 14 17 16 1 33 15 20 46 22 2 20 11 1 28 3 1 4 1 17 3 9 10 2 1 2 2 6 1 1 3 17 27 2 86 82 24 49 50 32 63 20 53 11 60 1 16 27 15 14 12 88 52 17...
output:
1 84 13 0 7 4 26 1 9 1 7 3 10 0 7 3 6 4 4 0 13 1 0 4 4 0 40 22 36 0 5 6 0 37 28 213 1 6 10 4 0 3 5 0 3 5 25 2 0 1 3 12 1 39 0 8 4 2 6 0 6 4 23 17 0 0 4 13 6 6 0 0 6 10 11 30 0 7 10 10 101 0 1 0 62 0 10 100 26 4 5 1 24 0 1 4 15 3 0 6
result:
ok 100 numbers
Test #4:
score: 0
Accepted
time: 1ms
memory: 3716kb
input:
100 100 1 1 1 1 1 36 58 18 15 1 1 2 1 1 1 1 4 10 9 2 2 4 28 1 2 1 2 2 28 27 12 6 3 10 1 1 2 1 1 2 7 40 1 4 8 17 16 3 5 1 2 2 16 5 6 1 5 3 4 11 1 1 11 51 11 4 3 7 14 16 1 11 1 2 8 3 4 1 1 3 2 5 21 7 3 10 2 2 1 20 23 1 3 6 1 1 15 3 34 1 8 1 2 3 92 48 11 43 9 71 69 5 6 7 12 26 31 45 80 83 14 16 17 25 2...
output:
29 1 9 19 6 1 3 2 0 16 15 2 11 14 24 191 25 21 33 34 19 2 0 0 11 32 1 9 2 16 24 28 9 0 123 4 6 0 13 69 271 15 47 6 0 121 0 1 15 1 9 61 0 63 10 2 2 13 55 25 12 2 40 4 3 39 14 15 0 15 2 48 6 0 0 6 0 5 0 11 3 1 78 9 12 0 0 14 15 38 7 3 0 12 10 0 1 6 0 22
result:
ok 100 numbers
Test #5:
score: 0
Accepted
time: 1ms
memory: 3616kb
input:
100 100 1 15 6 12 6 13 8 8 3 5 14 5 7 13 8 1 1 3 8 5 4 7 9 1 1 7 4 14 7 5 4 6 7 5 11 6 4 14 8 11 12 9 12 14 10 12 9 14 14 11 12 12 11 8 5 11 10 6 13 5 6 4 6 15 10 2 13 12 7 15 9 14 15 1 9 9 8 4 5 2 2 5 13 14 12 5 9 13 7 12 1 1 1 7 10 5 11 4 4 6 17 65 39 50 77 97 44 15 72 11 8 59 74 14 1 3 5 68 21 4 ...
output:
24 5 0 4 0 1 0 0 2 0 2 11 0 13 12 3 19 0 0 0 5 1 0 0 14 0 9 0 1 22 7 25 44 0 7 0 0 3 27 1 0 0 8 3 0 0 2 2 18 26 63 15 2 28 0 1 8 10 18 19 4 0 0 5 0 0 3 15 5 63 5 8 29 0 14 8 7 2 3 1 1 1 2 1 6 8 0 0 19 0 20 0 3 11 0 4 6 5 55 0
result:
ok 100 numbers
Test #6:
score: 0
Accepted
time: 79ms
memory: 5368kb
input:
5000 5000 2 29 12 10 25 32 45 18 16 38 41 50 8 32 54 37 18 29 37 42 9 38 25 43 35 5 47 20 32 35 8 14 8 37 30 16 45 19 14 33 14 31 31 33 28 12 11 49 32 6 11 4 50 39 3 55 25 26 13 40 41 44 31 31 18 19 25 18 29 19 21 1 52 19 2 53 39 55 11 27 54 22 16 49 12 23 41 22 34 38 40 20 5 35 43 40 29 14 20 40 6 ...
output:
21908 618 65611 229 7839 55433 12508 4205 5 12239 31375 1958 20 2290 29810 13020 12234 5752 14318 13451 122 0 109 3852 26843 12709 11522 14307 45468 886 5020 6667 21808 3367 637 593 37 5078 2698 61 364 20500 88231 2311 39 2487 18627 3425 194 4845 54212 84095 14695 8 67509 4364 312 23989 3876 86722 1...
result:
ok 5000 numbers
Test #7:
score: 0
Accepted
time: 31ms
memory: 5092kb
input:
5000 5000 15 86 121 63 26 26 74 43 35 32 48 131 61 120 103 48 101 1 70 97 130 105 30 38 68 141 85 141 108 17 143 106 47 90 4 68 63 8 79 26 10 66 48 61 18 75 62 84 80 113 20 27 109 102 32 13 20 63 12 53 112 146 33 27 49 110 55 132 121 67 75 18 38 80 140 62 7 20 104 4 42 91 67 20 127 77 13 141 64 3 28...
output:
0 132 8 26 0 32 1 61 40 0 0 0 0 18 49 15 0 14 2 63 24 9 10 94 10 21 1 2 1 6 17 21 1 83 19 9 0 21 67 8 13 0 0 0 13 1 0 21 0 13 1 123 0 0 8 0 0 10 14 2 32 2 8 53 29 0 23 6 6 0 0 13 0 1 0 3 0 0 1 0 5 15 15 0 4 4 1 0 1 0 0 2 1 0 0 0 0 1 2 1 11 23 45 1 0 9 14 104 60 8 3 1 2 38 34 32 1 0 0 38 27 18 62 0 5...
result:
ok 5000 numbers
Test #8:
score: -100
Wrong Answer
time: 162ms
memory: 5208kb
input:
5000 5000 342 3 498 1667 87 63 8 215 1 25 67 5 7 600 1 1 51 14 6 1990 7 2707 105 637 5 805 328 74 1 57 316 236 6 1 1 700 965 2 493 21 12 2 1 1626 1 1352 11 109 1 2253 70 4 788 55 140 1 2 1 57 21 1 530 3060 62 266 86 32 1861 1 173 2 2 234 1 402 1392 101 9 7 1 8 3 8 137 46 4 17 212 1 4 10 6 1389 200 4...
output:
31295 10437 9719 9867 9790 1000 8756 6385 11371 10466 9065 11637 1756 169788 9926 1314 387 11401 9125 30655 418 111933 122174 553 30337 9915 11514 125165 8915 28309 10832 6724 11110 9774 11473 11337 11139 11617 56797 1 2093 9424 103019 9958 257 28645 121966 128335 119276 5970 9964 122331 10607 10301...
result:
wrong answer 2nd numbers differ - expected: '316', found: '10437'