Submission #967075

# Submission time Handle Problem Language Result Execution time Memory
967075 2024-04-21T05:44:10 Z kilkuwu Street Lamps (APIO19_street_lamps) C++17
100 / 100
1047 ms 229844 KB
#include <bits/stdc++.h>

#define nl '\n'

#ifdef LOCAL
#include "template/debug.hpp"
#else
#define dbg(...) ;
#define timer(...) ;
#endif

struct SegmentTreeX {
  struct SegmentTreeY {
    struct Node {
      int v = 0, l = 0, r = 0;
    };

    std::vector<Node> tree = {Node(), Node()};

    int add_node() {
      tree.emplace_back();
      return tree.size() - 1;
    }

    void update(int k, int l, int r, int ql, int qr, int qv) {
      if (ql <= l && r <= qr) {
        tree[k].v += qv;
        return;
      }

      int m = (l + r) / 2;
      if (m >= ql) { // (l, m)
        if (tree[k].l == 0) tree[k].l = add_node();
        update(tree[k].l, l, m, ql, qr, qv);
      }
      if (m + 1 <= qr) {
        if (tree[k].r == 0) tree[k].r = add_node();
        update(tree[k].r, m + 1, r, ql, qr, qv);
      }
    }

    int query(int k, int l, int r, int qp) {
      int ans = tree[k].v;
      if (l == r) {
        return ans;
      }

      int m = (l + r) / 2;
      if (qp <= m) {
        return ans + query(tree[k].l, l, m, qp);
      }
      return ans + query(tree[k].r, m + 1, r, qp);
    }
  };

  int n;
  std::vector<SegmentTreeY> tree;

  SegmentTreeX(int _n) : n(_n), tree(4 * n) {}

  void update(int k, int l, int r, int ql, int qr, int ly, int ry, int qv) {
    if (ql <= l && r <= qr) {
      tree[k].update(1, 0, n - 1, ly, ry, qv);
      return;
    }

    int m = (l + r) / 2;
    if (m >= ql) {  // (l, m)
      update(k << 1, l, m, ql, qr, ly, ry, qv);
    }
    if (m + 1 <= qr) {
      update(k << 1 | 1, m + 1, r, ql, qr, ly, ry, qv);
    }
  }

  int query(int k, int l, int r, int qpx, int qpy) {
    int ans = tree[k].query(1, 0, n - 1, qpy);
    if (l == r) {
      return ans;
    }

    int m = (l + r) / 2;
    if (qpx <= m) {
      return ans + query(k << 1, l, m, qpx, qpy);
    }
    return ans + query(k << 1 | 1, m + 1, r, qpx, qpy);
  }
};

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n, q;
  std::cin >> n >> q;
  std::vector<int> a(n);
  for (int i = 0; i < n; i++) {
    char c; std::cin >> c;
    a[i] = c - '0';
  }
  
  std::set<std::pair<int, int>> ranges;

  for (int i = 0; i < n; i++) {
    if (a[i] == 0) continue;
    int j = i;
    while (j < n && a[j] == 1) {
      j++;
    }
    ranges.emplace(i, j); // [i, j) full of ones
    i = j;
  }

  auto find_contain_and_toggle = [&](int i) -> std::pair<int, int> {
    auto it = ranges.upper_bound({i, std::numeric_limits<int>::max()}); // {{i, i+1}, ...}
    assert(it != ranges.begin());
    --it;
    auto res = *it;
    ranges.erase(it);
    if (res.first < i) { 
      ranges.emplace(res.first, i);
    }
    if (i + 1 < res.second) {
      ranges.emplace(i + 1, res.second);
    }
    return res;
  };

  auto find_left_right_and_toggle = [&](int i) -> std::pair<int, int> {
    auto it = ranges.upper_bound({i, i + 1});
    int l = i, r = i + 1;
    if (it != ranges.end()) {
      if (it->first == i + 1) {
        r = it->second;
        it = ranges.erase(it);
      }
    } 
    if (it != ranges.begin()) {
      --it;
      if (it->second == i) {
        l = it->first;
        it = ranges.erase(it);
      }
    }

    ranges.emplace(l, r);
    return {l, r};
  };

  auto check_range = [&](int l, int r) -> bool {
    auto it = ranges.upper_bound({l, std::numeric_limits<int>::max()});
    if (it == ranges.begin()) return false;
    --it;
    return r < it->second;
  };

  SegmentTreeX tree(n);

  for (int i = 1; i <= q; i++) {
    std::string com;
    std::cin >> com;
    if (com[0] == 't') {
      int id;
      std::cin >> id;
      --id;
      if (a[id] == 0) {
        auto [l, r] = find_left_right_and_toggle(id);
        tree.update(1, 0, n - 1, l, id, id, r - 1, -i);
      } else {
        auto [l, r] = find_contain_and_toggle(id);
        tree.update(1, 0, n - 1, l, id, id, r - 1, i);
      }
      a[id] ^= 1;
    } else {
      int l, r;
      std::cin >> l >> r;
      l--, r -= 2;
      // query value of (a, b)  
      // get value of a, b 
      int ans = i * check_range(l, r);
      ans += tree.query(1, 0, n - 1, l, r);
      std::cout << ans << nl;

    }
  }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 132 ms 1876 KB Output is correct
2 Correct 156 ms 5200 KB Output is correct
3 Correct 255 ms 9812 KB Output is correct
4 Correct 859 ms 135620 KB Output is correct
5 Correct 1047 ms 174304 KB Output is correct
6 Correct 792 ms 143344 KB Output is correct
7 Correct 730 ms 73816 KB Output is correct
8 Correct 739 ms 75092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 856 KB Output is correct
2 Correct 2 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 919 ms 229844 KB Output is correct
6 Correct 941 ms 202380 KB Output is correct
7 Correct 967 ms 168048 KB Output is correct
8 Correct 804 ms 69424 KB Output is correct
9 Correct 91 ms 1200 KB Output is correct
10 Correct 103 ms 1608 KB Output is correct
11 Correct 108 ms 1364 KB Output is correct
12 Correct 740 ms 68192 KB Output is correct
13 Correct 814 ms 69364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
3 Correct 1 ms 800 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 942 ms 79132 KB Output is correct
6 Correct 859 ms 116444 KB Output is correct
7 Correct 783 ms 142648 KB Output is correct
8 Correct 631 ms 167680 KB Output is correct
9 Correct 147 ms 4276 KB Output is correct
10 Correct 106 ms 3496 KB Output is correct
11 Correct 148 ms 4176 KB Output is correct
12 Correct 111 ms 3412 KB Output is correct
13 Correct 148 ms 4272 KB Output is correct
14 Correct 110 ms 3340 KB Output is correct
15 Correct 701 ms 73768 KB Output is correct
16 Correct 709 ms 75092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 132 ms 1876 KB Output is correct
9 Correct 156 ms 5200 KB Output is correct
10 Correct 255 ms 9812 KB Output is correct
11 Correct 859 ms 135620 KB Output is correct
12 Correct 1047 ms 174304 KB Output is correct
13 Correct 792 ms 143344 KB Output is correct
14 Correct 730 ms 73816 KB Output is correct
15 Correct 739 ms 75092 KB Output is correct
16 Correct 2 ms 856 KB Output is correct
17 Correct 2 ms 860 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 919 ms 229844 KB Output is correct
21 Correct 941 ms 202380 KB Output is correct
22 Correct 967 ms 168048 KB Output is correct
23 Correct 804 ms 69424 KB Output is correct
24 Correct 91 ms 1200 KB Output is correct
25 Correct 103 ms 1608 KB Output is correct
26 Correct 108 ms 1364 KB Output is correct
27 Correct 740 ms 68192 KB Output is correct
28 Correct 814 ms 69364 KB Output is correct
29 Correct 1 ms 600 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 1 ms 800 KB Output is correct
32 Correct 1 ms 860 KB Output is correct
33 Correct 942 ms 79132 KB Output is correct
34 Correct 859 ms 116444 KB Output is correct
35 Correct 783 ms 142648 KB Output is correct
36 Correct 631 ms 167680 KB Output is correct
37 Correct 147 ms 4276 KB Output is correct
38 Correct 106 ms 3496 KB Output is correct
39 Correct 148 ms 4176 KB Output is correct
40 Correct 111 ms 3412 KB Output is correct
41 Correct 148 ms 4272 KB Output is correct
42 Correct 110 ms 3340 KB Output is correct
43 Correct 701 ms 73768 KB Output is correct
44 Correct 709 ms 75092 KB Output is correct
45 Correct 53 ms 3012 KB Output is correct
46 Correct 83 ms 2868 KB Output is correct
47 Correct 474 ms 57936 KB Output is correct
48 Correct 870 ms 135156 KB Output is correct
49 Correct 116 ms 4348 KB Output is correct
50 Correct 109 ms 4492 KB Output is correct
51 Correct 127 ms 4768 KB Output is correct
52 Correct 161 ms 4948 KB Output is correct
53 Correct 160 ms 4884 KB Output is correct
54 Correct 159 ms 4944 KB Output is correct