Submission #161775

# Submission time Handle Problem Language Result Execution time Memory
161775 2019-11-04T11:45:27 Z Minnakhmetov Street Lamps (APIO19_street_lamps) C++14
100 / 100
2703 ms 210792 KB
#include <bits/stdc++.h>
    
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()
 
using namespace std;

const int N = 3e5 + 5;

struct ST {
    vector<int> vec, t;

    void resize() {
        t.resize(vec.size() * 4);
    }

    void upd(int l, int r, int x, int v, int L, int R) {
        if (r < vec[L] || l > vec[R])
            return;
        if (l <= vec[L] && vec[R] <= r) {
            t[v] += x;
        }
        else {
            int m = (L + R) >> 1;
            upd(l, r, x, v * 2, L, m);
            upd(l, r, x, v * 2 + 1, m + 1, R);
        }
    }

    int que(int x, int v, int L, int R) {
        int ans = t[v];
        // if (L == R) {
        //     cout << "? " << x << " " << vec[L] << " : ";
        //     for (int y : vec)
        //         cout << y << " ";
        //     cout << "\n";
        // }
        if (L != R) {
            int m = (L + R) >> 1;
            if (x <= vec[m])
                ans += que(x, v * 2, L, m);
            else
                ans += que(x, v * 2 + 1, m + 1, R);
        }
        return ans;
        
    }
} t[N * 4];

struct Q {
    int type, x, y;
};

int n, q;
string s;
vector<pair<int, int>> pts;

void build(int v, int L, int R) {
    if (L == R) {
        t[v].vec = {pts[L].second};        
    }
    else {
        int m = (L + R) >> 1;
        build(v * 2, L, m);
        build(v * 2 + 1, m + 1, R);
        set_union(all(t[v * 2].vec), 
            all(t[v * 2 + 1].vec), 
            back_inserter(t[v].vec));
    }
    t[v].resize();
}

void upd(int lx, int rx, int ly, int ry, int add, int v, int L, int R) {
    if (rx < pts[L].first || lx > pts[R].first)
        return;
    if (lx <= pts[L].first && pts[R].first <= rx) {
        t[v].upd(ly, ry, add, 1, 0, t[v].vec.size() - 1);
    }
    else {
        int m = (L + R) >> 1;
        upd(lx, rx, ly, ry, add, v * 2, L, m);
        upd(lx, rx, ly, ry, add, v * 2 + 1, m + 1, R);
    }
}

int que(int x, int y, int v, int L, int R) {
    int ans = t[v].que(y, 1, 0, t[v].vec.size() - 1);
    if (L != R) {
        int m = (L + R) >> 1;
        if (make_pair(x, y) <= pts[m])
            ans += que(x, y, v * 2, L, m);
        else
            ans += que(x, y, v * 2 + 1, m + 1, R);
    }
    return ans;
}

set<pair<int, int>> st;
int curt = 0;

void insSeg(int x, int y) {
    if (x <= y) {
        st.insert({x, y});
        // cout << x << " " << y << " " << curt << "\n";
        upd(x, y, x, y, -curt, 1, 0, pts.size() - 1);
    }
}

void delSeg(int x, int y) {
    st.erase({x, y});
    // cout << x << " " << y << " " << curt << "\n";
    upd(x, y, x, y, curt, 1, 0, pts.size() - 1);
}

void toggle(int x) {
    if (s[x] == '1') {
        auto it = prev(st.lower_bound({x + 1, -1}));
        insSeg(it->first, x - 1);
        insSeg(x + 1, it->second);
        delSeg(it->first, it->second);
        s[x] = '0';
    }
    else {
        auto nt = st.lower_bound({x + 1, -1}),
            pr = (nt == st.begin() ? st.end() : prev(nt));
        int l, r;
        if (nt != st.end() && nt->first == x + 1) {
            r = nt->second;
            delSeg(nt->first, nt->second);
        }
        else {
            r = x;
        }

        if (pr != st.end() && pr->second == x - 1) {
            l = pr->first;
            delSeg(pr->first, pr->second);
        }
        else {
            l = x;
        }

        insSeg(l, r);
        s[x] = '1';
    }
}

int countAnswer(int x, int y) {
    int ans = que(x, y, 1, 0, pts.size() - 1);
    // cout << x << " " << y << " " << ans << "\n";
    auto it = st.lower_bound({x + 1, -1});
    if (it != st.begin() && prev(it)->second >= y)
        ans += curt;
    return ans;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    
    cin >> n >> q >> s;

    vector<Q> qr;

    for (int i = 0; i < q; i++) {
        string t;
        cin >> t;

        if (t[0] == 'q') {
            int x, y;
            cin >> x >> y;
            x--;
            y -= 2;
            qr.push_back({0, x, y});
            pts.push_back({x, y});
        }
        else {
            int x;
            cin >> x;
            x--;
            qr.push_back({1, x});
        }
    }

    sort(all(pts));
    build(1, 0, pts.size() - 1);

    for (int i = 0, j = 0; i < n; i++) {
        if (s[i] == '0') {
            j = i + 1;
        }
        else if (i == n - 1 || s[i + 1] == '0') {
            insSeg(j, i);
        }
    }


    for (Q z : qr) {
        curt++;
        if (z.type == 0) {
            cout << countAnswer(z.x, z.y) << "\n";
        }
        else {
            toggle(z.x);
        }
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 57 ms 56696 KB Output is correct
2 Correct 57 ms 56696 KB Output is correct
3 Correct 58 ms 56696 KB Output is correct
4 Correct 60 ms 56824 KB Output is correct
5 Correct 58 ms 56700 KB Output is correct
6 Correct 65 ms 56872 KB Output is correct
7 Correct 59 ms 56696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 555 ms 84564 KB Output is correct
2 Correct 654 ms 84756 KB Output is correct
3 Correct 956 ms 86128 KB Output is correct
4 Correct 1639 ms 125508 KB Output is correct
5 Correct 1910 ms 138676 KB Output is correct
6 Correct 1364 ms 115376 KB Output is correct
7 Correct 2115 ms 170592 KB Output is correct
8 Correct 2162 ms 171980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 56776 KB Output is correct
2 Correct 58 ms 56824 KB Output is correct
3 Correct 60 ms 56952 KB Output is correct
4 Correct 61 ms 57020 KB Output is correct
5 Correct 503 ms 69540 KB Output is correct
6 Correct 1236 ms 108716 KB Output is correct
7 Correct 1890 ms 151536 KB Output is correct
8 Correct 2429 ms 210772 KB Output is correct
9 Correct 494 ms 96528 KB Output is correct
10 Correct 472 ms 100096 KB Output is correct
11 Correct 544 ms 101124 KB Output is correct
12 Correct 2330 ms 209300 KB Output is correct
13 Correct 2443 ms 210628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 56952 KB Output is correct
2 Correct 61 ms 57028 KB Output is correct
3 Correct 62 ms 56824 KB Output is correct
4 Correct 60 ms 56952 KB Output is correct
5 Correct 2703 ms 209120 KB Output is correct
6 Correct 2106 ms 164204 KB Output is correct
7 Correct 1356 ms 121436 KB Output is correct
8 Correct 574 ms 69492 KB Output is correct
9 Correct 833 ms 87812 KB Output is correct
10 Correct 422 ms 65636 KB Output is correct
11 Correct 720 ms 87896 KB Output is correct
12 Correct 419 ms 65748 KB Output is correct
13 Correct 826 ms 87944 KB Output is correct
14 Correct 430 ms 65760 KB Output is correct
15 Correct 2306 ms 209520 KB Output is correct
16 Correct 2463 ms 210792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 56696 KB Output is correct
2 Correct 57 ms 56696 KB Output is correct
3 Correct 58 ms 56696 KB Output is correct
4 Correct 60 ms 56824 KB Output is correct
5 Correct 58 ms 56700 KB Output is correct
6 Correct 65 ms 56872 KB Output is correct
7 Correct 59 ms 56696 KB Output is correct
8 Correct 555 ms 84564 KB Output is correct
9 Correct 654 ms 84756 KB Output is correct
10 Correct 956 ms 86128 KB Output is correct
11 Correct 1639 ms 125508 KB Output is correct
12 Correct 1910 ms 138676 KB Output is correct
13 Correct 1364 ms 115376 KB Output is correct
14 Correct 2115 ms 170592 KB Output is correct
15 Correct 2162 ms 171980 KB Output is correct
16 Correct 58 ms 56776 KB Output is correct
17 Correct 58 ms 56824 KB Output is correct
18 Correct 60 ms 56952 KB Output is correct
19 Correct 61 ms 57020 KB Output is correct
20 Correct 503 ms 69540 KB Output is correct
21 Correct 1236 ms 108716 KB Output is correct
22 Correct 1890 ms 151536 KB Output is correct
23 Correct 2429 ms 210772 KB Output is correct
24 Correct 494 ms 96528 KB Output is correct
25 Correct 472 ms 100096 KB Output is correct
26 Correct 544 ms 101124 KB Output is correct
27 Correct 2330 ms 209300 KB Output is correct
28 Correct 2443 ms 210628 KB Output is correct
29 Correct 61 ms 56952 KB Output is correct
30 Correct 61 ms 57028 KB Output is correct
31 Correct 62 ms 56824 KB Output is correct
32 Correct 60 ms 56952 KB Output is correct
33 Correct 2703 ms 209120 KB Output is correct
34 Correct 2106 ms 164204 KB Output is correct
35 Correct 1356 ms 121436 KB Output is correct
36 Correct 574 ms 69492 KB Output is correct
37 Correct 833 ms 87812 KB Output is correct
38 Correct 422 ms 65636 KB Output is correct
39 Correct 720 ms 87896 KB Output is correct
40 Correct 419 ms 65748 KB Output is correct
41 Correct 826 ms 87944 KB Output is correct
42 Correct 430 ms 65760 KB Output is correct
43 Correct 2306 ms 209520 KB Output is correct
44 Correct 2463 ms 210792 KB Output is correct
45 Correct 275 ms 74908 KB Output is correct
46 Correct 470 ms 75796 KB Output is correct
47 Correct 964 ms 106232 KB Output is correct
48 Correct 1696 ms 138616 KB Output is correct
49 Correct 832 ms 105604 KB Output is correct
50 Correct 678 ms 105344 KB Output is correct
51 Correct 711 ms 106368 KB Output is correct
52 Correct 811 ms 128496 KB Output is correct
53 Correct 808 ms 128628 KB Output is correct
54 Correct 803 ms 128612 KB Output is correct