답안 #859613

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
859613 2023-10-10T11:25:32 Z NeroZein 가로등 (APIO19_street_lamps) C++17
100 / 100
539 ms 39148 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif

const int N = 3e5 + 5; 

int n, q;
int tree[N]; 
vector<char> c;
vector<int> ans; 
vector<array<int, 3>> qq; 
vector<array<int, 3>> qu[N];
set<array<int, 3>> ranges;

void toggle(int ind, int t) {
  if (c[ind] == '1') {
    array<int, 3> key = {ind, n, n}; 
    auto f = ranges.upper_bound(key);
    --f;
    auto [l, r, val] = *f; 
    if (l < ind) {
      ranges.insert({l, ind - 1, t}); 
    }
    if (ind < r) {
      ranges.insert({ind + 1, r, t}); 
    }
    qu[t].push_back({l, r, t - val}); 
    ranges.erase(f); 
  } else {
    bool pre = ind > 0 && c[ind - 1] == '1';
    bool nx = ind < n - 1 && c[ind + 1] == '1';
    if (nx && pre) {
      array<int, 3> key = {ind, n, n};
      auto f = ranges.upper_bound(key);
      auto [l, r, val] = *f;
      auto [ll, rr, vval] = *prev(f); 
      qu[t].push_back({l, r, t - val});
      qu[t].push_back({ll, rr, t - vval}); 
      ranges.erase(prev(f));
      ranges.erase(f); 
      ranges.insert({ll, r, t}); 
    } else if (nx) {
      array<int, 3> key = {ind, n, n};
      auto f = ranges.upper_bound(key);
      auto [l, r, val] = *f;
      qu[t].push_back({l, r, t - val}); 
      ranges.insert({ind, r, t});
      ranges.erase(f); 
    } else if (pre) {
      array<int, 3> key = {ind, n, n};
      auto f = ranges.upper_bound(key);
      --f;
      auto [l, r, val] = *f; 
      qu[t].push_back({l, r, t - val}); 
      ranges.insert({l, ind, t}); 
      ranges.erase(f); 
    } else {
      ranges.insert({ind, ind, t}); 
    }
  }
  c[ind] ^= 1;
}

inline void upd (int id, int v) {
  id++; 
  while (id < N) {
    tree[id] += v;
    id += id & -id;
  }
}

inline long long qry (int id) {
  id++; 
  long long ret = 0; 
  while (id) {
    ret += tree[id];
    id -= id & -id; 
  }
  return ret; 
}

int qry(int l, int r) {
  return qry(r) - qry(l - 1); 
}


void solve(int l, int r) {
  if (l == r) {
    return; 
  }
  int mid = (l + r) / 2;
  vector<array<int, 4>> t; 
  for (int i = l; i <= mid; ++i) {
    for (auto [ll, rr, v] : qu[i]) {
      t.push_back({ll, 0, rr, v});
    }
  }
  for (int i = mid + 1; i <= r; ++i) {
    auto [ll, rr, e] = qq[i];
    if (e) {
      t.push_back({ll, 1, rr, i}); 
    }
  }
  //deb(l) deb(r) deb(t) cout << '\n';
  sort(t.begin(), t.end()); 
  for (int i = 0; i < (int) t.size(); ++i) {
    auto [ll, tp, rr, val] = t[i];
    if (tp == 0) {
      upd(rr, val); 
      //deb(rr) deb(val) cout << '\n';
    } else {
      //deb(rr) deb(qry(rr, n)) cout << '\n';
      ans[val] += qry(rr, n); 
    }
  }
  for (int i = 0; i < (int) t.size(); ++i) {
    auto [ll, tp, rr, val] = t[i];
    if (tp == 0) {
      upd(rr, -val); 
    }
  }
  solve(l, mid);
  solve(mid + 1, r); 
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> q;
  c.resize(n); 
  for (int i = 0; i < n; ++i) {
    cin >> c[i]; 
  }
  for (int i = 0; i < n; ++i) {
    if (c[i] == '0') {
      continue; 
    }
    int j = i;
    while (j + 1 < n && c[j + 1] == c[j]) {
      j++;
    }
    ranges.insert({i, j, -1}); 
    i = j; 
  }
  qq.resize(q); 
  ans.resize(q); 
  for (int i = 0; i < q; ++i) {
    string qt;
    cin >> qt;
    //deb(ranges) cout << '\n';
    if (qt[0] == 'q') {
      int x, y;
      cin >> x >> y;
      --x, y -= 2; 
      array<int, 3> key = {x, n, n};
      auto f = ranges.upper_bound(key);
      if (f != ranges.begin()) {
        --f;
        if ((*f)[1] >= y) {
          ans[i] += i - (*f)[2];          
        }
      }
      qq[i] = {x, y, 1}; 
    } else {
      int ind;
      cin >> ind;
      toggle(ind - 1, i); 
    }
  }
  solve(0, q - 1); 
  for (int i = 0; i < q; ++i) {
    if (qq[i][2] == 1) {
      cout << ans[i] << '\n';      
    }
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 2 ms 7516 KB Output is correct
3 Correct 2 ms 7516 KB Output is correct
4 Correct 2 ms 7516 KB Output is correct
5 Correct 2 ms 7384 KB Output is correct
6 Correct 2 ms 7256 KB Output is correct
7 Correct 2 ms 7260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 423 ms 23228 KB Output is correct
2 Correct 424 ms 27076 KB Output is correct
3 Correct 412 ms 27032 KB Output is correct
4 Correct 486 ms 33776 KB Output is correct
5 Correct 538 ms 31600 KB Output is correct
6 Correct 496 ms 39148 KB Output is correct
7 Correct 302 ms 25132 KB Output is correct
8 Correct 304 ms 24356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7516 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7512 KB Output is correct
4 Correct 2 ms 7516 KB Output is correct
5 Correct 445 ms 30216 KB Output is correct
6 Correct 539 ms 31288 KB Output is correct
7 Correct 535 ms 30908 KB Output is correct
8 Correct 331 ms 23480 KB Output is correct
9 Correct 180 ms 17552 KB Output is correct
10 Correct 209 ms 20248 KB Output is correct
11 Correct 199 ms 21184 KB Output is correct
12 Correct 305 ms 23488 KB Output is correct
13 Correct 306 ms 23476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7516 KB Output is correct
2 Correct 2 ms 7544 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 402 ms 25020 KB Output is correct
6 Correct 456 ms 31536 KB Output is correct
7 Correct 480 ms 34072 KB Output is correct
8 Correct 492 ms 34820 KB Output is correct
9 Correct 483 ms 29400 KB Output is correct
10 Correct 472 ms 31160 KB Output is correct
11 Correct 476 ms 33468 KB Output is correct
12 Correct 468 ms 29800 KB Output is correct
13 Correct 452 ms 34488 KB Output is correct
14 Correct 480 ms 31304 KB Output is correct
15 Correct 306 ms 24520 KB Output is correct
16 Correct 311 ms 24420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 2 ms 7516 KB Output is correct
3 Correct 2 ms 7516 KB Output is correct
4 Correct 2 ms 7516 KB Output is correct
5 Correct 2 ms 7384 KB Output is correct
6 Correct 2 ms 7256 KB Output is correct
7 Correct 2 ms 7260 KB Output is correct
8 Correct 423 ms 23228 KB Output is correct
9 Correct 424 ms 27076 KB Output is correct
10 Correct 412 ms 27032 KB Output is correct
11 Correct 486 ms 33776 KB Output is correct
12 Correct 538 ms 31600 KB Output is correct
13 Correct 496 ms 39148 KB Output is correct
14 Correct 302 ms 25132 KB Output is correct
15 Correct 304 ms 24356 KB Output is correct
16 Correct 2 ms 7516 KB Output is correct
17 Correct 3 ms 7516 KB Output is correct
18 Correct 3 ms 7512 KB Output is correct
19 Correct 2 ms 7516 KB Output is correct
20 Correct 445 ms 30216 KB Output is correct
21 Correct 539 ms 31288 KB Output is correct
22 Correct 535 ms 30908 KB Output is correct
23 Correct 331 ms 23480 KB Output is correct
24 Correct 180 ms 17552 KB Output is correct
25 Correct 209 ms 20248 KB Output is correct
26 Correct 199 ms 21184 KB Output is correct
27 Correct 305 ms 23488 KB Output is correct
28 Correct 306 ms 23476 KB Output is correct
29 Correct 2 ms 7516 KB Output is correct
30 Correct 2 ms 7544 KB Output is correct
31 Correct 3 ms 7516 KB Output is correct
32 Correct 3 ms 7516 KB Output is correct
33 Correct 402 ms 25020 KB Output is correct
34 Correct 456 ms 31536 KB Output is correct
35 Correct 480 ms 34072 KB Output is correct
36 Correct 492 ms 34820 KB Output is correct
37 Correct 483 ms 29400 KB Output is correct
38 Correct 472 ms 31160 KB Output is correct
39 Correct 476 ms 33468 KB Output is correct
40 Correct 468 ms 29800 KB Output is correct
41 Correct 452 ms 34488 KB Output is correct
42 Correct 480 ms 31304 KB Output is correct
43 Correct 306 ms 24520 KB Output is correct
44 Correct 311 ms 24420 KB Output is correct
45 Correct 287 ms 18376 KB Output is correct
46 Correct 274 ms 18880 KB Output is correct
47 Correct 288 ms 21440 KB Output is correct
48 Correct 482 ms 33720 KB Output is correct
49 Correct 238 ms 21436 KB Output is correct
50 Correct 236 ms 22468 KB Output is correct
51 Correct 229 ms 21280 KB Output is correct
52 Correct 252 ms 21440 KB Output is correct
53 Correct 214 ms 21456 KB Output is correct
54 Correct 218 ms 21544 KB Output is correct