This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* 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 |
---|
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... |