/*
check long long - int
check N
*/
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
const int N = (int)3e5 + 5, Q = N;
int n, q;
string initSate;
struct Query {
int t, a, b;
Query (int _t = 0, int _a = 0, int _b = 0) : t(_t), a(_a), b(_b) {}
} query[Q];
struct Bit {
static const int LOG = __lg(N) + 1;
int a[1 << LOG];
void init () { memset(a, 0, sizeof a); }
void update (int pos, int val) {
for (; pos < (1 << LOG); pos += pos & -pos) a[pos] += val;
}
int get (int pos) {
int ret = 0;
for (; pos > 0; pos -= pos & -pos) ret += a[pos];
return ret;
}
int findPos (int val) {
if (!val) return 0;
int ret = 0;
for (int i = LOG - 1; i >= 0; --i) if (a[ret | (1 << i)] < val) ret |= (1 << i), val -= a[ret];
return min(ret, n) + 1;
}
} bit;
template <class T>
struct Bit2D {
vector<int> cmprssNmbr[N];
vector<T> a[N];
void init () {
for (int i = 0; i < N; ++i) cmprssNmbr[i].assign(1, -1);
}
void add (int x, int y) {
for (; x < N; x += x & -x) {
cmprssNmbr[x].pb(y);
}
}
void interPhase () {
for (int i = 1; i < N; ++i) {
sort(cmprssNmbr[i].begin(), cmprssNmbr[i].end() );
cmprssNmbr[i].erase(unique(cmprssNmbr[i].begin(), cmprssNmbr[i].end() ), cmprssNmbr[i].end() );
a[i].assign(cmprssNmbr[i].size(), 0);
}
}
void update (int x, int y, int val) {
for (; x < N; x += x & -x) {
int _y = (int)(lower_bound(cmprssNmbr[x].begin(), cmprssNmbr[x].end(), y) - cmprssNmbr[x].begin() );
for (; _y < (int)a[x].size(); _y += _y & -_y) a[x][_y] += val;
}
}
T get (int x, int y) {
T ret = 0;
for (; x > 0; x -= x & -x) {
int _y = (int)(upper_bound(cmprssNmbr[x].begin(), cmprssNmbr[x].end(), y) - cmprssNmbr[x].begin() - 1);
for (; _y > 0; _y -= _y & -_y) ret += a[x][_y];
}
return ret;
}
void update (int x1, int y1, int x2, int y2, int val) {
update(x1, y1, val);
update(x2 + 1, y2 + 1, val);
update(x1, y2 + 1, -val);
update(x2 + 1, y1, -val);
}
};
Bit2D<long long> bit2D_0;
Bit2D<int> bit2D_1;
int32_t main () {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
freopen("test.INP", "r", stdin);
bit2D_0.init();
bit2D_1.init();
cin >> n >> q >> initSate; initSate = '!' + initSate;
for (int i = 1; i <= q; ++i) {
string str; cin >> str;
if (str == "toggle") {
int a; cin >> a;
query[i] = Query(1, a, 0);
}
else {
int a, b; cin >> a >> b; --b;
query[i] = Query(2, a, b);
}
}
string curState = initSate;
for (int i = 1; i <= n; ++i) if (curState[i] == '0') bit.update(i, 1);
for (int i = 0; i <= n; ++i) if (curState[i] != '1') {
int j = bit.findPos(bit.get(i) + 1);
if (i + 1 <= j - 1) {
bit2D_0.add(i + 1, i + 1);
bit2D_0.add(j, j);
bit2D_0.add(i + 1, j);
bit2D_0.add(j, i + 1);
bit2D_1.add(i + 1, i + 1);
bit2D_1.add(j, j);
bit2D_1.add(i + 1, j);
bit2D_1.add(j, i + 1);
}
}
for (int i = 1; i <= q; ++i) if (query[i].t == 1) {
if (curState[ query[i].a ] == '1') {
curState[ query[i].a ] = '0';
int l = bit.findPos(bit.get(query[i].a) ) + 1,
r = bit.findPos(bit.get(query[i].a) + 1) - 1;
bit2D_0.add(l, query[i].a);
bit2D_0.add(query[i].a + 1, r + 1);
bit2D_0.add(l, r + 1);
bit2D_0.add(query[i].a + 1, query[i].a);
bit2D_1.add(l, query[i].a);
bit2D_1.add(query[i].a + 1, r + 1);
bit2D_1.add(l, r + 1);
bit2D_1.add(query[i].a + 1, query[i].a);
bit.update(query[i].a, 1);
}
else {
curState[ query[i].a ] = '1';
int l = bit.findPos(bit.get(query[i].a) - 1 ) + 1,
r = bit.findPos(bit.get(query[i].a) + 1) - 1;
bit2D_0.add(l, query[i].a);
bit2D_0.add(query[i].a + 1, r + 1);
bit2D_0.add(l, r + 1);
bit2D_0.add(query[i].a + 1, query[i].a);
bit2D_1.add(l, query[i].a);
bit2D_1.add(query[i].a + 1, r + 1);
bit2D_1.add(l, r + 1);
bit2D_1.add(query[i].a + 1, query[i].a);
bit.update(query[i].a, -1);
}
}
bit.init();
bit2D_0.interPhase();
bit2D_1.interPhase();
curState = initSate;
for (int i = 1; i <= n; ++i) if (curState[i] == '0') bit.update(i, 1);
for (int i = 0; i <= n; ++i) if (curState[i] != '1') {
int j = bit.findPos(bit.get(i) + 1);
if (i + 1 <= j - 1) {
bit2D_1.update(i + 1, i + 1, j - 1, j - 1, 1);
}
}
for (int i = 1; i <= q; ++i) {
if (query[i].t == 1) {
if (curState[ query[i].a ] == '1') {
curState[ query[i].a ] = '0';
int l = bit.findPos(bit.get(query[i].a) ) + 1,
r = bit.findPos(bit.get(query[i].a) + 1) - 1;
bit2D_0.update(l, query[i].a, query[i].a, r, i);
bit2D_1.update(l, query[i].a, query[i].a, r, -1);
bit.update(query[i].a, 1);
}
else {
curState[ query[i].a ] = '1';
int l = bit.findPos(bit.get(query[i].a) - 1 ) + 1,
r = bit.findPos(bit.get(query[i].a) + 1) - 1;
bit2D_0.update(l, query[i].a, query[i].a, r, -i);
bit2D_1.update(l, query[i].a, query[i].a, r, 1);
bit.update(query[i].a, -1);
}
}
else {
cout << bit2D_0.get(query[i].a, query[i].b) + (bit2D_1.get(query[i].a, query[i].b) ? i : 0) << '\n';
}
}
return 0;
}
Compilation message
street_lamps.cpp: In function 'int32_t main()':
street_lamps.cpp:86:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("test.INP", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
119 ms |
71672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
120 ms |
71732 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
136 ms |
71700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
134 ms |
71684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
119 ms |
71672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |