Submission #230616

# Submission time Handle Problem Language Result Execution time Memory
230616 2020-05-10T15:09:00 Z lyc Street Lamps (APIO19_street_lamps) C++14
100 / 100
1633 ms 75912 KB
#include <bits/stdc++.h>
using namespace std;

#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)
using ll=long long;

const int MX_N = 3e5+5;
const int MX_Q = 3e5+5;
int N, Q;
string S;
vector<array<int,2>> change;
vector<array<int,3>> query;
vector<array<int,4>> itv;
set<array<int,2>> cur;
int ans[MX_Q];

struct FenwickTree {
    vector<ll> ft; int n;
    void init(int _n) { n = _n; ft.assign(n+1,0); }
    void update(int i, int x) { for (; i <= n; i += i&-i) ft[i] += x; }
    ll query(int i) { ll s = 0; for (; i; i -= i&-i) s += ft[i]; return s; }
    ll query(int i, int j) { return query(j)-query(i-1); }
} ft, ft2;

void cdq(int l, int r, vector<array<int,4>>& a, vector<array<int,3>>& b) {
    if (l > r) return;
    if (l == r) {
        for (auto& x : a) {
            ft.update(x[1], (x[3]?1:-1)*x[0]);
            ft2.update(x[1], (x[3]?-1:1));
        }
        for (auto& y : b) {
            ans[y[0]] += ft.query(y[1]);
            ans[y[0]] += ft2.query(y[1]) * y[0];
        }
        for (auto& x : a) {
            ft.update(x[1], -(x[3]?1:-1)*x[0]);
            ft2.update(x[1], -(x[3]?-1:1));
        }
        return;
    }

    int m = (l+r) >> 1;
    vector<array<int,4>> a1, a2;
    for (auto& x : a) (x[0] <= m ? a1 : a2).push_back(x);
    vector<array<int,3>> b1, b2;
    for (auto& y : b) (y[0] <= m ? b1 : b2).push_back(y);
    cdq(l,m,a1,b1);
    cdq(m+1,r,a2,b2);

    sort(ALL(a1), [](array<int,4> p, array<int,4> q){
            return (p[2]>q[2]||(p[2]==q[2]&&(p[1]>q[1]||(p[1]==q[1]&&p[0]>q[0]))));
            });
    sort(ALL(b2), [](array<int,3> p, array<int,3> q){
            return (p[2]>q[2]||(p[2]==q[2]&&(p[1]>q[1]||(p[1]==q[1]&&p[0]>q[0]))));
            });

    //TRACE(l _ r);
    //for (auto& x : a) TRACE(x[0] _ x[1] _ x[2] _ x[3]);
    //for (auto& y : b) TRACE(y[0] _ y[1] _ y[2]);
    //cout << endl;

    int i, j;
    for (i = 0, j = 0; i < SZ(a1) || j < SZ(b2); ) {
        if (i < SZ(a1) && (j >= SZ(b2) || a1[i][2] >= b2[j][2])) {
            ft.update(a1[i][1], (a1[i][3]?1:-1)*a1[i][0]);
            ft2.update(a1[i][1], (a1[i][3]?-1:1));
            ++i;
        } else {
            ans[b2[j][0]] += ft.query(b2[j][1]);
            ans[b2[j][0]] += ft2.query(b2[j][1]) * b2[j][0];
            ++j;
        }
    }
    for (;j < SZ(b2);) {
        ans[b2[j][0]] += ft.query(b2[j][1]);
        ans[b2[j][0]] += ft2.query(b2[j][1]) * b2[j][0];
        ++j;
    }
    FOR(k,0,i-1){
        ft.update(a1[k][1], -(a1[k][3]?1:-1)*a1[k][0]);
        ft2.update(a1[k][1], -(a1[k][3]?-1:1));
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> N >> Q >> S; S = '^' + S + '0';
    FOR(i,1,Q){
        string T; cin >> T;
        if (T == "toggle") {
            int I; cin >> I;
            change.push_back({i,I});
        } else {
            int A, B; cin >> A >> B;
            --B;
            query.push_back({i,A,B});
        }
    }

    int len = 0;
    FOR(i,1,N+1){
        if (S[i] == '0') {
            if (len) {
                cur.insert({i-len,i-1});
                itv.push_back({0,i-len,i-1,0});
            }
            len = 0;
        } else ++len;
    }

    for (auto& x : change) {
        //cout << x[1] << ' ' << x[1] << " vs ";
        //for (auto& y : cur) cout << y[0] << ' ' << y[1] << " | ";
        //cout << endl;

        if (S[x[1]] == '0') {
            auto it = cur.lower_bound({x[1]+1,0});
            array<int,2> y = {x[1],x[1]};
            if (it != cur.end() && y[1]+1 == (*it)[0]) {
                auto z = *it; cur.erase(it);
                itv.push_back({x[0],z[0],z[1],1});
                y[1] = z[1];
                it = cur.upper_bound({x[1],0});
            }
            if (it != cur.begin() && (*(--it))[1]+1 == y[0]) {
                auto z = *it; cur.erase(it);
                itv.push_back({x[0],z[0],z[1],1});
                y[0] = z[0];
            }
            cur.insert(y);
            itv.push_back({x[0],y[0],y[1],0});
            S[x[1]] = '1';
        } else {
            auto it = cur.lower_bound({x[1]+1,0});
            assert(it != cur.begin());
            auto y = *(--it); cur.erase(it);
            assert(y[1] >= x[1]);
            itv.push_back({x[0],y[0],y[1],1});
            if (y[0] < x[1]) {
                cur.insert({y[0],x[1]-1});
                itv.push_back({x[0],y[0],x[1]-1,0});
            }
            if (x[1] < y[1]) {
                cur.insert({x[1]+1,y[1]});
                itv.push_back({x[0],x[1]+1,y[1],0});
            }
            S[x[1]] = '0';
        }
    }

    for (auto& x : cur) {
        itv.push_back({Q,x[0],x[1],1});
    }

    //for (auto& x : itv) { TRACE(x[0] _ x[1] _ x[2] _ x[3]); }

    memset(ans,0,sizeof ans);
    ft.init(N);
    ft2.init(N);
    cdq(0,Q,itv,query);

    for (auto& q : query) {
        cout << ans[q[0]] << '\n';
    }
}

# Verdict Execution time Memory Grader output
1 Correct 5 ms 1536 KB Output is correct
2 Correct 5 ms 1536 KB Output is correct
3 Correct 5 ms 1536 KB Output is correct
4 Correct 5 ms 1536 KB Output is correct
5 Correct 5 ms 1536 KB Output is correct
6 Correct 5 ms 1536 KB Output is correct
7 Correct 5 ms 1536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 726 ms 24516 KB Output is correct
2 Correct 751 ms 27988 KB Output is correct
3 Correct 819 ms 28900 KB Output is correct
4 Correct 1242 ms 67996 KB Output is correct
5 Correct 1235 ms 62488 KB Output is correct
6 Correct 1348 ms 72612 KB Output is correct
7 Correct 487 ms 24364 KB Output is correct
8 Correct 496 ms 24352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1664 KB Output is correct
2 Correct 7 ms 1664 KB Output is correct
3 Correct 7 ms 1664 KB Output is correct
4 Correct 6 ms 1584 KB Output is correct
5 Correct 1326 ms 41852 KB Output is correct
6 Correct 1449 ms 56272 KB Output is correct
7 Correct 1212 ms 56604 KB Output is correct
8 Correct 501 ms 18476 KB Output is correct
9 Correct 297 ms 10212 KB Output is correct
10 Correct 343 ms 12632 KB Output is correct
11 Correct 326 ms 12388 KB Output is correct
12 Correct 495 ms 18592 KB Output is correct
13 Correct 523 ms 18468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1664 KB Output is correct
2 Correct 7 ms 1664 KB Output is correct
3 Correct 8 ms 1792 KB Output is correct
4 Correct 8 ms 1792 KB Output is correct
5 Correct 828 ms 57388 KB Output is correct
6 Correct 1077 ms 67104 KB Output is correct
7 Correct 1354 ms 72672 KB Output is correct
8 Correct 1633 ms 75912 KB Output is correct
9 Correct 891 ms 34264 KB Output is correct
10 Correct 972 ms 38524 KB Output is correct
11 Correct 888 ms 34268 KB Output is correct
12 Correct 958 ms 38284 KB Output is correct
13 Correct 901 ms 34264 KB Output is correct
14 Correct 957 ms 38400 KB Output is correct
15 Correct 494 ms 24372 KB Output is correct
16 Correct 511 ms 24360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1536 KB Output is correct
2 Correct 5 ms 1536 KB Output is correct
3 Correct 5 ms 1536 KB Output is correct
4 Correct 5 ms 1536 KB Output is correct
5 Correct 5 ms 1536 KB Output is correct
6 Correct 5 ms 1536 KB Output is correct
7 Correct 5 ms 1536 KB Output is correct
8 Correct 726 ms 24516 KB Output is correct
9 Correct 751 ms 27988 KB Output is correct
10 Correct 819 ms 28900 KB Output is correct
11 Correct 1242 ms 67996 KB Output is correct
12 Correct 1235 ms 62488 KB Output is correct
13 Correct 1348 ms 72612 KB Output is correct
14 Correct 487 ms 24364 KB Output is correct
15 Correct 496 ms 24352 KB Output is correct
16 Correct 7 ms 1664 KB Output is correct
17 Correct 7 ms 1664 KB Output is correct
18 Correct 7 ms 1664 KB Output is correct
19 Correct 6 ms 1584 KB Output is correct
20 Correct 1326 ms 41852 KB Output is correct
21 Correct 1449 ms 56272 KB Output is correct
22 Correct 1212 ms 56604 KB Output is correct
23 Correct 501 ms 18476 KB Output is correct
24 Correct 297 ms 10212 KB Output is correct
25 Correct 343 ms 12632 KB Output is correct
26 Correct 326 ms 12388 KB Output is correct
27 Correct 495 ms 18592 KB Output is correct
28 Correct 523 ms 18468 KB Output is correct
29 Correct 7 ms 1664 KB Output is correct
30 Correct 7 ms 1664 KB Output is correct
31 Correct 8 ms 1792 KB Output is correct
32 Correct 8 ms 1792 KB Output is correct
33 Correct 828 ms 57388 KB Output is correct
34 Correct 1077 ms 67104 KB Output is correct
35 Correct 1354 ms 72672 KB Output is correct
36 Correct 1633 ms 75912 KB Output is correct
37 Correct 891 ms 34264 KB Output is correct
38 Correct 972 ms 38524 KB Output is correct
39 Correct 888 ms 34268 KB Output is correct
40 Correct 958 ms 38284 KB Output is correct
41 Correct 901 ms 34264 KB Output is correct
42 Correct 957 ms 38400 KB Output is correct
43 Correct 494 ms 24372 KB Output is correct
44 Correct 511 ms 24360 KB Output is correct
45 Correct 443 ms 17328 KB Output is correct
46 Correct 478 ms 18148 KB Output is correct
47 Correct 665 ms 31292 KB Output is correct
48 Correct 1243 ms 68032 KB Output is correct
49 Correct 377 ms 16992 KB Output is correct
50 Correct 386 ms 17112 KB Output is correct
51 Correct 379 ms 17240 KB Output is correct
52 Correct 374 ms 17588 KB Output is correct
53 Correct 356 ms 17500 KB Output is correct
54 Correct 362 ms 17784 KB Output is correct