Submission #257464

# Submission time Handle Problem Language Result Execution time Memory
257464 2020-08-04T09:49:27 Z AM1648 Street Lamps (APIO19_street_lamps) C++17
100 / 100
1909 ms 185960 KB
/* 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
1 Correct 20 ms 33280 KB Output is correct
2 Correct 23 ms 33280 KB Output is correct
3 Correct 21 ms 33280 KB Output is correct
4 Correct 22 ms 33232 KB Output is correct
5 Correct 19 ms 33280 KB Output is correct
6 Correct 21 ms 33280 KB Output is correct
7 Correct 22 ms 33280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 203 ms 49400 KB Output is correct
2 Correct 211 ms 49656 KB Output is correct
3 Correct 284 ms 51192 KB Output is correct
4 Correct 1002 ms 95372 KB Output is correct
5 Correct 979 ms 105228 KB Output is correct
6 Correct 998 ms 95116 KB Output is correct
7 Correct 530 ms 80268 KB Output is correct
8 Correct 1607 ms 185960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 33408 KB Output is correct
2 Correct 21 ms 33460 KB Output is correct
3 Correct 25 ms 33408 KB Output is correct
4 Correct 24 ms 33536 KB Output is correct
5 Correct 1320 ms 103156 KB Output is correct
6 Correct 1285 ms 103308 KB Output is correct
7 Correct 1012 ms 104460 KB Output is correct
8 Correct 1861 ms 185856 KB Output is correct
9 Correct 109 ms 42616 KB Output is correct
10 Correct 129 ms 43512 KB Output is correct
11 Correct 117 ms 43640 KB Output is correct
12 Correct 576 ms 80268 KB Output is correct
13 Correct 1870 ms 185804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 33408 KB Output is correct
2 Correct 22 ms 33408 KB Output is correct
3 Correct 22 ms 33400 KB Output is correct
4 Correct 28 ms 33400 KB Output is correct
5 Correct 707 ms 90252 KB Output is correct
6 Correct 812 ms 92300 KB Output is correct
7 Correct 1093 ms 94600 KB Output is correct
8 Correct 1240 ms 97712 KB Output is correct
9 Correct 224 ms 50296 KB Output is correct
10 Correct 215 ms 50424 KB Output is correct
11 Correct 217 ms 50424 KB Output is correct
12 Correct 218 ms 50424 KB Output is correct
13 Correct 220 ms 50424 KB Output is correct
14 Correct 214 ms 50168 KB Output is correct
15 Correct 512 ms 80268 KB Output is correct
16 Correct 1909 ms 185868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 33280 KB Output is correct
2 Correct 23 ms 33280 KB Output is correct
3 Correct 21 ms 33280 KB Output is correct
4 Correct 22 ms 33232 KB Output is correct
5 Correct 19 ms 33280 KB Output is correct
6 Correct 21 ms 33280 KB Output is correct
7 Correct 22 ms 33280 KB Output is correct
8 Correct 203 ms 49400 KB Output is correct
9 Correct 211 ms 49656 KB Output is correct
10 Correct 284 ms 51192 KB Output is correct
11 Correct 1002 ms 95372 KB Output is correct
12 Correct 979 ms 105228 KB Output is correct
13 Correct 998 ms 95116 KB Output is correct
14 Correct 530 ms 80268 KB Output is correct
15 Correct 1607 ms 185960 KB Output is correct
16 Correct 26 ms 33408 KB Output is correct
17 Correct 21 ms 33460 KB Output is correct
18 Correct 25 ms 33408 KB Output is correct
19 Correct 24 ms 33536 KB Output is correct
20 Correct 1320 ms 103156 KB Output is correct
21 Correct 1285 ms 103308 KB Output is correct
22 Correct 1012 ms 104460 KB Output is correct
23 Correct 1861 ms 185856 KB Output is correct
24 Correct 109 ms 42616 KB Output is correct
25 Correct 129 ms 43512 KB Output is correct
26 Correct 117 ms 43640 KB Output is correct
27 Correct 576 ms 80268 KB Output is correct
28 Correct 1870 ms 185804 KB Output is correct
29 Correct 25 ms 33408 KB Output is correct
30 Correct 22 ms 33408 KB Output is correct
31 Correct 22 ms 33400 KB Output is correct
32 Correct 28 ms 33400 KB Output is correct
33 Correct 707 ms 90252 KB Output is correct
34 Correct 812 ms 92300 KB Output is correct
35 Correct 1093 ms 94600 KB Output is correct
36 Correct 1240 ms 97712 KB Output is correct
37 Correct 224 ms 50296 KB Output is correct
38 Correct 215 ms 50424 KB Output is correct
39 Correct 217 ms 50424 KB Output is correct
40 Correct 218 ms 50424 KB Output is correct
41 Correct 220 ms 50424 KB Output is correct
42 Correct 214 ms 50168 KB Output is correct
43 Correct 512 ms 80268 KB Output is correct
44 Correct 1909 ms 185868 KB Output is correct
45 Correct 117 ms 42940 KB Output is correct
46 Correct 141 ms 43376 KB Output is correct
47 Correct 457 ms 59640 KB Output is correct
48 Correct 964 ms 94988 KB Output is correct
49 Correct 121 ms 44408 KB Output is correct
50 Correct 125 ms 44408 KB Output is correct
51 Correct 120 ms 44516 KB Output is correct
52 Correct 122 ms 44920 KB Output is correct
53 Correct 122 ms 44920 KB Output is correct
54 Correct 121 ms 44920 KB Output is correct