Submission #374463

# Submission time Handle Problem Language Result Execution time Memory
374463 2021-03-07T10:06:51 Z KoD Street Lamps (APIO19_street_lamps) C++17
100 / 100
1424 ms 70608 KB
#include <bits/stdc++.h>

template <class T>
using Vec = std::vector<T>;

struct Fenwick {
    Vec<int> data;
    Fenwick() = default;
    Fenwick(const int n): data(n + 1) { }
    void add(int i, const int x) {
        for (i += 1; i < (int) data.size(); i += i & -i) {
            data[i] += x;
        }   
    }
    int get(int i) const {
        int ret = 0;
        for (i += 1; i > 0; i -= i & -i) {
            ret += data[i];
        }
        return ret;
    }
};

struct Add2D {
    Vec<Vec<int>> pts;
    Vec<Fenwick> fen;
    Add2D(const Vec<int> &x, const Vec<int> &y, const int n) {
        pts.resize(n + 1);
        fen.resize(n + 1);
        for (int i = 0; i < (int) x.size(); ++i) {
            for (int k = x[i] + 1; k > 0; k -= k & -k) {
                pts[k].push_back(y[i]);
            }
        }
        for (int i = 1; i <= n; ++i) {
            std::sort(pts[i].begin(), pts[i].end());
            pts[i].erase(std::unique(pts[i].begin(), pts[i].end()), pts[i].end());
            fen[i] = Fenwick(pts[i].size());
        }
    }
    void add(int l, int r, const int d, const int u, const int v) {
        for (l += 1; l < (int) pts.size(); l += l & -l) {
            fen[l].add(std::lower_bound(pts[l].begin(), pts[l].end(), d) - pts[l].begin(), v);
            fen[l].add(std::lower_bound(pts[l].begin(), pts[l].end(), u) - pts[l].begin(), -v);
        }
        for (r += 1; r < (int) pts.size(); r += r & -r) {
            fen[r].add(std::lower_bound(pts[r].begin(), pts[r].end(), d) - pts[r].begin(), -v);
            fen[r].add(std::lower_bound(pts[r].begin(), pts[r].end(), u) - pts[r].begin(), v);
        }
    }
    int get(int x, const int y) const {
        int ret = 0;
        for (x += 1; x > 0; x -= x & -x) {
            ret += fen[x].get(std::lower_bound(pts[x].begin(), pts[x].end(), y) - pts[x].begin());
        }
        return ret;
    }
};  

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int N, Q;
    std::cin >> N >> Q;
    Vec<int> state(N);
    for (auto &x: state) {
        char c;
        std::cin >> c;
        x = (c == '1');
    }
    std::set<int> off;
    for (int i = 0; i < N; ++i) {
        if (!state[i]) {
            off.insert(i);
        }
    }
    Vec<int> ans, X, Y;
    Vec<std::tuple<int, int, int, int, int>> add;
    Vec<std::pair<int, int>> qs;
    qs.reserve(Q);
    for (int t = 1; t <= Q; ++t) {
        std::string type;
        std::cin >> type;
        if (type == "query") {
            const auto idx = (int) ans.size();
            int a, b;
            std::cin >> a >> b;
            a -= 1;
            b -= 1;
            const auto itr = off.lower_bound(a);
            ans.push_back((itr == off.end() || *itr >= b) ? t : 0);
            X.push_back(a);
            Y.push_back(b);
            qs.emplace_back(idx, true);
        }
        else {
            const auto idx = (int) add.size();
            int s;
            std::cin >> s;
            s -= 1;
            if (off.find(s) == off.end()) {
                off.insert(s);
                const auto itr = off.find(s);
                const auto l = (itr == off.begin() ? 0 : *std::prev(itr) + 1);
                const auto r = (std::next(itr) == off.end() ? N : *std::next(itr));
                add.emplace_back(l, s, s + 1, r, t);
            }
            else {
                const auto itr = off.find(s);
                const auto l = (itr == off.begin() ? 0 : *std::prev(itr) + 1);
                const auto r = (std::next(itr) == off.end() ? N : *std::next(itr));
                add.emplace_back(l, s, s + 1, r, -t);
                off.erase(itr);
            }
            qs.emplace_back(idx, false);
        }
    }
    Add2D rect(X, Y, N + 1);
    for (const auto [i, f]: qs) {
        if (f) {
            ans[i] += rect.get(X[i], Y[i]);
        }
        else {
            const auto [l, r, d, u, v] = add[i];
            rect.add(l, r + 1, d, u + 1, v);
        }
    }
    for (const auto x: ans) {
        std::cout << x << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 187 ms 11380 KB Output is correct
2 Correct 229 ms 11748 KB Output is correct
3 Correct 385 ms 13568 KB Output is correct
4 Correct 1299 ms 54032 KB Output is correct
5 Correct 1291 ms 49664 KB Output is correct
6 Correct 1269 ms 51296 KB Output is correct
7 Correct 1424 ms 69404 KB Output is correct
8 Correct 1083 ms 56636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 820 ms 37896 KB Output is correct
6 Correct 1154 ms 42100 KB Output is correct
7 Correct 1223 ms 48636 KB Output is correct
8 Correct 1092 ms 57796 KB Output is correct
9 Correct 150 ms 9804 KB Output is correct
10 Correct 174 ms 10664 KB Output is correct
11 Correct 174 ms 10840 KB Output is correct
12 Correct 1378 ms 70608 KB Output is correct
13 Correct 1165 ms 57924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 2 ms 492 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1327 ms 63244 KB Output is correct
6 Correct 1239 ms 57864 KB Output is correct
7 Correct 1137 ms 51032 KB Output is correct
8 Correct 706 ms 40904 KB Output is correct
9 Correct 299 ms 10324 KB Output is correct
10 Correct 204 ms 12756 KB Output is correct
11 Correct 290 ms 10224 KB Output is correct
12 Correct 205 ms 12912 KB Output is correct
13 Correct 294 ms 10324 KB Output is correct
14 Correct 210 ms 12756 KB Output is correct
15 Correct 1388 ms 70572 KB Output is correct
16 Correct 1141 ms 57852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 187 ms 11380 KB Output is correct
9 Correct 229 ms 11748 KB Output is correct
10 Correct 385 ms 13568 KB Output is correct
11 Correct 1299 ms 54032 KB Output is correct
12 Correct 1291 ms 49664 KB Output is correct
13 Correct 1269 ms 51296 KB Output is correct
14 Correct 1424 ms 69404 KB Output is correct
15 Correct 1083 ms 56636 KB Output is correct
16 Correct 2 ms 492 KB Output is correct
17 Correct 2 ms 492 KB Output is correct
18 Correct 2 ms 492 KB Output is correct
19 Correct 2 ms 492 KB Output is correct
20 Correct 820 ms 37896 KB Output is correct
21 Correct 1154 ms 42100 KB Output is correct
22 Correct 1223 ms 48636 KB Output is correct
23 Correct 1092 ms 57796 KB Output is correct
24 Correct 150 ms 9804 KB Output is correct
25 Correct 174 ms 10664 KB Output is correct
26 Correct 174 ms 10840 KB Output is correct
27 Correct 1378 ms 70608 KB Output is correct
28 Correct 1165 ms 57924 KB Output is correct
29 Correct 2 ms 492 KB Output is correct
30 Correct 2 ms 492 KB Output is correct
31 Correct 2 ms 492 KB Output is correct
32 Correct 1 ms 492 KB Output is correct
33 Correct 1327 ms 63244 KB Output is correct
34 Correct 1239 ms 57864 KB Output is correct
35 Correct 1137 ms 51032 KB Output is correct
36 Correct 706 ms 40904 KB Output is correct
37 Correct 299 ms 10324 KB Output is correct
38 Correct 204 ms 12756 KB Output is correct
39 Correct 290 ms 10224 KB Output is correct
40 Correct 205 ms 12912 KB Output is correct
41 Correct 294 ms 10324 KB Output is correct
42 Correct 210 ms 12756 KB Output is correct
43 Correct 1388 ms 70572 KB Output is correct
44 Correct 1141 ms 57852 KB Output is correct
45 Correct 92 ms 6652 KB Output is correct
46 Correct 148 ms 7084 KB Output is correct
47 Correct 608 ms 23420 KB Output is correct
48 Correct 1251 ms 53520 KB Output is correct
49 Correct 187 ms 11712 KB Output is correct
50 Correct 181 ms 11332 KB Output is correct
51 Correct 203 ms 11584 KB Output is correct
52 Correct 232 ms 13244 KB Output is correct
53 Correct 231 ms 13124 KB Output is correct
54 Correct 242 ms 13124 KB Output is correct