Submission #554352

# Submission time Handle Problem Language Result Execution time Memory
554352 2022-04-28T08:08:53 Z Sweezy Street Lamps (APIO19_street_lamps) C++17
100 / 100
4975 ms 112108 KB
#include <bits/stdc++.h>

using namespace std;
#define int long long

template <typename A, typename B>
string to_string(pair<A, B> p);
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
 
string to_string(const string& s) {
  return '"' + s + '"';
}
 
string to_string(const char* s) {
  return to_string((string) s);
}
 
string to_string(bool b) {
  return (b ? "true" : "false");
}
 
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
 
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
 
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;
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
 
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

void solve() {
  int n, q;
  string s;
  cin >> n >> q >> s;

  vector<int> go(n, -1);
  go[n - 1] = (s[n - 1] == '1' ? n - 1 : -1);
  for (int i = n - 2; i >= 0; i--) {
    if (s[i] == '1') {
      go[i] = (s[i + 1] == '1' ? go[i + 1] : i);
    }
  }

  vector<array<int, 3>> queries;
  vector<vector<pair<int, int>>> gos(n);

  auto update = [&] (int i, int time, int nv) {
    go[i] = nv;
    gos[i].emplace_back(time, nv);
  };

  for (int i = 0; i < n; i++) {
    update(i, 0, go[i]);
  }

  for (int i = 1; i <= q; i++) {
    string t;
    cin >> t;
    if (t == "toggle") {
      int id;
      cin >> id;
      --id;
      if (s[id] == '1') {
        update(id, i, -1);
        for (int j = id - 1; j >= 0 && s[j] == '1'; j--) {
          update(j, i, id - 1);
        }
        s[id] = '0';
      } else {
        update(id, i, (id + 1 < n && s[id + 1] == '1' ? go[id + 1] : id));
        for (int j = id - 1; j >= 0 && s[j] == '1'; j--) {
          update(j, i, go[id]);
        }
        s[id] = '1';
      }
    } else {
      int l, r;
      cin >> l >> r;
      --l, --r;
      queries.push_back({l, r - 1, i});
    }
  }

  for (auto &[l, r, time] : queries) {
    int res = 0;
    for (int i = 0; i < (int) gos[l].size(); i++) {
      if (gos[l][i].second < r) continue;
      if (gos[l][i].first <= time) {
        res += min(time, (i + 1 < (int) gos[l].size() ? gos[l][i + 1].first : time)) - gos[l][i].first;
      }
    }

    cout << res << '\n';
  }
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1482 ms 15240 KB Output is correct
2 Correct 566 ms 15144 KB Output is correct
3 Correct 125 ms 16888 KB Output is correct
4 Correct 197 ms 36864 KB Output is correct
5 Correct 440 ms 112108 KB Output is correct
6 Correct 196 ms 37376 KB Output is correct
7 Correct 158 ms 37336 KB Output is correct
8 Correct 162 ms 37316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 449 ms 102856 KB Output is correct
6 Correct 449 ms 110336 KB Output is correct
7 Correct 425 ms 107908 KB Output is correct
8 Correct 163 ms 37104 KB Output is correct
9 Correct 77 ms 9712 KB Output is correct
10 Correct 91 ms 15788 KB Output is correct
11 Correct 81 ms 15988 KB Output is correct
12 Correct 151 ms 37316 KB Output is correct
13 Correct 156 ms 37224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 456 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 164 ms 37768 KB Output is correct
6 Correct 169 ms 35124 KB Output is correct
7 Correct 190 ms 36820 KB Output is correct
8 Correct 221 ms 38728 KB Output is correct
9 Correct 140 ms 18476 KB Output is correct
10 Correct 87 ms 15388 KB Output is correct
11 Correct 155 ms 18180 KB Output is correct
12 Correct 90 ms 15656 KB Output is correct
13 Correct 143 ms 18628 KB Output is correct
14 Correct 88 ms 15696 KB Output is correct
15 Correct 149 ms 37308 KB Output is correct
16 Correct 158 ms 37188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1482 ms 15240 KB Output is correct
9 Correct 566 ms 15144 KB Output is correct
10 Correct 125 ms 16888 KB Output is correct
11 Correct 197 ms 36864 KB Output is correct
12 Correct 440 ms 112108 KB Output is correct
13 Correct 196 ms 37376 KB Output is correct
14 Correct 158 ms 37336 KB Output is correct
15 Correct 162 ms 37316 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 449 ms 102856 KB Output is correct
21 Correct 449 ms 110336 KB Output is correct
22 Correct 425 ms 107908 KB Output is correct
23 Correct 163 ms 37104 KB Output is correct
24 Correct 77 ms 9712 KB Output is correct
25 Correct 91 ms 15788 KB Output is correct
26 Correct 81 ms 15988 KB Output is correct
27 Correct 151 ms 37316 KB Output is correct
28 Correct 156 ms 37224 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 456 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 164 ms 37768 KB Output is correct
34 Correct 169 ms 35124 KB Output is correct
35 Correct 190 ms 36820 KB Output is correct
36 Correct 221 ms 38728 KB Output is correct
37 Correct 140 ms 18476 KB Output is correct
38 Correct 87 ms 15388 KB Output is correct
39 Correct 155 ms 18180 KB Output is correct
40 Correct 90 ms 15656 KB Output is correct
41 Correct 143 ms 18628 KB Output is correct
42 Correct 88 ms 15696 KB Output is correct
43 Correct 149 ms 37308 KB Output is correct
44 Correct 158 ms 37188 KB Output is correct
45 Correct 4975 ms 9844 KB Output is correct
46 Correct 248 ms 9212 KB Output is correct
47 Correct 103 ms 17324 KB Output is correct
48 Correct 208 ms 36848 KB Output is correct
49 Correct 82 ms 15872 KB Output is correct
50 Correct 80 ms 15860 KB Output is correct
51 Correct 85 ms 15972 KB Output is correct
52 Correct 85 ms 16460 KB Output is correct
53 Correct 84 ms 16572 KB Output is correct
54 Correct 86 ms 16532 KB Output is correct