QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#612074 | #6509. Not Another Range Query Problem | nhuang685 | WA | 16ms | 5132kb | C++23 | 3.0kb | 2024-10-05 04:25:32 | 2024-10-05 04:25:32 |
Judging History
answer
/**
* @author n685
* @brief
* @date 2024-10-04 14:21:21
*
*
*/
#include "bits/stdc++.h"
#ifdef LOCAL
#include "dd/debug.h"
#else
#define dbg(...) 42
#define dbg_proj(...) 420
#define dbg_rproj(...) 420420
void nline() {}
void bar() {}
void start_clock() {}
void end_clock() {}
#endif
namespace rs = std::ranges;
namespace rv = std::views;
struct Fenwick {
using T = int64_t;
using U = int;
int sz;
std::vector<T> val;
static T comb(T a, T b) { return a + b; }
static T inv(T a) { return -a; }
Fenwick() : sz(0) {}
explicit Fenwick(int _sz) : sz(_sz), val(sz + 1) {}
void upd(int i, T a) {
i++;
for (; i <= sz; i += (i & -i)) {
val[i] = comb(val[i], a);
}
}
void upd(int i, int j, U a) {
upd(i, a);
if (j < sz) {
upd(j + 1, inv(a));
}
}
U sum(int i) const {
i++;
T ans{0};
for (; i > 0; i -= (i & -i)) {
ans = comb(ans, val[i]);
}
return static_cast<U>(ans);
}
#ifdef LOCAL
auto debug() {
return rv::iota(0, sz) | rv::transform([&](int ind) { return sum(ind); });
}
#else
void debug() {}
#endif
};
struct LL {
int l{-1}, r{-1};
};
int main() {
#ifndef LOCAL
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
#endif
int n, q;
std::cin >> n >> q;
std::vector<int> s(n);
for (int i = 0; i < n; ++i) {
char c;
std::cin >> c;
s[i] = c - '0';
}
std::vector<std::vector<std::tuple<int, int, int>>> queries(n + 1);
for (int i = 0; i < q; ++i) {
int l, r, k;
std::cin >> l >> r >> k;
--l;
--r;
queries[k].emplace_back(l, r, i);
}
std::vector<LL> ll(n);
for (int i = 0; i < n; ++i) {
if (i > 0) {
ll[i].l = i - 1;
}
if (i < n - 1) {
ll[i].r = i + 1;
}
}
Fenwick li(n), ri(n);
for (int i = 0; i < n; ++i) {
li.upd(i, i, i);
ri.upd(i, i, i);
}
std::vector<int> del, ndel;
for (int i = 0; i < n; ++i) {
if (i == 0 || s[i] != s[i - 1]) {
del.push_back(i);
}
}
int fi = 0;
std::vector<int> ans(q);
for (int i = 0; i <= n; ++i) {
dbg(li.debug());
dbg(ri.debug());
for (auto [l, r, ind] : queries[i]) {
ans[ind] = std::max(0, ri.sum(r) - li.sum(l) + 1);
}
if (i == n) {
break;
}
int pre = -1;
for (int j = 0; j < std::ssize(del); ++j) {
int nxt = ll[del[j]].r;
if (j == std::ssize(del) - 1 || (del[j + 1] != nxt && s[nxt] != pre)) {
ndel.push_back(nxt);
}
}
li.upd(0, n, 1);
for (int j = 0; j < std::ssize(del); ++j) {
if (fi == del[j]) {
fi = ll[del[j]].r;
}
if (ll[del[j]].l != -1) {
ll[ll[del[j]].l].r = ll[del[j]].r;
}
if (ll[del[j]].r != -1) {
ll[ll[del[j]].r].l = ll[del[j]].l;
}
li.upd(del[j], n, -1);
ri.upd(del[j], n, -1);
}
}
for (int i : ans) {
std::cout << i << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3488kb
input:
9 7 100110001 2 5 1 3 6 1 4 8 2 2 7 1 1 9 1 1 9 0 1 9 8
output:
2 1 1 3 4 9 0
result:
ok 7 numbers
Test #2:
score: -100
Wrong Answer
time: 16ms
memory: 5132kb
input:
100 100000 0000011010101000111011110110000111110101101010111111101011011010111001111010111000001000011000001010 76 99 3 25 84 7 45 83 11 10 12 10 69 86 4 27 28 1 22 42 42 4 86 25 26 91 22 20 81 17 50 78 0 77 93 50 31 50 34 7 46 13 78 89 0 79 98 0 2 84 33 58 93 11 56 75 2 55 77 68 7 9 41 44 46 11 47 ...
output:
0 0 0 0 0 0 0 0 0 0 29 0 0 0 12 20 0 0 0 0 0 0 0 0 0 0 0 0 18 0 0 57 0 0 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 8 0 0 16 0 19 29 40 21 12 26 0 21 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 51 0 0 0 0 11 0 0 0 9 0 0 16 22 0 0 0 0 0 0 0 0 0 0 0 46 59 0 0 43 10 0 0 0 0 15 0...
result:
wrong answer 1st numbers differ - expected: '8', found: '0'