#pragma GCC optimize("O2")
#include <bits/stdc++.h>
#ifdef DEBUG
#include "debug.hpp"
#endif
using namespace std;
#define all(c) (c).begin(), (c).end()
#define traverse(c, it) for(auto it = (c).begin(); it != (c).end(); it++)
#define rep(i, N) for(int i = 0; i < (N); i++)
#define rep1(i, N) for(int i = 1; i <= (N); i++)
#define rep2(i, s, e) for(int i = (s); i <= (e); i++)
#define rep3(i, s, e, d) for(int i = (s); (d) >= 0 ? i <= (e) : i >= (e); i += (d))
#define pb push_back
#ifdef DEBUG
#define debug(x...) {dbg::depth++; string dbg_vals = dbg::to_string(x); dbg::depth--; dbg::fprint(__func__, __LINE__, #x, dbg_vals);}
#define light_debug(x) {dbg::light = 1; dbg::dout << __func__ << ":" << __LINE__ << " " << #x << " = " << x << endl; dbg::light = 0;}
#else
#define debug(x...)
#define light_debug(x)
#endif
template<typename T>
T& ckmin(T& a, T b){ return a = a > b ? b : a; }
template<typename T>
T& ckmax(T& a, T b){ return a = a < b ? b : a; }
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
template<int n>
struct BIT{
array<int, n + 1> a;
vi modifications;
void update(int i, int v){
for(i++; i <= n; i += i & -i) a[i] += v, modifications.pb(i);
}
void rupdate(int i, int v){
update(1, v);
update(i + 1, -v);
}
int query(int i) const {
int s = 0;
for(i++; i; i -= i & -i) s += a[i];
return s;
}
void clear(){
for(int i : modifications) a[i] = 0;
modifications.clear();
}
};
struct interval{
int l, r, t; // [l, r) is one at time t;
bool operator <(const interval& x) const{
return l < x.l; // Enough, because no two intervals can have the same left endpoint (disjoint intervals).
}
};
string to_string(interval x){
return "{" + to_string(x.l) + ", " + to_string(x.r) + ", " + to_string(x.t) + "}";
}
struct offline_interval{
int l, r, t, d;
};
string to_string(offline_interval x){
return "{" + to_string(x.l) + ", " + to_string(x.r) + ", " + to_string(x.t) + ", " + to_string(x.d) + "}";
}
vi is_query, ans;
vector<offline_interval> a;
BIT<300005> bit;
void CDQ(int s, int e){
debug(s, e);
if(e - s <= 1)
return;
int m = (s + e) >> 1;
CDQ(s, m), CDQ(m, e);
vector<offline_interval> tmp;
tmp.reserve(e - s);
for(int x = s, y = m; x < m || y < e;){
if(x < m && (y == e || a[x].l <= a[y].l)){
if(!is_query[a[x].t])
bit.rupdate(a[x].r, a[x].d);
tmp.pb(a[x++]);
}else{
if(is_query[a[y].t])
ans[a[y].t] += bit.query(a[y].r);
tmp.pb(a[y++]);
}
}
bit.clear();
rep2(i, s, e - 1)
a[i] = tmp[i - s];
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
#ifdef DEBUG
freopen("debug", "w", stderr);
#endif
int n, q;
cin >> n >> q;
string status;
cin >> status;
is_query.resize(q + 1), ans.resize(q + 1);
set<interval> intervals;
for(int i = 0; i < n;){
while(i < n && status[i] == '0') i++;
int e = i;
for(; e < n && status[e] == '1'; e++);
if(e != i) intervals.insert({i, e, 0});
i = e;
}
auto toggle = [&](int i, int t){
auto next_ = intervals.upper_bound({i, -1, -1});
if(status[i] == '1'){
assert(next_ != intervals.begin());
auto cur = prev(next_);
auto tmp = *cur;
intervals.erase(cur);
a.pb({tmp.l, tmp.r, t, t - tmp.t});
int l = tmp.l, r = tmp.r;
if(l < i)
intervals.insert({l, i, t});
if(r > i + 1)
intervals.insert({i + 1, r, t});
status[i] = '0';
} else {
interval itvl = {i, i + 1, t};
if(next_ != intervals.begin() && prev(next_)->r == i){
auto pre = prev(next_);
itvl.l = pre->l;
a.pb({pre->l, pre->r, t, t - pre->t});
intervals.erase(pre);
}
if(next_ != intervals.end() && next_->l == i + 1){
itvl.r = next_->r;
a.pb({next_->l, next_->r, t, t - next_->t});
intervals.erase(next_);
}
intervals.insert(itvl);
status[i] = '1';
}
};
rep1(t, q){
string qtype;
cin >> qtype;
if(qtype == "toggle"){
int i;
cin >> i;
toggle(--i, t);
debug(t, status, intervals);
} else {
int l, r;
is_query[t] = 1;
cin >> l >> r;
--l, --r;
auto next_ = intervals.upper_bound({l, -1, -1});
if(next_ != intervals.begin()){
next_--;
if(next_->l <= l && r <= next_->r)
ans[t] += t - next_->t;
}
a.pb({l, r, t, 0});
}
}
debug(a);
CDQ(0, a.size());
debug(a);
rep1(i, q)
if(is_query[i]) cout << ans[i] << '\n';
#ifdef DEBUG
dbg::dout << "\nExecution time: " << clock() << "ms\n";
#endif
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
559 ms |
30156 KB |
Output is correct |
2 |
Correct |
568 ms |
33740 KB |
Output is correct |
3 |
Correct |
592 ms |
34636 KB |
Output is correct |
4 |
Correct |
713 ms |
36512 KB |
Output is correct |
5 |
Correct |
810 ms |
45184 KB |
Output is correct |
6 |
Correct |
810 ms |
41980 KB |
Output is correct |
7 |
Correct |
271 ms |
20624 KB |
Output is correct |
8 |
Correct |
270 ms |
20720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
875 ms |
38600 KB |
Output is correct |
6 |
Correct |
895 ms |
41328 KB |
Output is correct |
7 |
Correct |
779 ms |
45540 KB |
Output is correct |
8 |
Correct |
279 ms |
20712 KB |
Output is correct |
9 |
Correct |
146 ms |
14568 KB |
Output is correct |
10 |
Correct |
191 ms |
15960 KB |
Output is correct |
11 |
Correct |
168 ms |
16088 KB |
Output is correct |
12 |
Correct |
269 ms |
20764 KB |
Output is correct |
13 |
Correct |
277 ms |
20716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
411 ms |
27360 KB |
Output is correct |
6 |
Correct |
620 ms |
42360 KB |
Output is correct |
7 |
Correct |
827 ms |
41992 KB |
Output is correct |
8 |
Correct |
951 ms |
41472 KB |
Output is correct |
9 |
Correct |
668 ms |
48088 KB |
Output is correct |
10 |
Correct |
805 ms |
47172 KB |
Output is correct |
11 |
Correct |
691 ms |
47940 KB |
Output is correct |
12 |
Correct |
792 ms |
47160 KB |
Output is correct |
13 |
Correct |
671 ms |
47864 KB |
Output is correct |
14 |
Correct |
799 ms |
47160 KB |
Output is correct |
15 |
Correct |
275 ms |
20716 KB |
Output is correct |
16 |
Correct |
291 ms |
20716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
559 ms |
30156 KB |
Output is correct |
9 |
Correct |
568 ms |
33740 KB |
Output is correct |
10 |
Correct |
592 ms |
34636 KB |
Output is correct |
11 |
Correct |
713 ms |
36512 KB |
Output is correct |
12 |
Correct |
810 ms |
45184 KB |
Output is correct |
13 |
Correct |
810 ms |
41980 KB |
Output is correct |
14 |
Correct |
271 ms |
20624 KB |
Output is correct |
15 |
Correct |
270 ms |
20720 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
875 ms |
38600 KB |
Output is correct |
21 |
Correct |
895 ms |
41328 KB |
Output is correct |
22 |
Correct |
779 ms |
45540 KB |
Output is correct |
23 |
Correct |
279 ms |
20712 KB |
Output is correct |
24 |
Correct |
146 ms |
14568 KB |
Output is correct |
25 |
Correct |
191 ms |
15960 KB |
Output is correct |
26 |
Correct |
168 ms |
16088 KB |
Output is correct |
27 |
Correct |
269 ms |
20764 KB |
Output is correct |
28 |
Correct |
277 ms |
20716 KB |
Output is correct |
29 |
Correct |
1 ms |
512 KB |
Output is correct |
30 |
Correct |
2 ms |
512 KB |
Output is correct |
31 |
Correct |
2 ms |
512 KB |
Output is correct |
32 |
Correct |
2 ms |
512 KB |
Output is correct |
33 |
Correct |
411 ms |
27360 KB |
Output is correct |
34 |
Correct |
620 ms |
42360 KB |
Output is correct |
35 |
Correct |
827 ms |
41992 KB |
Output is correct |
36 |
Correct |
951 ms |
41472 KB |
Output is correct |
37 |
Correct |
668 ms |
48088 KB |
Output is correct |
38 |
Correct |
805 ms |
47172 KB |
Output is correct |
39 |
Correct |
691 ms |
47940 KB |
Output is correct |
40 |
Correct |
792 ms |
47160 KB |
Output is correct |
41 |
Correct |
671 ms |
47864 KB |
Output is correct |
42 |
Correct |
799 ms |
47160 KB |
Output is correct |
43 |
Correct |
275 ms |
20716 KB |
Output is correct |
44 |
Correct |
291 ms |
20716 KB |
Output is correct |
45 |
Correct |
338 ms |
18780 KB |
Output is correct |
46 |
Correct |
358 ms |
19292 KB |
Output is correct |
47 |
Correct |
417 ms |
22616 KB |
Output is correct |
48 |
Correct |
723 ms |
35688 KB |
Output is correct |
49 |
Correct |
191 ms |
19156 KB |
Output is correct |
50 |
Correct |
181 ms |
18008 KB |
Output is correct |
51 |
Correct |
184 ms |
18648 KB |
Output is correct |
52 |
Correct |
190 ms |
18468 KB |
Output is correct |
53 |
Correct |
189 ms |
18392 KB |
Output is correct |
54 |
Correct |
190 ms |
18392 KB |
Output is correct |