This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define pb emplace_back
#define ld double
#define ll __int128
mt19937 rnd(51);
const int N = 3e5 + 10;
vector<int> arr[N];
struct Fenwick {
vector<int> t;
void build(int n) {
t.resize(n);
}
void inc(int i, int d) {
for (; i < t.size(); i = (i | (i + 1))) {
t[i] += d;
}
}
int get(int r) {
int ans = 0;
for (; r >= 0; r = (r & (r + 1)) - 1) {
ans += t[r];
}
return ans;
}
} t[N];
void fake_add(int i, int j) {
for (; i < N; i = (i | (i + 1))) {
arr[i].pb(j);
}
}
void add(int i, int j, int val) {
for (; i < N; i = (i | (i + 1))) {
int ind = lower_bound(arr[i].begin(), arr[i].end(), j) - arr[i].begin();
t[i].inc(ind, val);
}
}
int get(int i, int j) {
int ans = 0;
for (; i >= 0; i = (i & (i + 1)) - 1) {
int ind = upper_bound(arr[i].begin(), arr[i].end(), j) - arr[i].begin() - 1;
ans += t[i].get(ind);
}
return ans;
}
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> s(n);
vector<vector<int>> sum(n, vector<int>(n));
set<int> st{-1, n};
for (int i = 0; i < n; i++) {
char c;
cin >> c;
s[i] = c - '0';
if (s[i] == 0) {
st.insert(i);
}
}
vector<vector<int>> u;
for (int i = 0; i < q; i++) {
string t;
cin >> t;
if (t == "toggle") {
int j;
cin >> j;
j--;
if (st.find(j) == st.end()) {
auto l = *prev(st.lower_bound(j)) + 1, r = *st.upper_bound(j) - 1;
u.push_back({l, r, i + 1, j});
fake_add(l, j);
fake_add(l, r + 1);
fake_add(j + 1, j);
fake_add(j + 1, r + 1);
st.insert(j);
} else {
st.erase(j);
auto l = *prev(st.lower_bound(j)) + 1, r = *st.upper_bound(j) - 1;
u.push_back({l, r, -(i + 1), j});
fake_add(l, j);
fake_add(l, r + 1);
fake_add(j + 1, j);
fake_add(j + 1, r + 1);
}
} else {
int l, r;
cin >> l >> r;
l--, r -= 2;
auto val = *st.lower_bound(l);
if (val > r) {
u.push_back({l, r, 1});
} else {
u.push_back({l, r, 0});
}
}
}
for (int i = 0; i < N; i++) {
sort(arr[i].begin(), arr[i].end());
arr[i].erase(unique(arr[i].begin(), arr[i].end()), arr[i].end());
t[i].build(arr[i].size());
}
for (int i = 0; i < u.size(); i++) {
if (u[i].size() == 3) {
int l = u[i][0], r = u[i][1];
cout << get(l, r) + (i + 1) * u[i][2] << endl;
} else {
int l = u[i][0], r = u[i][1], val = u[i][2], j = u[i][3];
add(l, j, val);
add(l, r + 1, -val);
add(j + 1, j, -val);
add(j + 1, r + 1, val);
}
}
return 0;
}
Compilation message (stderr)
street_lamps.cpp: In member function 'void Fenwick::inc(int, int)':
street_lamps.cpp:20:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for (; i < t.size(); i = (i | (i + 1))) {
| ~~^~~~~~~~~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:116:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
116 | for (int i = 0; i < u.size(); i++) {
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |