Submission #75820034
Source Code Expand
#include <bits/stdc++.h>
#define fastio cin.tie(0)->sync_with_stdio(0)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define sz(x) (int)(x).size()
using namespace std;
typedef long long ll;
struct Node {
int l, sum, hs, pt, pl, st, sl;
};
template<typename Node> struct SegTree {
int n, size;
Node e; // 항등원
vector<Node> tree;
function<Node(Node, Node)> func;
SegTree(int n, const Node& e, auto func) : n(n), size(1<<(__lg(n)+1)), e(e), tree(size<<1, e), func(func) {}
SegTree(const vector<Node>& v, const Node& e, auto func) : n(sz(v)), size(1<<(__lg(n)+1)), e(e), tree(size<<1, e), func(func) {
for (int i = 0; i < n; i++) tree[i+size] = v[i];
for (int i = size-1; i > 0; i--) tree[i] = func(tree[i<<1], tree[i<<1 | 1]);
}
Node query(int i) { return tree[i + size]; }
Node query(int l, int r) {
Node L = e, R = e;
for (l += size, r += size; l <= r; l >>= 1, r >>= 1) {
if (l & 1) L = func(L, tree[l++]);
if (~r & 1) R = func(tree[r--], R);
}
return func(L, R);
}
};
int main() {
fastio; int N, Q; cin >> N >> Q;
string s; cin >> s;
vector<Node> v(N);
for (int i = 0; i < N; i++) {
int t = 0;
if (s[i] == 'R') t = 1;
else if (s[i] == 'G') t = 2;
else if (s[i] == 'B') t = 3;
if (t == 0) v[i] = { 1, 0, 0, 0, 0, 0, 0 };
else v[i] = { 1, 1, 0, t, 1, t, 1 };
}
SegTree<Node> sg(v, { 0, 0, 0, 0, 0, 0, 0 }, [](Node a, Node b) {
Node res;
res.l = a.l + b.l;
res.sum = a.sum + b.sum;
res.hs = a.hs + b.hs;
if (a.st == b.pt && a.st != 0) {
if (a.sl%2 && b.pl%2) res.hs++;
}
if (a.l == a.pl) {
res.pl = a.l + (a.st == b.pt && a.st != 0 ? b.pl : 0);
}
else res.pl = a.pl;
res.pt = a.pt;
if (b.l == b.sl) {
res.sl = b.l + (a.st == b.pt && a.st != 0 ? a.sl : 0);
}
else res.sl = b.sl;
res.st = b.st;
return res;
});
for (int q = 0; q < Q; q++) {
int l, r; cin >> l >> r; l--; r--;
auto res = sg.query(l, r);
cout << res.hs << "\n";
}
return 0;
}
Submission Info
| Submission Time |
|
| Task |
D - Coloring |
| User |
Lov34ever |
| Language |
C++23 (GCC 15.2.0) |
| Score |
100 |
| Code Size |
2394 Byte |
| Status |
AC |
| Exec Time |
153 ms |
| Memory |
40672 KiB |
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
100 / 100 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
00-sample-001.txt |
| All |
00-sample-001.txt, 01-001.txt, 01-002.txt, 01-003.txt, 01-004.txt, 01-005.txt, 01-006.txt, 01-007.txt, 01-008.txt, 01-009.txt, 01-010.txt, 01-011.txt, 01-012.txt, 01-013.txt, 01-014.txt, 01-015.txt, 01-016.txt, 01-017.txt, 01-018.txt, 01-019.txt, 01-020.txt, 01-021.txt, 01-022.txt, 01-023.txt, 01-024.txt, 01-025.txt, 01-026.txt, 01-027.txt, 01-028.txt, 01-029.txt, 01-030.txt, 01-031.txt, 01-032.txt, 01-033.txt, 01-034.txt, 01-035.txt, 01-036.txt, 01-037.txt, 01-038.txt, 01-039.txt, 01-040.txt, 01-041.txt, 01-042.txt, 01-043.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 00-sample-001.txt |
AC |
1 ms |
3440 KiB |
| 01-001.txt |
AC |
143 ms |
40564 KiB |
| 01-002.txt |
AC |
145 ms |
40464 KiB |
| 01-003.txt |
AC |
145 ms |
40668 KiB |
| 01-004.txt |
AC |
146 ms |
40488 KiB |
| 01-005.txt |
AC |
144 ms |
40464 KiB |
| 01-006.txt |
AC |
145 ms |
40416 KiB |
| 01-007.txt |
AC |
152 ms |
40488 KiB |
| 01-008.txt |
AC |
144 ms |
40664 KiB |
| 01-009.txt |
AC |
145 ms |
40452 KiB |
| 01-010.txt |
AC |
146 ms |
40552 KiB |
| 01-011.txt |
AC |
143 ms |
40444 KiB |
| 01-012.txt |
AC |
146 ms |
40648 KiB |
| 01-013.txt |
AC |
147 ms |
40568 KiB |
| 01-014.txt |
AC |
153 ms |
40532 KiB |
| 01-015.txt |
AC |
151 ms |
40576 KiB |
| 01-016.txt |
AC |
128 ms |
40584 KiB |
| 01-017.txt |
AC |
128 ms |
40608 KiB |
| 01-018.txt |
AC |
136 ms |
40636 KiB |
| 01-019.txt |
AC |
125 ms |
40672 KiB |
| 01-020.txt |
AC |
102 ms |
40496 KiB |
| 01-021.txt |
AC |
103 ms |
40644 KiB |
| 01-022.txt |
AC |
106 ms |
40552 KiB |
| 01-023.txt |
AC |
92 ms |
40608 KiB |
| 01-024.txt |
AC |
108 ms |
40612 KiB |
| 01-025.txt |
AC |
143 ms |
40632 KiB |
| 01-026.txt |
AC |
138 ms |
40644 KiB |
| 01-027.txt |
AC |
140 ms |
40532 KiB |
| 01-028.txt |
AC |
133 ms |
40472 KiB |
| 01-029.txt |
AC |
117 ms |
40564 KiB |
| 01-030.txt |
AC |
142 ms |
40464 KiB |
| 01-031.txt |
AC |
145 ms |
40552 KiB |
| 01-032.txt |
AC |
145 ms |
40668 KiB |
| 01-033.txt |
AC |
137 ms |
40444 KiB |
| 01-034.txt |
AC |
102 ms |
40652 KiB |
| 01-035.txt |
AC |
99 ms |
40464 KiB |
| 01-036.txt |
AC |
92 ms |
40556 KiB |
| 01-037.txt |
AC |
134 ms |
40640 KiB |
| 01-038.txt |
AC |
1 ms |
3440 KiB |
| 01-039.txt |
AC |
1 ms |
3408 KiB |
| 01-040.txt |
AC |
1 ms |
3440 KiB |
| 01-041.txt |
AC |
1 ms |
3392 KiB |
| 01-042.txt |
AC |
1 ms |
3440 KiB |
| 01-043.txt |
AC |
1 ms |
3464 KiB |