Submission #917710

# Submission time Handle Problem Language Result Execution time Memory
917710 2024-01-28T16:27:47 Z duckindog Street Lamps (APIO19_street_lamps) C++14
20 / 100
500 ms 524288 KB
//from duckindog wth depression
#include<bits/stdc++.h>

using namespace std;

const int N = 3e5 + 10;
int n, q;
bool a[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];

int f[4 * N];
void build(int s, int l, int 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] = min(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] = min(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 min(query(s * 2, l, mid, u, v), query(s * 2 + 1, mid + 1, r, u, v));
}
int get(int x, bool w) {
  int ret = x;
  int l, r;
  if (w) l = x, r = n;
  else l = 1, r = x;

  while (l <= r) {
    int mid = l + r >> 1;
    bool s = (w == 1 ? query(1, 1, n, x, mid) : query(1, 1, n, mid, x));

    if (s) {
      if (w) l = (ret = mid) + 1;
      else r = (ret = mid) - 1;
    } else {
      if (w) r = mid - 1;
      else l = mid + 1;
    }
  }
  return ret;

}

vector<int> bit[N], vt[N];
void upd(int i, int j, int x) {
  for (; i; i -= i & -i) {
    for (int t = j; t <= n; t += t & -t) bit[i][t] += x;
  }
}
int que(int i, int j) {
  int ret = 0;
  for (; i <= n; i += i & -i) {
    for (int t = j; 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] = (x == '1');
  }

  vector<pair<int, int>> updlist;
  for (int i = 1; i <= q; ++i) {
    string ty; cin >> ty;
    if (ty == "toggle") {
      int x; cin >> x;
      Q[i] = {0, x, 0};
      updlist.push_back({x, x});
    } else {
      int x, y; cin >> x >> y;
      updlist.push_back({x, y - 1});
      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(n + 5);
  }

  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) : x), r = (a[x + 1] == 1 ? get(x + 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:21:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   21 |   int mid = l + r >> 1;
      |             ~~^~~
street_lamps.cpp: In function 'void update(int, int, int, int, int)':
street_lamps.cpp:31:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   31 |   int mid = l + r >> 1;
      |             ~~^~~
street_lamps.cpp: In function 'int query(int, int, int, int, int)':
street_lamps.cpp:38:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   38 |   int mid = l + r >> 1;
      |             ~~^~~
street_lamps.cpp: In function 'int get(int, bool)':
street_lamps.cpp:48:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   48 |     int mid = l + r >> 1;
      |               ~~^~~
street_lamps.cpp: In function 'int32_t main()':
street_lamps.cpp:81:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |     freopen("duck.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:82:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |     freopen("duck.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 18780 KB Output is correct
2 Correct 5 ms 18520 KB Output is correct
3 Correct 5 ms 18524 KB Output is correct
4 Correct 5 ms 18524 KB Output is correct
5 Correct 5 ms 18700 KB Output is correct
6 Correct 5 ms 18572 KB Output is correct
7 Correct 5 ms 18524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 168 ms 25852 KB Output is correct
2 Correct 235 ms 28360 KB Output is correct
3 Correct 500 ms 124188 KB Output is correct
4 Runtime error 338 ms 524288 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 22620 KB Output is correct
2 Correct 11 ms 22620 KB Output is correct
3 Correct 12 ms 22624 KB Output is correct
4 Correct 11 ms 22620 KB Output is correct
5 Runtime error 318 ms 524288 KB Execution killed with signal 9
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 22620 KB Output is correct
2 Correct 10 ms 22660 KB Output is correct
3 Correct 12 ms 22656 KB Output is correct
4 Correct 11 ms 22616 KB Output is correct
5 Runtime error 336 ms 524288 KB Execution killed with signal 9
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 18780 KB Output is correct
2 Correct 5 ms 18520 KB Output is correct
3 Correct 5 ms 18524 KB Output is correct
4 Correct 5 ms 18524 KB Output is correct
5 Correct 5 ms 18700 KB Output is correct
6 Correct 5 ms 18572 KB Output is correct
7 Correct 5 ms 18524 KB Output is correct
8 Correct 168 ms 25852 KB Output is correct
9 Correct 235 ms 28360 KB Output is correct
10 Correct 500 ms 124188 KB Output is correct
11 Runtime error 338 ms 524288 KB Execution killed with signal 9
12 Halted 0 ms 0 KB -