답안 #553482

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
553482 2022-04-26T02:47:58 Z quocnguyen1012 가로등 (APIO19_street_lamps) C++14
100 / 100
2473 ms 149704 KB
#include "bits/stdc++.h"

using namespace std;

string to_string(string s) {
  return '"' + s + '"';
}
 
string to_string(const char* s) {
  return to_string((string) s);
}
 
string to_string(bool b) {
  return (b ? "true" : "false");
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
 
void debug_out() { cerr << endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
 
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

struct BIT2d {
  vector<vector<int>> val;
  vector<vector<pair<int, int>>> bit;
  BIT2d(int n) {
    bit.assign(n + 5, {});
    val.assign(n + 5, {});
  }
  pair<int, int> add(pair<int, int> x, pair<int, int> y) {
    x.first += y.first; x.second += y.second;
    return x;
  }
  pair<int, int> minus(pair<int, int> x, pair<int, int> y) {
    x.first -= y.first; x.second -= y.second;
    return x;
  }
  void normalize() {
    for (int x = 1; x < (int)bit.size(); ++x) {
      val[x].push_back(INT_MIN);
      sort(val[x].begin(), val[x].end());
      val[x].erase(unique(val[x].begin(), val[x].end()), val[x].end());
      bit[x].assign(val[x].size() + 5, {0, 0});
    }
  }
  void fakeupdate(int x, int y) {
    for (int i = x; i < (int)bit.size(); i += i & -i) {
      val[i].push_back(y);
    }
  }
  void update(int x, int y, pair<int, int> delta) {
    for (; x < (int)bit.size(); x += x & -x) {
      //debug(x, y, *upper_bound(val[x].begin(), val[x].end(), y));
      //debug(val[x]);
      for (int i = upper_bound(val[x].begin(), val[x].end(), y) - val[x].begin();
                i < (int)bit[x].size(); i += i & -i) {
                  bit[x][i] = add(bit[x][i], delta);
                }
    }
  }

  pair<int, int> getsum(int x, int l, int r) {
    pair<int, int> tot{0, 0};
    for (; x; x -= x & -x) {
      const auto get = [&](int y) {
        pair<int, int> res{0, 0};
        for (int i = upper_bound(val[x].begin(), val[x].end(), y) - val[x].begin();
                i; i -= i & -i) {
                  res = add(res, bit[x][i]);
                }
        return res;
      };
      tot = add(tot, minus(get(r), get(l - 1)));
    }
    return tot;
  }
  pair<int, int> getsum(int x1, int y1, int x2, int y2) {
    return minus(getsum(x2, y1, y2), getsum(x1 - 1, y1, y2));
  }
  pair<int, int> nestedInterval(int l, int r) {
    return getsum(1, r, l, 1e9);
  }
};

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int n, q; cin >> n >> q;
  string s; cin >> s; s = " " + s;
  vector<tuple<int, int, int>> queries;
  queries.emplace_back(0, 0, 0);
  for (int i = 1; i <= q; ++i) {
    string op; cin >> op;
    if (op[0] == 'q') {
      int l, r; cin >> l >> r;
      queries.emplace_back(1, l, r);
    } else {
      int pos; cin >> pos;
      queries.emplace_back(0, pos, 0);
    }
  }
  set<pair<int, int>> intervals;
  BIT2d t(n + 5);

  const auto initIntervals = [&](const string &s) {
    vector<pair<int, int>> v;
    for (int i = 1; i <= n; ++i) {
      if (s[i] == '0') continue;
      int j = i;
      while (j <= n and s[i] == s[j]) ++j; --j;
      v.emplace_back(i, j);
      i = j;
    }
    return v;
  };
  /// fakeupdate 
  string cs = s;
  intervals.clear();
  for (const auto &x : initIntervals(s)) {
    intervals.insert(x);
  }
  vector<vector<tuple<int, int, int>>> addSeg(q + 5), delSeg(q + 5);
  map<pair<int, int>, int> last;
  for (auto [l, r] : intervals) {
    addSeg[1].emplace_back(l, r, 0);
    last[{l, r}] = 0;
  }
  for (int i = 1; i <= q; ++i) {
    const auto&[op, a, b] = queries[i];
    const auto addInterval = [&](int l, int r) {
      if (l > r) return;
      last[{l, r}] = i;
      intervals.insert({l, r});
      addSeg[i].emplace_back(l, r, i);
    };
    const auto delInterval = [&](int l, int r) {
      if (l > r) return; 
      intervals.erase(intervals.find({l, r}));
      delSeg[i].emplace_back(l, r, i);
      last[{l, r}] = 0;
    };
    if (op == 1) {
      
    } else {
      if (s[a] == '0') {
        auto it = intervals.upper_bound({a, INT_MAX}); 
        int l = a, r = a;
        auto pre = it;
        bool good = (it!=intervals.begin());
        if (good) --pre;
        if (it != intervals.end()) {
          if (it->first==a+1) {
            r=it->second;
            delInterval(it->first, it->second);
          }
        }
        if (good) {
          if (pre->second+1==a) {
            l = pre->first;
            delInterval(pre->first, pre->second);
          }
        }
        addInterval(l, r);
      } else {
        auto it = intervals.upper_bound({a, INT_MAX});
        --it;
        auto [l, r] = (*it);
        delInterval(l, r);
        addInterval(l, a-1);
        addInterval(a+1, r);
      }
      s[a] = '0' + '1' - s[a];
    }
    for (auto [x, y, ignore] : addSeg[i]) t.fakeupdate(x, y);
    for (auto [x, y, ignore] : delSeg[i]) t.fakeupdate(x, y);
  }
  t.normalize();
  for (int i = 1; i <= q; ++i) {
    for (auto [x, y, id] : addSeg[i]) {
      debug("add", x, y, id);
      t.update(x, y, {1, -id});
    } 
    for (auto [x, y, id] : delSeg[i]){
      t.update(x, y, {1, id});
      debug("del", x, y, id);
    } 
    auto [op, a, b] = queries[i];
    if (op == 1) {
      auto res = t.nestedInterval(a, b-1);
      debug(res);
      cout << (res.first % 2) * (i) + res.second<< '\n';
    }
  }
}

Compilation message

street_lamps.cpp: In lambda function:
street_lamps.cpp:137:7: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  137 |       while (j <= n and s[i] == s[j]) ++j; --j;
      |       ^~~~~
street_lamps.cpp:137:44: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  137 |       while (j <= n and s[i] == s[j]) ++j; --j;
      |                                            ^~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:151:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  151 |   for (auto [l, r] : intervals) {
      |             ^
street_lamps.cpp:156:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  156 |     const auto&[op, a, b] = queries[i];
      |                ^
street_lamps.cpp:194:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  194 |         auto [l, r] = (*it);
      |              ^
street_lamps.cpp:201:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  201 |     for (auto [x, y, ignore] : addSeg[i]) t.fakeupdate(x, y);
      |               ^
street_lamps.cpp:202:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  202 |     for (auto [x, y, ignore] : delSeg[i]) t.fakeupdate(x, y);
      |               ^
street_lamps.cpp:206:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  206 |     for (auto [x, y, id] : addSeg[i]) {
      |               ^
street_lamps.cpp:48:20: warning: statement has no effect [-Wunused-value]
   48 | #define debug(...) 42
      |                    ^~
street_lamps.cpp:207:7: note: in expansion of macro 'debug'
  207 |       debug("add", x, y, id);
      |       ^~~~~
street_lamps.cpp:210:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  210 |     for (auto [x, y, id] : delSeg[i]){
      |               ^
street_lamps.cpp:48:20: warning: statement has no effect [-Wunused-value]
   48 | #define debug(...) 42
      |                    ^~
street_lamps.cpp:212:7: note: in expansion of macro 'debug'
  212 |       debug("del", x, y, id);
      |       ^~~~~
street_lamps.cpp:214:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  214 |     auto [op, a, b] = queries[i];
      |          ^
street_lamps.cpp:48:20: warning: statement has no effect [-Wunused-value]
   48 | #define debug(...) 42
      |                    ^~
street_lamps.cpp:217:7: note: in expansion of macro 'debug'
  217 |       debug(res);
      |       ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 326 ms 32692 KB Output is correct
2 Correct 340 ms 34288 KB Output is correct
3 Correct 583 ms 40360 KB Output is correct
4 Correct 1666 ms 115276 KB Output is correct
5 Correct 1627 ms 110100 KB Output is correct
6 Correct 1700 ms 120788 KB Output is correct
7 Correct 315 ms 62152 KB Output is correct
8 Correct 306 ms 63620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 724 KB Output is correct
2 Correct 3 ms 584 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 2452 ms 140712 KB Output is correct
6 Correct 2200 ms 130808 KB Output is correct
7 Correct 1541 ms 113892 KB Output is correct
8 Correct 340 ms 68784 KB Output is correct
9 Correct 87 ms 18296 KB Output is correct
10 Correct 96 ms 19996 KB Output is correct
11 Correct 128 ms 20108 KB Output is correct
12 Correct 294 ms 67308 KB Output is correct
13 Correct 278 ms 68668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 2 ms 596 KB Output is correct
3 Correct 3 ms 596 KB Output is correct
4 Correct 3 ms 724 KB Output is correct
5 Correct 563 ms 80416 KB Output is correct
6 Correct 1118 ms 99388 KB Output is correct
7 Correct 1661 ms 120316 KB Output is correct
8 Correct 2473 ms 149704 KB Output is correct
9 Correct 429 ms 42912 KB Output is correct
10 Correct 392 ms 45344 KB Output is correct
11 Correct 401 ms 42952 KB Output is correct
12 Correct 377 ms 45196 KB Output is correct
13 Correct 413 ms 42832 KB Output is correct
14 Correct 361 ms 45160 KB Output is correct
15 Correct 269 ms 62216 KB Output is correct
16 Correct 285 ms 63592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 326 ms 32692 KB Output is correct
9 Correct 340 ms 34288 KB Output is correct
10 Correct 583 ms 40360 KB Output is correct
11 Correct 1666 ms 115276 KB Output is correct
12 Correct 1627 ms 110100 KB Output is correct
13 Correct 1700 ms 120788 KB Output is correct
14 Correct 315 ms 62152 KB Output is correct
15 Correct 306 ms 63620 KB Output is correct
16 Correct 3 ms 724 KB Output is correct
17 Correct 3 ms 584 KB Output is correct
18 Correct 2 ms 596 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 2452 ms 140712 KB Output is correct
21 Correct 2200 ms 130808 KB Output is correct
22 Correct 1541 ms 113892 KB Output is correct
23 Correct 340 ms 68784 KB Output is correct
24 Correct 87 ms 18296 KB Output is correct
25 Correct 96 ms 19996 KB Output is correct
26 Correct 128 ms 20108 KB Output is correct
27 Correct 294 ms 67308 KB Output is correct
28 Correct 278 ms 68668 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 2 ms 596 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
32 Correct 3 ms 724 KB Output is correct
33 Correct 563 ms 80416 KB Output is correct
34 Correct 1118 ms 99388 KB Output is correct
35 Correct 1661 ms 120316 KB Output is correct
36 Correct 2473 ms 149704 KB Output is correct
37 Correct 429 ms 42912 KB Output is correct
38 Correct 392 ms 45344 KB Output is correct
39 Correct 401 ms 42952 KB Output is correct
40 Correct 377 ms 45196 KB Output is correct
41 Correct 413 ms 42832 KB Output is correct
42 Correct 361 ms 45160 KB Output is correct
43 Correct 269 ms 62216 KB Output is correct
44 Correct 285 ms 63592 KB Output is correct
45 Correct 123 ms 22760 KB Output is correct
46 Correct 173 ms 23276 KB Output is correct
47 Correct 767 ms 57404 KB Output is correct
48 Correct 1617 ms 119384 KB Output is correct
49 Correct 109 ms 22148 KB Output is correct
50 Correct 93 ms 22052 KB Output is correct
51 Correct 100 ms 22212 KB Output is correct
52 Correct 105 ms 22576 KB Output is correct
53 Correct 101 ms 22528 KB Output is correct
54 Correct 101 ms 22528 KB Output is correct