/* be name khoda */
// #define long_enable
#include <iostream>
#include <algorithm>
#include <cstring>
#include <numeric>
#include <vector>
#include <fstream>
#include <set>
#include <map>
using namespace std;
#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif
typedef pair<ll, ll> pii;
typedef pair<pii, ll> ppi;
typedef pair<ll, pii> pip;
typedef vector<ll> vi;
typedef vector<pii> vpii;
#define forifrom(i, s, n) for (ll i = s; i < n; ++i)
#define forirto(i, n, e) for (ll i = (n) - 1; i >= e; --i)
#define fori(i, n) forifrom (i, 0, n)
#define forir(i, n) forirto (i, n, 0)
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))
#define debug(x) cout << #x << " -> " << (x) << endl
#define debug2(x, y) cout << #x << ' ' << #y << " -> " << (x) << ' ' << (y) << endl
#define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> " << (x) << ' ' << (y) << ' ' << (z) << endl
#define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> " << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << endl
#define debuga(x, n) cout << #x << " -> "; fori (i1_da, n) { cout << (x)[i1_da] << ' '; } cout << endl
#define debugaa(x, n, m) cout << #x << " ->\n"; fori (i1_daa, n) { fori (i2_daa, m) { cout << (x)[i1_daa][i2_daa] << ' '; } cout << '\n'; } cout << endl
const ll MOD = 1000000007;
const ll INF = 2000000000;
const long long BIG = 1446803456761533460LL;
#define XL (x << 1)
#define XR (XL | 1)
#define MD ((l + r) >> 1)
#define Add(a, b) a = ((a) + (b)) % MOD
#define Mul(a, b) a = (1LL * (a) * (b)) % MOD
// -----------------------------------------------------------------------
const ll maxn = 300010;
ll N, Q;
string init, street;
set<ll> zrs;
pii qs[maxn];
ll res[maxn];
struct Fenwick {
ll n;
vi ar, fen;
void update(ll i, ll v) {
for (++i; i <= n; i += i & -i) fen[i] += v;
}
void updatelr(ll x, ll y, ll v, bool built) {
if (!built) ar.eb(x), ar.eb(y);
else {
ll l = lower_bound(all(ar), x) - ar.begin();
ll r = lower_bound(all(ar), y) - ar.begin();
update(l, v);
update(r, -v);
}
}
ll get(ll x) {
ll i = upper_bound(all(ar), x) - ar.begin() - 1;
ll v = 0;
for (++i; i; i ^= i & -i) v += fen[i];
return v;
}
};
struct Segment {
Fenwick seg[maxn * 2];
void build() {
forifrom (i, 1, N << 1) {
sort(all(seg[i].ar));
seg[i].n = unique(all(seg[i].ar)) - seg[i].ar.begin();
seg[i].ar.resize(seg[i].n);
seg[i].fen.resize(seg[i].n + 2, 0);
}
}
void update(ll l, ll r, ll li, ll ri, ll v, bool built) {
for (l += N, r += N; l < r; l >>= 1, r >>= 1) {
if (l & 1) seg[l++].updatelr(li, ri, v, built);
if (r & 1) seg[--r].updatelr(li, ri, v, built);
}
}
ll get(ll l, ll r) {
ll s = 0;
for (l += N; l; l >>= 1) s += seg[l].get(r - 1);
return s;
}
} seg;
void enable(ll t, ll p, bool built) {
street[p] = '1';
auto lt = zrs.lower_bound(p);
--lt;
ll x = *lt;
++lt, ++lt;
ll y = *lt;
--lt;
zrs.erase(lt);
seg.update(x + 1, p + 1, p, y, Q - t, built);
}
void disable(ll t, ll p, bool built) {
street[p] = '0';
zrs.insert(p);
auto lt = zrs.lower_bound(p);
--lt;
ll x = *lt;
++lt, ++lt;
ll y = *lt;
seg.update(x + 1, p + 1, p, y, -(Q - t), built);
}
void solve(bool built) {
street = init;
fori (i, N + 2) zrs.insert(i - 1);
fori (i, N) if (street[i] == '1') enable(0, i, built);
fori (i, Q) {
if (qs[i].F == -1) {
if (street[qs[i].S] == '0') enable(i + 1, qs[i].S, built);
else disable(i + 1, qs[i].S, built);
} else if (built) {
res[i] = seg.get(qs[i].F, qs[i].S);
if (*zrs.lower_bound(qs[i].F) >= qs[i].S) res[i] -= Q - (i + 1);
}
}
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> N >> Q >> init;
fori (i, Q) {
string t; cin >> t;
if (t == "toggle") {
ll p; cin >> p; --p;
qs[i] = {-1, p};
} else {
ll l, r; cin >> l >> r; --l; --r;
qs[i] = {l, r};
}
}
solve(false);
seg.build();
solve(true);
fori (i, Q) if (qs[i].F != -1) cout << res[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
33152 KB |
Output is correct |
2 |
Correct |
19 ms |
33272 KB |
Output is correct |
3 |
Correct |
19 ms |
33272 KB |
Output is correct |
4 |
Correct |
19 ms |
33280 KB |
Output is correct |
5 |
Correct |
20 ms |
33280 KB |
Output is correct |
6 |
Correct |
21 ms |
33280 KB |
Output is correct |
7 |
Correct |
20 ms |
33280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
190 ms |
40312 KB |
Output is correct |
2 |
Correct |
206 ms |
40312 KB |
Output is correct |
3 |
Correct |
260 ms |
40952 KB |
Output is correct |
4 |
Correct |
963 ms |
79784 KB |
Output is correct |
5 |
Correct |
1066 ms |
86412 KB |
Output is correct |
6 |
Correct |
1002 ms |
79748 KB |
Output is correct |
7 |
Correct |
574 ms |
70924 KB |
Output is correct |
8 |
Correct |
1572 ms |
126132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
33408 KB |
Output is correct |
2 |
Correct |
21 ms |
33408 KB |
Output is correct |
3 |
Correct |
22 ms |
33408 KB |
Output is correct |
4 |
Correct |
22 ms |
33408 KB |
Output is correct |
5 |
Correct |
1099 ms |
87156 KB |
Output is correct |
6 |
Correct |
1094 ms |
86412 KB |
Output is correct |
7 |
Correct |
988 ms |
85900 KB |
Output is correct |
8 |
Correct |
1783 ms |
126032 KB |
Output is correct |
9 |
Correct |
110 ms |
36984 KB |
Output is correct |
10 |
Correct |
117 ms |
37240 KB |
Output is correct |
11 |
Correct |
115 ms |
37368 KB |
Output is correct |
12 |
Correct |
534 ms |
70796 KB |
Output is correct |
13 |
Correct |
1789 ms |
126260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
33408 KB |
Output is correct |
2 |
Correct |
21 ms |
33408 KB |
Output is correct |
3 |
Correct |
21 ms |
33408 KB |
Output is correct |
4 |
Correct |
22 ms |
33408 KB |
Output is correct |
5 |
Correct |
714 ms |
76940 KB |
Output is correct |
6 |
Correct |
962 ms |
77964 KB |
Output is correct |
7 |
Correct |
1077 ms |
79372 KB |
Output is correct |
8 |
Correct |
1250 ms |
80804 KB |
Output is correct |
9 |
Correct |
214 ms |
40692 KB |
Output is correct |
10 |
Correct |
226 ms |
40600 KB |
Output is correct |
11 |
Correct |
209 ms |
40696 KB |
Output is correct |
12 |
Correct |
214 ms |
40696 KB |
Output is correct |
13 |
Correct |
217 ms |
40676 KB |
Output is correct |
14 |
Correct |
206 ms |
40568 KB |
Output is correct |
15 |
Correct |
494 ms |
70924 KB |
Output is correct |
16 |
Correct |
1760 ms |
126136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
33152 KB |
Output is correct |
2 |
Correct |
19 ms |
33272 KB |
Output is correct |
3 |
Correct |
19 ms |
33272 KB |
Output is correct |
4 |
Correct |
19 ms |
33280 KB |
Output is correct |
5 |
Correct |
20 ms |
33280 KB |
Output is correct |
6 |
Correct |
21 ms |
33280 KB |
Output is correct |
7 |
Correct |
20 ms |
33280 KB |
Output is correct |
8 |
Correct |
190 ms |
40312 KB |
Output is correct |
9 |
Correct |
206 ms |
40312 KB |
Output is correct |
10 |
Correct |
260 ms |
40952 KB |
Output is correct |
11 |
Correct |
963 ms |
79784 KB |
Output is correct |
12 |
Correct |
1066 ms |
86412 KB |
Output is correct |
13 |
Correct |
1002 ms |
79748 KB |
Output is correct |
14 |
Correct |
574 ms |
70924 KB |
Output is correct |
15 |
Correct |
1572 ms |
126132 KB |
Output is correct |
16 |
Correct |
21 ms |
33408 KB |
Output is correct |
17 |
Correct |
21 ms |
33408 KB |
Output is correct |
18 |
Correct |
22 ms |
33408 KB |
Output is correct |
19 |
Correct |
22 ms |
33408 KB |
Output is correct |
20 |
Correct |
1099 ms |
87156 KB |
Output is correct |
21 |
Correct |
1094 ms |
86412 KB |
Output is correct |
22 |
Correct |
988 ms |
85900 KB |
Output is correct |
23 |
Correct |
1783 ms |
126032 KB |
Output is correct |
24 |
Correct |
110 ms |
36984 KB |
Output is correct |
25 |
Correct |
117 ms |
37240 KB |
Output is correct |
26 |
Correct |
115 ms |
37368 KB |
Output is correct |
27 |
Correct |
534 ms |
70796 KB |
Output is correct |
28 |
Correct |
1789 ms |
126260 KB |
Output is correct |
29 |
Correct |
22 ms |
33408 KB |
Output is correct |
30 |
Correct |
21 ms |
33408 KB |
Output is correct |
31 |
Correct |
21 ms |
33408 KB |
Output is correct |
32 |
Correct |
22 ms |
33408 KB |
Output is correct |
33 |
Correct |
714 ms |
76940 KB |
Output is correct |
34 |
Correct |
962 ms |
77964 KB |
Output is correct |
35 |
Correct |
1077 ms |
79372 KB |
Output is correct |
36 |
Correct |
1250 ms |
80804 KB |
Output is correct |
37 |
Correct |
214 ms |
40692 KB |
Output is correct |
38 |
Correct |
226 ms |
40600 KB |
Output is correct |
39 |
Correct |
209 ms |
40696 KB |
Output is correct |
40 |
Correct |
214 ms |
40696 KB |
Output is correct |
41 |
Correct |
217 ms |
40676 KB |
Output is correct |
42 |
Correct |
206 ms |
40568 KB |
Output is correct |
43 |
Correct |
494 ms |
70924 KB |
Output is correct |
44 |
Correct |
1760 ms |
126136 KB |
Output is correct |
45 |
Correct |
117 ms |
37552 KB |
Output is correct |
46 |
Correct |
131 ms |
37496 KB |
Output is correct |
47 |
Correct |
440 ms |
50936 KB |
Output is correct |
48 |
Correct |
936 ms |
79628 KB |
Output is correct |
49 |
Correct |
115 ms |
37408 KB |
Output is correct |
50 |
Correct |
111 ms |
37368 KB |
Output is correct |
51 |
Correct |
120 ms |
37368 KB |
Output is correct |
52 |
Correct |
121 ms |
37368 KB |
Output is correct |
53 |
Correct |
131 ms |
37588 KB |
Output is correct |
54 |
Correct |
126 ms |
37368 KB |
Output is correct |