Submission #522309

# Submission time Handle Problem Language Result Execution time Memory
522309 2022-02-04T14:55:33 Z SuffixAutomata Street Lamps (APIO19_street_lamps) C++17
100 / 100
4976 ms 243812 KB
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

// clang-format off

namespace io {
	const int __SIZE = (1 << 21) + 1;
	char ibuf[__SIZE], *iS, *iT, obuf[__SIZE], *oS = obuf, *oT = oS + __SIZE - 1, __c, qu[55]; int __f, qr, _eof;
	#define Gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	inline void flush () { fwrite (obuf, 1, oS - obuf, stdout), oS = obuf; }
	inline void gc (char &x) { x = Gc(); }
	inline void pc (char x) { *oS ++ = x; if (oS == oT) flush (); }
	inline void pstr (const char *s) { int __len = strlen(s); for (__f = 0; __f < __len; ++__f) pc (s[__f]); }
	inline void gstr (char *s) { for(__c = Gc(); __c < 32 || __c > 126 || __c == ' ';)  __c = Gc();
		for(; __c > 31 && __c < 127 && __c != ' '; ++s, __c = Gc()) *s = __c; *s = 0; }
	template <class I> inline bool gi (I &x) { _eof = 0;
		for (__f = 1, __c = Gc(); (__c < '0' || __c > '9') && !_eof; __c = Gc()) { if (__c == '-') __f = -1; _eof |= __c == EOF; }
		for (x = 0; __c <= '9' && __c >= '0' && !_eof; __c = Gc()) x = x * 10 + (__c & 15), _eof |= __c == EOF; x *= __f; return !_eof; }
	template <class I> inline void print (I x) { if (!x) pc ('0'); if (x < 0) pc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10; while (qr) pc (qu[qr --]); }
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
} using io::pc; using io::gc; using io::pstr; using io::gstr; using io::gi; using io::print;

// clang-format on

#define all(a) a.begin(), a.end()
#define fi first
#define se second
#define pb push_back
#define mp make_pair

using ll = long long;
using pii = pair<int, int>;
//#define int ll

const int MOD = 1000000007;

int n;
int trr[300005];
struct lin {
  int k, b;
  const lin operator+(const lin &o) const { return {k + o.k, b + o.b}; }
} s[15 << 20];
int lc[15 << 20], rc[15 << 20];
int _stt = 1;

void modif(int &idx, int u, lin d, int l = 0, int r = n + 1) {
  if (!(l <= u && u < r))
    return;
  if (!idx)
    idx = _stt++;
  if (r - l == 1) {
    s[idx] = s[idx] + d;
    return;
  }
  modif(lc[idx], u, d, l, (l + r) / 2);
  modif(rc[idx], u, d, (l + r) / 2, r);
  s[idx] = s[lc[idx]] + s[rc[idx]];
}
lin nsbasket(int idx, int L, int R, int l = 0, int r = n + 1) {
  if (!idx || R <= l || r <= L)
    return {0, 0};
  if (L <= l && r <= R)
    return s[idx];
  return nsbasket(lc[idx], L, R, l, (l + r) / 2) +
         nsbasket(rc[idx], L, R, (l + r) / 2, r);
}

void add(int i, int j, lin r) {
  i++;
  while (i <= n + 2) {
    modif(trr[i], j, r);
    i += (i & (-i));
  }
}
lin qry(int i, int j) {
  i++;
  lin ans = {0, 0};
  while (i) {
    ans = ans + nsbasket(trr[i], 0, j + 1);
    i -= (i & (-i));
  }
  return ans;
}

int T;
void toggle(int l, int r, int f) {
  if (l > r)
    return;
  lin yes = {1, -T}, no = {-1, T};
  if (f == -1)
    swap(yes, no);
  add(l, l, yes), add(l, r + 1, no), add(r + 1, l, no), add(r + 1, r + 1, yes);
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int q;
  gi(n), gi(q);
  static char s[300005];
  gstr(s);
  set<int> zp;
  zp.insert(-1), zp.insert(n);
  for (int i = 0; i < n; i++)
    if (s[i] == '0')
      zp.insert(i);
  {
    auto s = zp.begin();
    while (s != zp.end()) {
      if (*s != -1)
        toggle(*prev(s) + 1, *s - 1, 1);
      s = next(s);
    }
  }
  for (T = 1; T <= q; T++) {
    gstr(s);
    if (s[0] == 't') {
      int i;
      gi(i);
      auto q = zp.find(--i);
      if (q != zp.end()) {
        toggle(*prev(q) + 1, i - 1, -1);
        toggle(i + 1, *next(q) - 1, -1);
        toggle(*prev(q) + 1, *next(q) - 1, 1);
        zp.erase(q);
      } else {
        q = zp.insert(i).first;
        toggle(*prev(q) + 1, *next(q) - 1, -1);
        toggle(*prev(q) + 1, i - 1, 1);
        toggle(i + 1, *next(q) - 1, 1);
      }
    } else {
      int l, r;
      gi(l), gi(r);
      lin ans = qry(l - 1, r - 2);
      print(T * ans.k + ans.b), pc('\n');
    }
  }
}

Compilation message

street_lamps.cpp: In function 'void io::gstr(char*)':
street_lamps.cpp:16:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   16 |   for(; __c > 31 && __c < 127 && __c != ' '; ++s, __c = Gc()) *s = __c; *s = 0; }
      |   ^~~
street_lamps.cpp:16:73: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   16 |   for(; __c > 31 && __c < 127 && __c != ' '; ++s, __c = Gc()) *s = __c; *s = 0; }
      |                                                                         ^
street_lamps.cpp: In function 'bool io::gi(I&)':
street_lamps.cpp:19:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   19 |   for (x = 0; __c <= '9' && __c >= '0' && !_eof; __c = Gc()) x = x * 10 + (__c & 15), _eof |= __c == EOF; x *= __f; return !_eof; }
      |   ^~~
street_lamps.cpp:19:107: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   19 |   for (x = 0; __c <= '9' && __c >= '0' && !_eof; __c = Gc()) x = x * 10 + (__c & 15), _eof |= __c == EOF; x *= __f; return !_eof; }
      |                                                                                                           ^
street_lamps.cpp: In function 'void io::print(I)':
street_lamps.cpp:21:3: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
   21 |   while (x) qu[++ qr] = x % 10 + '0',  x /= 10; while (qr) pc (qu[qr --]); }
      |   ^~~~~
street_lamps.cpp:21:49: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
   21 |   while (x) qu[++ qr] = x % 10 + '0',  x /= 10; while (qr) pc (qu[qr --]); }
      |                                                 ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 313 ms 4284 KB Output is correct
2 Correct 449 ms 4544 KB Output is correct
3 Correct 957 ms 8524 KB Output is correct
4 Correct 3096 ms 186052 KB Output is correct
5 Correct 3193 ms 187676 KB Output is correct
6 Correct 3495 ms 191876 KB Output is correct
7 Correct 89 ms 17924 KB Output is correct
8 Correct 35 ms 6564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 844 KB Output is correct
2 Correct 5 ms 844 KB Output is correct
3 Correct 3 ms 716 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 4895 ms 243812 KB Output is correct
6 Correct 4165 ms 234932 KB Output is correct
7 Correct 2834 ms 191716 KB Output is correct
8 Correct 40 ms 12580 KB Output is correct
9 Correct 21 ms 6808 KB Output is correct
10 Correct 22 ms 7244 KB Output is correct
11 Correct 23 ms 7568 KB Output is correct
12 Correct 93 ms 23788 KB Output is correct
13 Correct 42 ms 12568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 588 KB Output is correct
2 Correct 2 ms 588 KB Output is correct
3 Correct 4 ms 716 KB Output is correct
4 Correct 5 ms 664 KB Output is correct
5 Correct 679 ms 137716 KB Output is correct
6 Correct 1951 ms 171076 KB Output is correct
7 Correct 3338 ms 190716 KB Output is correct
8 Correct 4976 ms 205856 KB Output is correct
9 Correct 623 ms 6484 KB Output is correct
10 Correct 553 ms 5496 KB Output is correct
11 Correct 644 ms 6436 KB Output is correct
12 Correct 544 ms 5436 KB Output is correct
13 Correct 611 ms 6468 KB Output is correct
14 Correct 543 ms 5576 KB Output is correct
15 Correct 91 ms 23972 KB Output is correct
16 Correct 37 ms 12492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 313 ms 4284 KB Output is correct
9 Correct 449 ms 4544 KB Output is correct
10 Correct 957 ms 8524 KB Output is correct
11 Correct 3096 ms 186052 KB Output is correct
12 Correct 3193 ms 187676 KB Output is correct
13 Correct 3495 ms 191876 KB Output is correct
14 Correct 89 ms 17924 KB Output is correct
15 Correct 35 ms 6564 KB Output is correct
16 Correct 5 ms 844 KB Output is correct
17 Correct 5 ms 844 KB Output is correct
18 Correct 3 ms 716 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 4895 ms 243812 KB Output is correct
21 Correct 4165 ms 234932 KB Output is correct
22 Correct 2834 ms 191716 KB Output is correct
23 Correct 40 ms 12580 KB Output is correct
24 Correct 21 ms 6808 KB Output is correct
25 Correct 22 ms 7244 KB Output is correct
26 Correct 23 ms 7568 KB Output is correct
27 Correct 93 ms 23788 KB Output is correct
28 Correct 42 ms 12568 KB Output is correct
29 Correct 1 ms 588 KB Output is correct
30 Correct 2 ms 588 KB Output is correct
31 Correct 4 ms 716 KB Output is correct
32 Correct 5 ms 664 KB Output is correct
33 Correct 679 ms 137716 KB Output is correct
34 Correct 1951 ms 171076 KB Output is correct
35 Correct 3338 ms 190716 KB Output is correct
36 Correct 4976 ms 205856 KB Output is correct
37 Correct 623 ms 6484 KB Output is correct
38 Correct 553 ms 5496 KB Output is correct
39 Correct 644 ms 6436 KB Output is correct
40 Correct 544 ms 5436 KB Output is correct
41 Correct 611 ms 6468 KB Output is correct
42 Correct 543 ms 5576 KB Output is correct
43 Correct 91 ms 23972 KB Output is correct
44 Correct 37 ms 12492 KB Output is correct
45 Correct 79 ms 5172 KB Output is correct
46 Correct 199 ms 5056 KB Output is correct
47 Correct 1386 ms 68396 KB Output is correct
48 Correct 2860 ms 190508 KB Output is correct
49 Correct 19 ms 7236 KB Output is correct
50 Correct 16 ms 7068 KB Output is correct
51 Correct 19 ms 7236 KB Output is correct
52 Correct 29 ms 7572 KB Output is correct
53 Correct 19 ms 7708 KB Output is correct
54 Correct 20 ms 7648 KB Output is correct