답안 #917768

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
917768 2024-01-28T18:11:28 Z duckindog 가로등 (APIO19_street_lamps) C++14
100 / 100
1768 ms 110636 KB
//from duckindog wth depression
#include<bits/stdc++.h>

using namespace std;

const int N = 3e5 + 10;
int n, q;
bool a[N], b[N];
struct Query {
  int ty, x, y;
  Query() : ty(0), x(0), y(0) {}
  Query(int ty, int x, int y) : ty(ty), x(x), y(y) {}
} Q[N];

bool f[4 * N];
pair<int, int> rvs[N * 4];
void build(int s, int l, int r) {
  rvs[s] = {l, r};
  if (l == r) {
    f[s] = a[l];
    return;
  }
  int mid = l + r >> 1;
  build(s * 2, l, mid); build(s * 2 + 1, mid + 1, r);
  f[s] = f[s * 2] & f[s * 2 + 1];
}
void update(int s, int l, int r, int x, int y) {
  if (y < l || y > r) return;
  if (l == r) {
    f[s] = x;
    return;
  }
  int mid = l + r >> 1;
  update(s * 2, l, mid, x, y); update(s * 2 + 1, mid + 1, r, x, y);
  f[s] = f[s * 2] & f[s * 2 + 1];
}
int query(int s, int l, int r, int u, int v) {
  if (v < l || u > r) return 1;
  if (u <= l && r <= v) return f[s];
  int mid = l + r >> 1;
  return (query(s * 2, l, mid, u, v) & query(s * 2 + 1, mid + 1, r, u, v));
}
vector<int> section;
void put(int s, int l, int r, int u, int v) {
  if (v < l || u > r) return;
  if (u <= l && r <= v) {
    section.push_back(s);
    return;
  }
  int mid = l + r >> 1;
  put(s * 2, l, mid, u, v); put(s * 2 + 1, mid + 1, r, u, v);
}

int dfsl(int s, int l, int r) {
  if (l == r) return l;
  int m = l + r >> 1;
  if (!f[s * 2 + 1]) return dfsl(2 * s + 1, m + 1, r);
  return dfsl(2 * s, l, m);
}
int dfsr(int s, int l, int r) {
  if (l == r) return l;
  int m = l + r >> 1;
  if (!f[s * 2]) return dfsr(2 * s, l, m);
  return dfsr(2 * s + 1, m + 1, r);
}

int get(int x, int w) {
  section.clear();
  int l = w == 1 ? x : 1, r = w == 1 ? n : x;
  put(1, 1, n, l, r);

  if (w) {
    for (const auto&s : section) {
      if (!f[s]) {
        return dfsr(s, rvs[s].first, rvs[s].second);
      }
    }
    return n + 1;
  } else {
    reverse(section.begin(), section.end());
    for (const auto&s : section) {
      if (!f[s]) {
        return dfsl(s, rvs[s].first, rvs[s].second);
      }
    }
    return 0;
  }
}

vector<int> bit[N], vt[N];
void upd(int i, int j, int x) {
  for (; i; i -= i & -i) {
    auto p = lower_bound(vt[i].begin(), vt[i].end(), j) - vt[i].begin() + 1;
    for (int t = p; t <= vt[i].size(); t += t & -t) bit[i][t] += x;
  }
}
int que(int i, int j) {
  int ret = 0;
  for (; i <= n; i += i & -i) {
    auto p = upper_bound(vt[i].begin(), vt[i].end(), j) - vt[i].begin();
    for (int t = p; t; t -= t & -t) ret += bit[i][t];
  }
  return ret;
}

int32_t main() {
  cin.tie(0)->sync_with_stdio(0);

  if (fopen("duck.inp", "r")) {
    freopen("duck.inp", "r", stdin);
    freopen("duck.out", "w", stdout);
  }
  cin >> n >> q;
  for (int i = 1; i <= n; ++i) {
    char x; cin >> x;
    a[i] = b[i] = (x == '1');
  }

  vector<pair<int, int>> updlist;
  build(1, 1, n);

  for (int i = 1; i <= q; ++i) {
    string ty; cin >> ty;
    if (ty == "toggle") {
      int x; cin >> x;
      a[x] = 1 - a[x];
      update(1, 1, n, a[x], x);
      int l = (a[x - 1] == 1 ? get(x - 1, 0) + 1 : x), r = (a[x + 1] == 1 ? get(x + 1, 1) - 1 : x);

      updlist.push_back({x, x});
      updlist.push_back({l - 1, x});
      updlist.push_back({x, r + 1});
      updlist.push_back({l - 1, r + 1});

      Q[i] = {0, x, 0};

    } else {
      int x, y; cin >> x >> y;
      Q[i] = {1, x, y - 1};
    }
  }

  sort(updlist.begin(), updlist.end());
  updlist.resize(unique(updlist.begin(), updlist.end()) - updlist.begin());
  for (auto duck : updlist) {
    int x, y; tie(x, y) = duck;
    for (int i = x; i; i -= i & -i) vt[i].push_back(y);
  }
  for (int i = 1; i <= n; ++i) {
    sort(vt[i].begin(), vt[i].end());
    vt[i].resize(unique(vt[i].begin(), vt[i].end()) - vt[i].begin());
    bit[i].resize(vt[i].size() + 5);
  }
  for (int i = 1; i <= n; ++i) a[i] = b[i];
  build(1, 1, n);

  for (int i = 1; i <= q; ++i) {
    int ty = Q[i].ty, x = Q[i].x, y = Q[i].y;
    if (!ty) {
      a[x] = 1 - a[x];
      update(1, 1, n, a[x], x);
      int l = (a[x - 1] == 1 ? get(x - 1, 0) + 1 : x), r = (a[x + 1] == 1 ? get(x + 1, 1) - 1 : x);

      if (a[x]) {
        upd(x, x, -i);
        upd(l - 1, x, i);
        upd(x, r + 1, i);
        upd(l - 1, r + 1, -i);
      } else {
        upd(x, x, i);
        upd(l - 1, x, -i);
        upd(x, r + 1, -i);
        upd(l - 1, r + 1, i);
      }


    } else {
      if (!query(1, 1, n, x, y)) cout << que(x, y) << "\n";
      else cout << i + que(x, y) << '\n';
    }

  }

}

Compilation message

street_lamps.cpp: In function 'void build(int, int, int)':
street_lamps.cpp:23:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   23 |   int mid = l + r >> 1;
      |             ~~^~~
street_lamps.cpp: In function 'void update(int, int, int, int, int)':
street_lamps.cpp:33:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   33 |   int mid = l + r >> 1;
      |             ~~^~~
street_lamps.cpp: In function 'int query(int, int, int, int, int)':
street_lamps.cpp:40:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   40 |   int mid = l + r >> 1;
      |             ~~^~~
street_lamps.cpp: In function 'void put(int, int, int, int, int)':
street_lamps.cpp:50:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   50 |   int mid = l + r >> 1;
      |             ~~^~~
street_lamps.cpp: In function 'int dfsl(int, int, int)':
street_lamps.cpp:56:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   56 |   int m = l + r >> 1;
      |           ~~^~~
street_lamps.cpp: In function 'int dfsr(int, int, int)':
street_lamps.cpp:62:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   62 |   int m = l + r >> 1;
      |           ~~^~~
street_lamps.cpp: In function 'void upd(int, int, int)':
street_lamps.cpp:94:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |     for (int t = p; t <= vt[i].size(); t += t & -t) bit[i][t] += x;
      |                     ~~^~~~~~~~~~~~~~~
street_lamps.cpp: In function 'int32_t main()':
street_lamps.cpp:110:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |     freopen("duck.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:111:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |     freopen("duck.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 20312 KB Output is correct
2 Correct 5 ms 20316 KB Output is correct
3 Correct 5 ms 20440 KB Output is correct
4 Correct 5 ms 20316 KB Output is correct
5 Correct 6 ms 20568 KB Output is correct
6 Correct 6 ms 20316 KB Output is correct
7 Correct 6 ms 20312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 191 ms 33176 KB Output is correct
2 Correct 225 ms 31672 KB Output is correct
3 Correct 389 ms 33100 KB Output is correct
4 Correct 1016 ms 79876 KB Output is correct
5 Correct 907 ms 75064 KB Output is correct
6 Correct 1091 ms 83384 KB Output is correct
7 Correct 172 ms 45140 KB Output is correct
8 Correct 169 ms 46344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 20572 KB Output is correct
2 Correct 7 ms 20568 KB Output is correct
3 Correct 7 ms 20316 KB Output is correct
4 Correct 7 ms 20460 KB Output is correct
5 Correct 1768 ms 110636 KB Output is correct
6 Correct 1411 ms 97692 KB Output is correct
7 Correct 974 ms 74716 KB Output is correct
8 Correct 226 ms 46372 KB Output is correct
9 Correct 84 ms 23892 KB Output is correct
10 Correct 93 ms 24148 KB Output is correct
11 Correct 91 ms 24388 KB Output is correct
12 Correct 207 ms 45060 KB Output is correct
13 Correct 219 ms 46928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 20316 KB Output is correct
2 Correct 6 ms 20316 KB Output is correct
3 Correct 7 ms 20572 KB Output is correct
4 Correct 9 ms 20568 KB Output is correct
5 Correct 290 ms 46228 KB Output is correct
6 Correct 788 ms 69540 KB Output is correct
7 Correct 1145 ms 84036 KB Output is correct
8 Correct 1596 ms 101288 KB Output is correct
9 Correct 300 ms 30904 KB Output is correct
10 Correct 263 ms 39604 KB Output is correct
11 Correct 295 ms 31832 KB Output is correct
12 Correct 262 ms 41396 KB Output is correct
13 Correct 299 ms 31428 KB Output is correct
14 Correct 263 ms 40724 KB Output is correct
15 Correct 201 ms 44932 KB Output is correct
16 Correct 207 ms 46324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 20312 KB Output is correct
2 Correct 5 ms 20316 KB Output is correct
3 Correct 5 ms 20440 KB Output is correct
4 Correct 5 ms 20316 KB Output is correct
5 Correct 6 ms 20568 KB Output is correct
6 Correct 6 ms 20316 KB Output is correct
7 Correct 6 ms 20312 KB Output is correct
8 Correct 191 ms 33176 KB Output is correct
9 Correct 225 ms 31672 KB Output is correct
10 Correct 389 ms 33100 KB Output is correct
11 Correct 1016 ms 79876 KB Output is correct
12 Correct 907 ms 75064 KB Output is correct
13 Correct 1091 ms 83384 KB Output is correct
14 Correct 172 ms 45140 KB Output is correct
15 Correct 169 ms 46344 KB Output is correct
16 Correct 7 ms 20572 KB Output is correct
17 Correct 7 ms 20568 KB Output is correct
18 Correct 7 ms 20316 KB Output is correct
19 Correct 7 ms 20460 KB Output is correct
20 Correct 1768 ms 110636 KB Output is correct
21 Correct 1411 ms 97692 KB Output is correct
22 Correct 974 ms 74716 KB Output is correct
23 Correct 226 ms 46372 KB Output is correct
24 Correct 84 ms 23892 KB Output is correct
25 Correct 93 ms 24148 KB Output is correct
26 Correct 91 ms 24388 KB Output is correct
27 Correct 207 ms 45060 KB Output is correct
28 Correct 219 ms 46928 KB Output is correct
29 Correct 6 ms 20316 KB Output is correct
30 Correct 6 ms 20316 KB Output is correct
31 Correct 7 ms 20572 KB Output is correct
32 Correct 9 ms 20568 KB Output is correct
33 Correct 290 ms 46228 KB Output is correct
34 Correct 788 ms 69540 KB Output is correct
35 Correct 1145 ms 84036 KB Output is correct
36 Correct 1596 ms 101288 KB Output is correct
37 Correct 300 ms 30904 KB Output is correct
38 Correct 263 ms 39604 KB Output is correct
39 Correct 295 ms 31832 KB Output is correct
40 Correct 262 ms 41396 KB Output is correct
41 Correct 299 ms 31428 KB Output is correct
42 Correct 263 ms 40724 KB Output is correct
43 Correct 201 ms 44932 KB Output is correct
44 Correct 207 ms 46324 KB Output is correct
45 Correct 90 ms 27596 KB Output is correct
46 Correct 138 ms 26580 KB Output is correct
47 Correct 525 ms 47356 KB Output is correct
48 Correct 1051 ms 80824 KB Output is correct
49 Correct 101 ms 24404 KB Output is correct
50 Correct 102 ms 24400 KB Output is correct
51 Correct 100 ms 24560 KB Output is correct
52 Correct 96 ms 24996 KB Output is correct
53 Correct 97 ms 24916 KB Output is correct
54 Correct 98 ms 24916 KB Output is correct