// #pragma GCC optimize ("Ofast")
// #pragma GCC target ("avx2")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
// #define int long long
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)
#define FOS(i, r, l) for (int i = (r); i >= (l); --i)
#define FRN(i, n) for (int i = 0; i < (n); ++i)
#define FSN(i, n) for (int i = (n) - 1; i >= 0; --i)
#define EACH(i, x) for (auto &i : (x))
#define WHILE while
template<typename T> T gcd(T a, T b) { T d2 = (a | b) & -(a | b); a /= d2; b /= d2; WHILE(b) { a = a % b; swap(a, b); } return a * d2; }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
void _assert(bool statement) { if (statement) return; cerr << "\n>> Assertion failed!\n"; exit(0); }
void _assert(bool statement, const str &message) { if (statement) return; cerr << "\n>> Assertion failed: " << message << '\n'; exit(0); }
void _error(const str &message) { cerr << "\n>> Error: " << message << '\n'; exit(0); }
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Tran The Bao - ghostwriter
Training for VOI23 gold medal
----------------------------------------------------------------
GOAT
----------------------------------------------------------------
*/
struct Event {
str type;
int i, a, b;
Event() {}
};
const int oo = 1e9 + 5;
const int N = 3e5 + 5;
int n, q;
char s[N];
Event event[N];
void input(int test_id) {
cin >> n >> q;
FOR(i, 1, n) cin >> s[i];
FOR(i, 1, q) {
cin >> event[i].type;
if (event[i].type == "toggle") cin >> event[i].i;
else {
cin >> event[i].a >> event[i].b;
--event[i].b;
}
}
}
struct SegTree1d {
int n;
struct Node {
Node *left, *right;
int sum;
Node() : left(0), right(0), sum(0) {}
};
Node *root;
void setSize(int n) {
this -> n = n;
root = new Node();
}
void update(Node *cur, int l, int r, int q, int v) {
cur -> sum += v;
if (l == r) return;
int mid = l + (r - l) / 2;
if (mid >= q) {
if (!cur -> left) cur -> left = new Node();
update(cur -> left, l, mid, q, v);
}
else {
if (!cur -> right) cur -> right = new Node();
update(cur -> right, mid + 1, r, q, v);
}
}
void update(int q, int v) {
update(root, 1, n, q, v);
}
int getSum(Node *cur, int l, int r, int q) {
if (r <= q) return cur -> sum;
int mid = l + (r - l) / 2, ans = 0;
if (cur -> left) ans += getSum(cur -> left, l, mid, q);
if (mid + 1 <= q && cur -> right) ans += getSum(cur -> right, mid + 1, r, q);
return ans;
}
int getSum(int q) {
return getSum(root, 1, n, q);
}
};
struct SegTree2d {
int n;
struct Node {
Node *left, *right;
int sum;
SegTree1d seg;
Node(int n) {
seg.setSize(n);
left = right = 0;
}
};
Node *root;
SegTree2d(int n) : n(n) {
root = new Node(n);
}
void update(Node *cur, int l, int r, int x, int y, int v) {
cur -> seg.update(y, v);
if (l == r) return;
int mid = l + (r - l) / 2;
if (mid >= x) {
if (!cur -> left) cur -> left = new Node(n);
update(cur -> left, l, mid, x, y, v);
}
else {
if (!cur -> right) cur -> right = new Node(n);
update(cur -> right, mid + 1, r, x, y, v);
}
}
void update(int x, int y, int v) {
update(root, 1, n, x, y, v);
}
int getSum(Node *cur, int l, int r, int x, int y) {
if (r <= x) return cur -> seg.getSum(y);
int mid = l + (r - l) / 2, ans = 0;
if (cur -> left) ans += getSum(cur -> left, l, mid, x, y);
if (mid + 1 <= x && cur -> right) ans += getSum(cur -> right, mid + 1, r, x, y);
return ans;
}
int getSum(int x, int y) {
return getSum(root, 1, n, x, y);
}
};
void update(SegTree2d &seg, int xl, int xr, int yl, int yr, int v) {
seg.update(xl, yl, v);
if (yr < n) seg.update(xl, yr + 1, -v);
if (xr < n) seg.update(xr + 1, yl, -v);
if (xr < n && yr < n) seg.update(xr + 1, yr + 1, v);
}
void toggle(SegTree2d &seg, multiset<pi> &segment, int i, int j) {
s[i] ^= 1;
if (s[i] == '1') {
int lm = i, rm = i;
if (s[i - 1] == '1') lm = (*--segment.ub({i, oo})).st;
if (s[i + 1] == '1') rm = (*segment.ub({i, oo})).nd;
if (lm == -oo) lm = i;
if (rm == oo) rm = i;
update(seg, lm, i, i, rm, -j);
if (lm < i) segment.ers(--segment.ub({i, oo}));
if (rm > i) segment.ers(segment.ub({i, oo}));
segment.ins({lm, rm});
}
else {
pi tmp = *--segment.ub({i, oo});
int lm = tmp.st, rm = tmp.nd;
update(seg, lm, i, i, rm, j);
segment.ers(--segment.ub({i, oo}));
if (lm < i) segment.ins({lm, i - 1});
if (rm > i) segment.ins({i + 1, rm});
}
}
int getAns(SegTree2d &seg, multiset<pi> &segment, int a, int b, int j) {
int ans = seg.getSum(a, b);
pi tmp = *--segment.ub({a, oo});
if (tmp.st <= a && b <= tmp.nd) ans += j;
return ans;
}
void solve(int test_id) {
SegTree2d seg(n);
multiset<pi> segment;
segment.ins({-oo, -oo});
segment.ins({oo, oo});
int pre = 0;
FOR(i, 1, n + 1)
if (i == n + 1 || s[i] == '0') {
if (pre + 1 < i) segment.ins({pre + 1, i - 1});
pre = i;
}
FOR(j, 1, q)
if (event[j].type == "toggle") {
int i = event[j].i;
toggle(seg, segment, i, j);
}
else {
int a = event[j].a, b = event[j].b;
cout << getAns(seg, segment, a, b, j) << '\n';
}
}
void reinit(int test_id) {
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
int test_num = 1;
// cin >> test_num; // comment if the problem does not requires multitest
FOR(i, 1, test_num) {
input(i); // input in noninteractive problems for case #i
solve(i); // main function to solve case #i
reinit(i); // reinit global data to default used in case #i
}
#ifdef LOCAL
cerr << "\nTime: " << setprecision(5) << fixed << (ldb)clock() / CLOCKS_PER_SEC << "ms.\n";
#endif
return 0;
}
/*
----------------------------------------------------------------
From Benq:
stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
9 ms |
14372 KB |
Output is correct |
3 |
Correct |
9 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14292 KB |
Output is correct |
7 |
Correct |
7 ms |
14292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
259 ms |
15572 KB |
Output is correct |
2 |
Correct |
389 ms |
18752 KB |
Output is correct |
3 |
Correct |
846 ms |
31344 KB |
Output is correct |
4 |
Runtime error |
2005 ms |
524288 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16204 KB |
Output is correct |
2 |
Correct |
14 ms |
15916 KB |
Output is correct |
3 |
Correct |
10 ms |
15476 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Runtime error |
1421 ms |
524288 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
15316 KB |
Output is correct |
3 |
Correct |
10 ms |
15588 KB |
Output is correct |
4 |
Correct |
10 ms |
15828 KB |
Output is correct |
5 |
Correct |
354 ms |
57224 KB |
Output is correct |
6 |
Correct |
1578 ms |
466080 KB |
Output is correct |
7 |
Runtime error |
1640 ms |
524288 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
9 ms |
14372 KB |
Output is correct |
3 |
Correct |
9 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14292 KB |
Output is correct |
7 |
Correct |
7 ms |
14292 KB |
Output is correct |
8 |
Correct |
259 ms |
15572 KB |
Output is correct |
9 |
Correct |
389 ms |
18752 KB |
Output is correct |
10 |
Correct |
846 ms |
31344 KB |
Output is correct |
11 |
Runtime error |
2005 ms |
524288 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |