#include <bits/stdc++.h>
template <class T>
using Vec = std::vector<T>;
struct Fenwick {
Vec<int> data;
Fenwick() = default;
Fenwick(const int n): data(n + 1) { }
void add(int i, const int x) {
for (i += 1; i < (int) data.size(); i += i & -i) {
data[i] += x;
}
}
int get(int i) const {
int ret = 0;
for (i += 1; i > 0; i -= i & -i) {
ret += data[i];
}
return ret;
}
};
struct Add2D {
Vec<Vec<int>> pts;
Vec<Fenwick> fen;
Add2D(const Vec<int> &x, const Vec<int> &y, const int n) {
pts.resize(2 * n);
fen.resize(2 * n);
for (int i = 0; i < (int) x.size(); ++i) {
pts[x[i] + n].push_back(y[i]);
}
for (int i = n; i < 2 * n; ++i) {
std::sort(pts[i].begin(), pts[i].end());
}
for (int i = n - 1; i > 0; --i) {
pts[i].reserve(pts[i * 2].size() + pts[i * 2 + 1].size());
std::copy(pts[i * 2].begin(), pts[i * 2].end(), std::back_inserter(pts[i]));
std::copy(pts[i * 2 + 1].begin(), pts[i * 2 + 1].end(), std::back_inserter(pts[i]));
std::inplace_merge(pts[i].begin(), pts[i].begin() + pts[i * 2].size(), pts[i].end());
}
for (int i = 1; i < 2 * n; ++i) {
pts[i].erase(std::unique(pts[i].begin(), pts[i].end()), pts[i].end());
fen[i] = Fenwick(pts[i].size());
}
}
void add(int l, int r, const int d, const int u, const int v) {
for (l += size(), r += size(); l < r; l >>= 1, r >>= 1) {
if (l & 1) {
fen[l].add(std::lower_bound(pts[l].begin(), pts[l].end(), d) - pts[l].begin(), v);
fen[l].add(std::lower_bound(pts[l].begin(), pts[l].end(), u) - pts[l].begin(), -v);
l += 1;
}
if (r & 1) {
r -= 1;
fen[r].add(std::lower_bound(pts[r].begin(), pts[r].end(), d) - pts[r].begin(), v);
fen[r].add(std::lower_bound(pts[r].begin(), pts[r].end(), u) - pts[r].begin(), -v);
}
}
}
int get(int x, const int y) const {
int ret = 0;
for (x += size(); x > 0; x >>= 1) {
ret += fen[x].get(std::lower_bound(pts[x].begin(), pts[x].end(), y) - pts[x].begin());
}
return ret;
}
int size() const {
return (int) pts.size() / 2;
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, Q;
std::cin >> N >> Q;
Vec<int> state(N);
for (auto &x: state) {
char c;
std::cin >> c;
x = (c == '1');
}
std::set<int> off;
for (int i = 0; i < N; ++i) {
if (!state[i]) {
off.insert(i);
}
}
Vec<int> ans, X, Y;
Vec<std::tuple<int, int, int, int, int>> add;
Vec<std::pair<int, int>> qs;
qs.reserve(Q);
for (int t = 1; t <= Q; ++t) {
std::string type;
std::cin >> type;
if (type == "query") {
const auto idx = (int) ans.size();
int a, b;
std::cin >> a >> b;
a -= 1;
b -= 1;
const auto itr = off.lower_bound(a);
ans.push_back((itr == off.end() || *itr >= b) ? t : 0);
X.push_back(a);
Y.push_back(b);
qs.emplace_back(idx, true);
}
else {
const auto idx = (int) add.size();
int s;
std::cin >> s;
s -= 1;
if (off.find(s) == off.end()) {
off.insert(s);
const auto itr = off.find(s);
const auto l = (itr == off.begin() ? 0 : *std::prev(itr) + 1);
const auto r = (std::next(itr) == off.end() ? N : *std::next(itr));
add.emplace_back(l, s, s + 1, r, t);
}
else {
const auto itr = off.find(s);
const auto l = (itr == off.begin() ? 0 : *std::prev(itr) + 1);
const auto r = (std::next(itr) == off.end() ? N : *std::next(itr));
add.emplace_back(l, s, s + 1, r, -t);
off.erase(itr);
}
qs.emplace_back(idx, false);
}
}
Add2D rect(X, Y, N + 1);
for (const auto [i, f]: qs) {
if (f) {
ans[i] += rect.get(X[i], Y[i]);
}
else {
const auto [l, r, d, u, v] = add[i];
rect.add(l, r + 1, d, u + 1, v);
}
}
for (const auto x: ans) {
std::cout << x << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
160 ms |
13940 KB |
Output is correct |
2 |
Correct |
192 ms |
14964 KB |
Output is correct |
3 |
Correct |
338 ms |
18300 KB |
Output is correct |
4 |
Correct |
1226 ms |
89764 KB |
Output is correct |
5 |
Correct |
1338 ms |
86620 KB |
Output is correct |
6 |
Correct |
1002 ms |
85336 KB |
Output is correct |
7 |
Correct |
2215 ms |
112692 KB |
Output is correct |
8 |
Correct |
1930 ms |
100032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
2 ms |
620 KB |
Output is correct |
3 |
Correct |
2 ms |
620 KB |
Output is correct |
4 |
Correct |
2 ms |
620 KB |
Output is correct |
5 |
Correct |
571 ms |
57672 KB |
Output is correct |
6 |
Correct |
877 ms |
73780 KB |
Output is correct |
7 |
Correct |
1388 ms |
87320 KB |
Output is correct |
8 |
Correct |
1973 ms |
104884 KB |
Output is correct |
9 |
Correct |
173 ms |
13700 KB |
Output is correct |
10 |
Correct |
187 ms |
14832 KB |
Output is correct |
11 |
Correct |
201 ms |
15540 KB |
Output is correct |
12 |
Correct |
2245 ms |
117444 KB |
Output is correct |
13 |
Correct |
1988 ms |
104784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
620 KB |
Output is correct |
2 |
Correct |
2 ms |
620 KB |
Output is correct |
3 |
Correct |
2 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
2185 ms |
110172 KB |
Output is correct |
6 |
Correct |
1596 ms |
98376 KB |
Output is correct |
7 |
Correct |
1037 ms |
85336 KB |
Output is correct |
8 |
Correct |
475 ms |
64712 KB |
Output is correct |
9 |
Correct |
220 ms |
12244 KB |
Output is correct |
10 |
Correct |
146 ms |
12756 KB |
Output is correct |
11 |
Correct |
224 ms |
12248 KB |
Output is correct |
12 |
Correct |
143 ms |
12780 KB |
Output is correct |
13 |
Correct |
223 ms |
12244 KB |
Output is correct |
14 |
Correct |
143 ms |
12756 KB |
Output is correct |
15 |
Correct |
2194 ms |
117596 KB |
Output is correct |
16 |
Correct |
1940 ms |
104924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
160 ms |
13940 KB |
Output is correct |
9 |
Correct |
192 ms |
14964 KB |
Output is correct |
10 |
Correct |
338 ms |
18300 KB |
Output is correct |
11 |
Correct |
1226 ms |
89764 KB |
Output is correct |
12 |
Correct |
1338 ms |
86620 KB |
Output is correct |
13 |
Correct |
1002 ms |
85336 KB |
Output is correct |
14 |
Correct |
2215 ms |
112692 KB |
Output is correct |
15 |
Correct |
1930 ms |
100032 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
2 ms |
620 KB |
Output is correct |
18 |
Correct |
2 ms |
620 KB |
Output is correct |
19 |
Correct |
2 ms |
620 KB |
Output is correct |
20 |
Correct |
571 ms |
57672 KB |
Output is correct |
21 |
Correct |
877 ms |
73780 KB |
Output is correct |
22 |
Correct |
1388 ms |
87320 KB |
Output is correct |
23 |
Correct |
1973 ms |
104884 KB |
Output is correct |
24 |
Correct |
173 ms |
13700 KB |
Output is correct |
25 |
Correct |
187 ms |
14832 KB |
Output is correct |
26 |
Correct |
201 ms |
15540 KB |
Output is correct |
27 |
Correct |
2245 ms |
117444 KB |
Output is correct |
28 |
Correct |
1988 ms |
104784 KB |
Output is correct |
29 |
Correct |
2 ms |
620 KB |
Output is correct |
30 |
Correct |
2 ms |
620 KB |
Output is correct |
31 |
Correct |
2 ms |
620 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
2185 ms |
110172 KB |
Output is correct |
34 |
Correct |
1596 ms |
98376 KB |
Output is correct |
35 |
Correct |
1037 ms |
85336 KB |
Output is correct |
36 |
Correct |
475 ms |
64712 KB |
Output is correct |
37 |
Correct |
220 ms |
12244 KB |
Output is correct |
38 |
Correct |
146 ms |
12756 KB |
Output is correct |
39 |
Correct |
224 ms |
12248 KB |
Output is correct |
40 |
Correct |
143 ms |
12780 KB |
Output is correct |
41 |
Correct |
223 ms |
12244 KB |
Output is correct |
42 |
Correct |
143 ms |
12756 KB |
Output is correct |
43 |
Correct |
2194 ms |
117596 KB |
Output is correct |
44 |
Correct |
1940 ms |
104924 KB |
Output is correct |
45 |
Correct |
93 ms |
7804 KB |
Output is correct |
46 |
Correct |
139 ms |
8848 KB |
Output is correct |
47 |
Correct |
592 ms |
38652 KB |
Output is correct |
48 |
Correct |
1169 ms |
90284 KB |
Output is correct |
49 |
Correct |
213 ms |
16396 KB |
Output is correct |
50 |
Correct |
210 ms |
16192 KB |
Output is correct |
51 |
Correct |
227 ms |
17264 KB |
Output is correct |
52 |
Correct |
286 ms |
20140 KB |
Output is correct |
53 |
Correct |
288 ms |
20300 KB |
Output is correct |
54 |
Correct |
314 ms |
20276 KB |
Output is correct |