답안 #126061

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
126061 2019-07-06T23:24:57 Z tri 가로등 (APIO19_street_lamps) C++14
100 / 100
1704 ms 60596 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define pb push_back
#define f first
#define s second

namespace debug {
    const int DEBUG = false;

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x);

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x);

    template<class T>
    void pr(const vector<T> &x);

    template<class T>
    void pr(const set<T> &x);

    template<class T1, class T2>
    void pr(const map<T1, T2> &x);

    template<class T>
    void pr(const T &x) { if (DEBUG) cout << x; }

    template<class T, class... Ts>
    void pr(const T &first, const Ts &... rest) { pr(first), pr(rest...); }

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x) { pr("{", x.f, ", ", x.s, "}"); }

    template<class T>
    void prIn(const T &x) {
        pr("{");
        bool fst = 1;
        for (auto &a : x) {
            pr(fst ? "" : ", ", a), fst = 0;
        }
        pr("}");
    }

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x) { prIn(x); }

    template<class T>
    void pr(const vector<T> &x) { prIn(x); }

    template<class T>
    void pr(const set<T> &x) { prIn(x); }

    template<class T1, class T2>
    void pr(const map<T1, T2> &x) { prIn(x); }

    void ps() { pr("\n"); }

    template<class Arg, class... Args>
    void ps(const Arg &first, const Args &... rest) {
        pr(first, " ");
        ps(rest...);
    }
}
using namespace debug;


const int INF = 100000000;

static int searchF(vi &a, int x) { // returns first element ge x
    return lower_bound(a.begin(), a.end(), x) - a.begin();
}

static int searchL(vi &a, int x) { // returns last element le x
    return upper_bound(a.begin(), a.end(), x) - a.begin() - 1;
}

struct BIT2D { // 2D BIT with 0 indexing; range updates, points queries

    struct node {
        vi yCoor;
        vi tr;

        void in_u(int i, int d) {
            while (i < tr.size()) {
                tr[i] += d;
                i = i | (i + 1);
            }
        }

        int in_q(int i) {
//            ps("in_q:", i);
            int s = 0;
            while (i >= 0) {
                s += tr[i];
                i = (i & (i + 1)) - 1;
            }
            return s;
        }

        void rU(int l, int r, int d) {
            l = searchF(yCoor, l);
            r = searchL(yCoor, r);
            if (l > r) return;
            in_u(l, d);
            in_u(r + 1, -d);
        }

        int q(int i) {
//            ps("inside q:", i);
//            ps(yCoor);
//            ps("searchL:", searchL(yCoor, i));
            return in_q(searchL(yCoor, i));
        }
    };

    vector<node> tr;

    void init(vector<pi> &pts) {
        int maxX = 0;
        for (pi cP: pts) {
            maxX = max(maxX, cP.f);
        }
        tr.resize(maxX + 1);

        // coordinate compress on x if necessary in future
        for (pi cP: pts) {
            int i = cP.f;
            while (i >= 0) {
                tr[i].yCoor.pb(cP.s);
                i = (i & (i + 1)) - 1;
            }
        }

        for (node &cN : tr) {
            sort(cN.yCoor.begin(), cN.yCoor.end());
            cN.yCoor.erase(unique(cN.yCoor.begin(), cN.yCoor.end()), cN.yCoor.end());
            cN.tr.resize(cN.yCoor.size(), 0);
        }
    }

    void in_rU(int i, int l, int r, int d) {
        while (i < tr.size()) {
//            ps("in_rU i:", i);
            tr[i].rU(l, r, d);
            i = i | (i + 1);
//            ps("in_rU post i:", i);
        }
    }

    int q(int i1, int i2) {
//        ps("q:", i1, i2);
        int s = 0;
        while (i1 >= 0) {
//            ps("i1:", i1);
            s += tr[i1].q(i2);
//            ps("qRes:", tr[i1].q(i2));
            i1 = (i1 & (i1 + 1)) - 1;
//            ps("post i1:", i1);
        }
        return s;
    }

    void rU(int l1, int r1, int l2, int r2, int d) {
        if (l1 > r1) return;
        in_rU(l1, l2, r2, d);
        in_rU(r1 + 1, l2, r2, -d);
    }
} bit2D;


int main() {
    int N, Q;
    cin >> N >> Q;
    string line;
    cin >> line;

    vector<bool> state(N);
    for (int i = 0; i < N; i++) {
        state[i] = line[i] == '1';
    }

    vector<pi> qs;
    vector<pi> pts;

    for (int i = 0; i < Q; i++) {
        string t;
        cin >> t;
        if (t == "toggle") {
            int x;
            cin >> x;
            x--;
            qs.pb({-1, x});
        } else {
            int l, r;
            cin >> l >> r;
            l--, r -= 2;
            qs.pb({l, r});
            pts.pb({l, r});
        }
    }

//    ps(pts);

    bit2D.init(pts);

//    ps(bit2D.tr[0].yCoor);
//    ps(bit2D.tr[1].yCoor);
//    ps(bit2D.tr[2].yCoor);
//
//    ps("f2");
//
//    bit2D.tr[1].rU(2, 4, -5);
//    ps(bit2D.tr[1].q(3));
//
//    ps("f3");
//
//    ps(bit2D.q(2, 3));
//    ps("prelim done");
//    bit2D.rU(1, 2, 2, 4, -5);
//    // bit2D.rU(0, 2, 2, 4, -5);
//    ps("passed in: (2, 3)");
//    ps(bit2D.q(2, 3));


    set<pi> segs;
    for (int i = 0; i < N;) {
        while (!state[i]) {
            i++;
            continue;
        }

        int j = i;
        while (j + 1 < N && state[j + 1]) {
            j++;
        }
        segs.insert({i, j});
        i = j + 1;
    }

//    ps("queries begin");
//    ps(segs);

    for (int cT = 1; cT <= Q; cT++) {
        pi cQ = qs[cT - 1];

        auto aftIt = segs.upper_bound({cQ.s, INF});
        if (cQ.f == -1) { // update
            int x = cQ.s;

            if (state[x]) {
                pi cSeg = *(--aftIt);

                segs.erase(cSeg);
                if (cSeg.f < x) {
                    segs.insert({cSeg.f, x - 1});
                }
                if (x < cSeg.s) {
                    segs.insert({x + 1, cSeg.s});
                }

                bit2D.rU(cSeg.f, x, x, cSeg.s, cT);
            } else {
                int l = x, r = x;

                if (aftIt != segs.end()) {
                    pi aft = *aftIt;
//                    ps("aft: ", aft);
                    if (x + 1 == aft.f) {
                        segs.erase(aft);
                        r = aft.s;
                        aftIt = segs.upper_bound({x, INF});
                    }
                }

                if (aftIt != segs.begin()) {
                    pi bef = *(--aftIt);

//                    ps("bef:", bef);
                    if (bef.s == x - 1) {
                        segs.erase(bef);
                        l = bef.f;
                    }
                }

                segs.insert({l, r});

//                ps("l x r", l, x, r);
//                ps(bit2D.q(2, 3));
                bit2D.rU(l, x, x, r, -cT);
//                ps(bit2D.q(2, 3));
            }
            state[x] = !state[x];
        } else {
//            ps("p query");
//            ps(cQ.f, cQ.s);
            int ans = bit2D.q(cQ.f, cQ.s);
//            ps("obt ans", ans);

            if (aftIt != segs.begin()) {
                pi around = *(--aftIt);
//                ps("around:", around);
//                ps(cQ.f, cQ.s);
//                ps(around.f <= cQ.f);
//                ps(cQ.s <= around.s);
                if (around.f <= cQ.f && cQ.s <= around.s) { // interval is still active
                    ans += cT;
                }
            }
            cout << ans << '\n';
        }
//        ps("finished:", cT);
//        for (int i = 0; i < N; i++) {
//            pr(state[i]);
//        }
//        ps();
//        ps("relevant:", bit2D.q(0, 2));
//        ps(segs);
    }
    cout << flush;
}

Compilation message

street_lamps.cpp: In member function 'void BIT2D::node::in_u(int, int)':
street_lamps.cpp:94:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while (i < tr.size()) {
                    ~~^~~~~~~~~~~
street_lamps.cpp: In member function 'void BIT2D::in_rU(int, int, int, int)':
street_lamps.cpp:152:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while (i < tr.size()) {
                ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 442 ms 10460 KB Output is correct
2 Correct 501 ms 11044 KB Output is correct
3 Correct 679 ms 13628 KB Output is correct
4 Correct 1410 ms 46980 KB Output is correct
5 Correct 1504 ms 50668 KB Output is correct
6 Correct 1266 ms 43264 KB Output is correct
7 Correct 1527 ms 56988 KB Output is correct
8 Correct 1468 ms 58480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 4 ms 376 KB Output is correct
3 Correct 4 ms 504 KB Output is correct
4 Correct 4 ms 504 KB Output is correct
5 Correct 839 ms 25876 KB Output is correct
6 Correct 1201 ms 39300 KB Output is correct
7 Correct 1414 ms 49144 KB Output is correct
8 Correct 1462 ms 59024 KB Output is correct
9 Correct 373 ms 11652 KB Output is correct
10 Correct 410 ms 12532 KB Output is correct
11 Correct 450 ms 12976 KB Output is correct
12 Correct 1448 ms 57392 KB Output is correct
13 Correct 1483 ms 58760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 504 KB Output is correct
2 Correct 5 ms 504 KB Output is correct
3 Correct 4 ms 376 KB Output is correct
4 Correct 4 ms 376 KB Output is correct
5 Correct 1704 ms 60596 KB Output is correct
6 Correct 1468 ms 52496 KB Output is correct
7 Correct 1262 ms 42756 KB Output is correct
8 Correct 857 ms 25728 KB Output is correct
9 Correct 519 ms 8924 KB Output is correct
10 Correct 418 ms 7268 KB Output is correct
11 Correct 516 ms 8796 KB Output is correct
12 Correct 415 ms 7240 KB Output is correct
13 Correct 524 ms 8924 KB Output is correct
14 Correct 413 ms 7268 KB Output is correct
15 Correct 1493 ms 57504 KB Output is correct
16 Correct 1440 ms 58868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 252 KB Output is correct
8 Correct 442 ms 10460 KB Output is correct
9 Correct 501 ms 11044 KB Output is correct
10 Correct 679 ms 13628 KB Output is correct
11 Correct 1410 ms 46980 KB Output is correct
12 Correct 1504 ms 50668 KB Output is correct
13 Correct 1266 ms 43264 KB Output is correct
14 Correct 1527 ms 56988 KB Output is correct
15 Correct 1468 ms 58480 KB Output is correct
16 Correct 3 ms 376 KB Output is correct
17 Correct 4 ms 376 KB Output is correct
18 Correct 4 ms 504 KB Output is correct
19 Correct 4 ms 504 KB Output is correct
20 Correct 839 ms 25876 KB Output is correct
21 Correct 1201 ms 39300 KB Output is correct
22 Correct 1414 ms 49144 KB Output is correct
23 Correct 1462 ms 59024 KB Output is correct
24 Correct 373 ms 11652 KB Output is correct
25 Correct 410 ms 12532 KB Output is correct
26 Correct 450 ms 12976 KB Output is correct
27 Correct 1448 ms 57392 KB Output is correct
28 Correct 1483 ms 58760 KB Output is correct
29 Correct 4 ms 504 KB Output is correct
30 Correct 5 ms 504 KB Output is correct
31 Correct 4 ms 376 KB Output is correct
32 Correct 4 ms 376 KB Output is correct
33 Correct 1704 ms 60596 KB Output is correct
34 Correct 1468 ms 52496 KB Output is correct
35 Correct 1262 ms 42756 KB Output is correct
36 Correct 857 ms 25728 KB Output is correct
37 Correct 519 ms 8924 KB Output is correct
38 Correct 418 ms 7268 KB Output is correct
39 Correct 516 ms 8796 KB Output is correct
40 Correct 415 ms 7240 KB Output is correct
41 Correct 524 ms 8924 KB Output is correct
42 Correct 413 ms 7268 KB Output is correct
43 Correct 1493 ms 57504 KB Output is correct
44 Correct 1440 ms 58868 KB Output is correct
45 Correct 240 ms 5964 KB Output is correct
46 Correct 313 ms 6712 KB Output is correct
47 Correct 730 ms 21988 KB Output is correct
48 Correct 1348 ms 45848 KB Output is correct
49 Correct 456 ms 13804 KB Output is correct
50 Correct 452 ms 13300 KB Output is correct
51 Correct 475 ms 14148 KB Output is correct
52 Correct 545 ms 15952 KB Output is correct
53 Correct 538 ms 15936 KB Output is correct
54 Correct 542 ms 15900 KB Output is correct