#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double dou;
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define MASK(x) (1LL<<(x))
#define BITj(x, j) (((x)>>(j))&1)
template<typename T> bool maximize(T &x, const T &y) {
if (x < y) {x = y; return 1;}
return 0;
}
template<typename T> bool minimize(T &x, const T &y) {
if (x > y) {x = y; return 1;}
return 0;
}
struct BIT{
int n;
vector<int> bit;
void init(int _n = 0) {
n = _n;
bit.assign(n+1, 0);
}
void update(int k, int x) {
while (k <= n) {
bit[k] += x;
k += k & (-k);
}
}
int getsum(int k) {
int res = 0;
while (k > 0) {
res += bit[k];
k -= k & (-k);
}
return res;
}
};
const int maxn = 3e5+5;
int n, q;
namespace sub1{
bool check(){
return (n <= 105 && q <= 105);
}
bool getval(string s, int l, int r) {
for (int i= l; i <= r; i++) {
if (s[i] == '0') return 0;
}
return 1;
}
void solve() {
string cur;
vector<string> s;
cin >> cur;
while (q--) {
s.pb(cur);
string type;
cin >> type;
if (type[0] == 't') {
int i;
cin >> i;
int x = cur[i-1]-'0';
x = (x+1)%2;
cur[i-1] = x+'0';
}
else {
int l, r;
cin >> l >> r;
int ans = 0;
for (int i= 0; i < (int)s.size(); i++) {
ans += getval(s[i], l-1, r-2);
}
cout << ans << "\n";
}
}
}
}
namespace sub5{
struct event{
bool type;
int l, r, val; //ans in case of queries
event(bool _type, int _l, int _r, int _val = 0) {
type = _type;
l = _l;
r = _r;
val = _val;
}
void check() {
cout << type << ' ' << l << ' ' << r << ' ' << val << "\n";
}
};
bool cmp(event a, event b) {
if (a.r != b.r) return a.r > b.r;
return a.type < b.type;
}
vector<event> all;
BIT bit;
void dnc(int ql, int qr) {
if (ql >= qr) return;
// system("pause");
int mid = (ql+qr)>>1;
//continue
dnc(ql, mid);
dnc(mid+1, qr);
// cout << "dnc: " << ql << ' ' << qr << "\n";
//solve
vector<event> e;
bool can;
can = 0;
for (int i = ql; i <= mid; i++) {
if (all[i].type == 0) {
can = 1;
e.pb(all[i]);
}
}
if (!can) return;
can = 0;
for (int i = mid+1; i <= qr; i++) {
if (all[i].type == 1) {
can = 1;
e.pb(event(all[i].type, all[i].l, all[i].r, i));
}
}
if (!can) return;
sort(e.begin(), e.end(), cmp);
for (int i = 0; i < (int)e.size(); i++) {
if (e[i].type == 0) {
bit.update(e[i].l, e[i].val);
}
else {
all[e[i].val].val += bit.getsum(e[i].l);
}
// e[i].check();
}
//reset
for (int i= 0; i < (int)e.size(); i++) {
if (e[i].type == 0) {
bit.update(e[i].l, -e[i].val);
}
}
}
void solve() {
string s;
cin >> s;
//init
bit.init(n);
multiset<pii> st;
int j = 0;
while (j < n) {
while (s[j] == '0' && j < n) ++j;
int i;
for (i = j; i < n; i++) {
if (s[i] == '0') {
st.insert({j+1, i});
all.pb(event(0, j+1, i, q+1));
// cout << j+1 << ' ' << i << endl;
break;
}
if (s[i] == '1' && i == n-1) {
st.insert({j+1, i+1});
all.pb(event(0, j+1, i+1, q+1));
}
}
j = i+1;
}
// system("pause");
//push events
for (int t = 1; t <= q; t++) {
// system("pause");
string type;
cin >> type;
if (type[0] == 't') {
//update
int j;
cin >> j;
auto it = st.upper_bound({j, n+1});
if (it != st.begin()) {
--it;
int l = (*it).fi, r = (*it).se;
if (l <= j && j <= r) {
//case remove
all.pb(event(0, l, r, -(q+1-t)));
st.erase(it);
if (l < j) {
all.pb(event(0, l, j-1, q+1-t));
st.insert({l, j-1});
}
if (j < r) {
all.pb(event(0, j+1, r, q+1-t));
st.insert({j+1, r});
}
continue;
}
//check case join
++it;
if (it != st.end()) {
int l2 = (*it).fi, r2 = (*it).se;
if (r == j-1 && l2 == j+1) {
//join
all.pb(event(0, l2, r2, -(q+1-t)));
auto temp = it;
--it;
st.erase(temp);
all.pb(event(0, l, r, -(q+1-t)));
st.erase(it);
all.pb(event(0, l, r2, q+1-t));
st.insert({l, r2});
continue;
}
}
if (r == j-1) {
//con
--it;
all.pb(event(0, l, r, -(q+1-t)));
st.erase(it);
all.pb(event(0, l, j, q+1-t));
st.insert({l, j});
continue;
}
}
if (it != st.end()) {
int l = (*it).fi, r = (*it).se;
if (l == j+1) {
//con
all.pb(event(0, l, r, -(q+1-t)));
st.erase(it);
all.pb(event(0, j, r, q+1-t));
st.insert({j, r});
continue;
}
}
//case new
all.pb(event(0, j, j, q+1-t));
st.insert({j, j});
}
else {
int l, r;
cin >> l >> r;
all.pb(event(1, l, r-1, 0));
//case last
auto it = st.upper_bound({l, n+1});
if (it != st.begin()) {
--it;
int l2 = (*it).fi, r2 = (*it).se;
// cout << l << ' ' << r-1 << ' ' << l2 << ' ' << r2 << "\n";
if (l2 <= l && r-1 <= r2) {
all.back().val += -(q+1-t);
}
}
}
}
// cout << "finish pushval" << endl;
// system("pause");
//cdq
// for (int i = 0; i < (int)all.size(); i++) {
// all[i].check();
// }
dnc(0, (int)all.size()-1);
for (int i = 0; i < (int)all.size(); i++) {
if (all[i].type == 1) {
cout << all[i].val << "\n";
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// #define name "task"
// if (fopen(name".inp", "r")) {
// freopen(name".inp", "r", stdin);
// freopen(name".out", "w", stdout);
// }
cin >> n >> q;
// if (sub1::check()) return sub1::solve(), 0;
return sub5::solve(), 0;
}
/*
4 2
0111
toggle 2
toggle 2
4 4
0111
toggle 1
query 1 2
toggle 2
toggle 2
5 7
11011
query 1 2
query 1 2
query 1 6
query 3 4
toggle 3
query 3 4
query 1 6
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
504 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
377 ms |
19372 KB |
Output is correct |
2 |
Correct |
398 ms |
20444 KB |
Output is correct |
3 |
Correct |
439 ms |
20336 KB |
Output is correct |
4 |
Correct |
579 ms |
33664 KB |
Output is correct |
5 |
Correct |
587 ms |
36032 KB |
Output is correct |
6 |
Correct |
300 ms |
36844 KB |
Output is correct |
7 |
Correct |
69 ms |
17616 KB |
Output is correct |
8 |
Correct |
108 ms |
21704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
464 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
532 ms |
32452 KB |
Output is correct |
6 |
Correct |
640 ms |
32136 KB |
Output is correct |
7 |
Correct |
600 ms |
34748 KB |
Output is correct |
8 |
Correct |
104 ms |
22212 KB |
Output is correct |
9 |
Correct |
103 ms |
11196 KB |
Output is correct |
10 |
Correct |
122 ms |
17500 KB |
Output is correct |
11 |
Correct |
116 ms |
16700 KB |
Output is correct |
12 |
Correct |
72 ms |
17620 KB |
Output is correct |
13 |
Correct |
110 ms |
21452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
224 ms |
31860 KB |
Output is correct |
6 |
Correct |
274 ms |
33360 KB |
Output is correct |
7 |
Correct |
303 ms |
35516 KB |
Output is correct |
8 |
Correct |
300 ms |
36828 KB |
Output is correct |
9 |
Correct |
173 ms |
26404 KB |
Output is correct |
10 |
Correct |
171 ms |
27084 KB |
Output is correct |
11 |
Correct |
166 ms |
26320 KB |
Output is correct |
12 |
Correct |
152 ms |
29880 KB |
Output is correct |
13 |
Correct |
169 ms |
26212 KB |
Output is correct |
14 |
Correct |
162 ms |
26804 KB |
Output is correct |
15 |
Correct |
71 ms |
17668 KB |
Output is correct |
16 |
Correct |
111 ms |
20948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
504 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
377 ms |
19372 KB |
Output is correct |
9 |
Correct |
398 ms |
20444 KB |
Output is correct |
10 |
Correct |
439 ms |
20336 KB |
Output is correct |
11 |
Correct |
579 ms |
33664 KB |
Output is correct |
12 |
Correct |
587 ms |
36032 KB |
Output is correct |
13 |
Correct |
300 ms |
36844 KB |
Output is correct |
14 |
Correct |
69 ms |
17616 KB |
Output is correct |
15 |
Correct |
108 ms |
21704 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
464 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
532 ms |
32452 KB |
Output is correct |
21 |
Correct |
640 ms |
32136 KB |
Output is correct |
22 |
Correct |
600 ms |
34748 KB |
Output is correct |
23 |
Correct |
104 ms |
22212 KB |
Output is correct |
24 |
Correct |
103 ms |
11196 KB |
Output is correct |
25 |
Correct |
122 ms |
17500 KB |
Output is correct |
26 |
Correct |
116 ms |
16700 KB |
Output is correct |
27 |
Correct |
72 ms |
17620 KB |
Output is correct |
28 |
Correct |
110 ms |
21452 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
596 KB |
Output is correct |
33 |
Correct |
224 ms |
31860 KB |
Output is correct |
34 |
Correct |
274 ms |
33360 KB |
Output is correct |
35 |
Correct |
303 ms |
35516 KB |
Output is correct |
36 |
Correct |
300 ms |
36828 KB |
Output is correct |
37 |
Correct |
173 ms |
26404 KB |
Output is correct |
38 |
Correct |
171 ms |
27084 KB |
Output is correct |
39 |
Correct |
166 ms |
26320 KB |
Output is correct |
40 |
Correct |
152 ms |
29880 KB |
Output is correct |
41 |
Correct |
169 ms |
26212 KB |
Output is correct |
42 |
Correct |
162 ms |
26804 KB |
Output is correct |
43 |
Correct |
71 ms |
17668 KB |
Output is correct |
44 |
Correct |
111 ms |
20948 KB |
Output is correct |
45 |
Correct |
198 ms |
16824 KB |
Output is correct |
46 |
Correct |
246 ms |
16176 KB |
Output is correct |
47 |
Correct |
346 ms |
18068 KB |
Output is correct |
48 |
Correct |
576 ms |
33728 KB |
Output is correct |
49 |
Correct |
136 ms |
18000 KB |
Output is correct |
50 |
Correct |
128 ms |
16812 KB |
Output is correct |
51 |
Correct |
121 ms |
17336 KB |
Output is correct |
52 |
Correct |
87 ms |
17632 KB |
Output is correct |
53 |
Correct |
87 ms |
17852 KB |
Output is correct |
54 |
Correct |
88 ms |
18356 KB |
Output is correct |