Submission #1005565

# Submission time Handle Problem Language Result Execution time Memory
1005565 2024-06-22T15:42:38 Z underwaterkillerwhale Street Lamps (APIO19_street_lamps) C++17
60 / 100
142 ms 20452 KB
//#ifdef ONLINE_JUDGE
//    #include "cyberland.h"
//#endif

#include <bits/stdc++.h>
#define se              second
#define fs              first
#define mp              make_pair
#define pb              push_back
#define ll              long long
#define ii              pair<ll,ll>
#define ld              long double
#define SZ(v)           (int)v.size()
#define ALL(v)          v.begin(), v.end()
#define bit(msk, i)     ((msk >> i) & 1)
#define iter(id, v)     for(auto id : v)
#define rep(i,m,n)      for(int i=(m); i<=(n); i++)
#define reb(i,m,n)      for(int i=(m); i>=(n); i--)

using namespace std;

mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }

const int N  = 3e5 + 2;
const ll Mod = 998244353;
const int szBL = 50;
const int INF = 1e9 + 7;
const int BASE = 137;

struct Query {
    int typ, u, v;
};

int n, Q;
int a[N], b[N];
Query qr[N];

namespace sub1 {
    bool check (int L, int R) {
        rep (i, L, R - 1) if (a[i] == 0) return 0;
        return 1;
    }
    void solution() {
        rep (i, 1, Q) {
            if (qr[i].typ == 0) {
                a[qr[i].u] ^= 1;
            }
            else {
                int L = qr[i].u, R = qr[i].v;
                int res = 0;
                reb (t, i, 1) {
                    if (qr[t].typ == 0) a[qr[t].u] ^= 1;
                    if (check(L, R)) {
                        ++res;
                    }
                }
                rep (t, 1, i) if (qr[t].typ == 0)  a[qr[t].u] ^= 1;
                cout << res <<"\n";
            }
        }
    }
}
namespace sub2 {
    int cnt[N];
    bool check () {
        rep (i, 1, Q) if (qr[i].typ == 1 && (qr[i].u != qr[i].v - 1)) return 0;
        return 1;
    }
    void solution() {
        rep (i, 1, n) a[i] = b[i];
        rep (i, 1, Q) {
            if (qr[i].typ == 0) {
                int pos = qr[i].u;
                a[pos] ^= 1;
                if (a[pos] == 0) cnt[pos] += i;
                else cnt[pos] -= i;
            }
            else {
                int pos = qr[i].u;
                int res = cnt[pos];
                if (a[pos] == 1) res += i;
                cout << res <<"\n";
            }
        }
    }
}
namespace sub3 {
    bool check () {
        rep (i ,1, n) a[i] = b[i];
        rep (i, 1, Q) if (qr[i].typ == 0 && a[qr[i].u] == 0) return 0; else a[qr[i].u] ^= 1;
        return 1;
    }
    struct Segment_Tree {
        int Range;
        int st[N << 2];

        void init (int n) {
            Range = n;
        }
        void build (int id, int l, int r) {
            if (l== r) {
                st[id] = a[l] == 1 ? 0 : INF;
                return;
            }
            int mid = l + r >> 1;
            build (id << 1, l, mid);
            build (id << 1 | 1, mid + 1, r);
            st[id] = max(st[id << 1], st[id << 1 | 1]);
        }
        void update (int id, int l, int r, int pos, int val) {
            if (l > pos || r < pos) {
                return;
            }
            if (l == r) {
                st[id] = val;
                return;
            }
            int mid = l + r >> 1;
            update (id << 1, l, mid, pos, val);
            update (id << 1 | 1, mid + 1, r, pos, val);
            st[id] = max(st[id << 1], st[id << 1 | 1]);
        }
        int get (int id, int l, int r, int u, int v) {
            if (l > v || r < u) return 0;
            if (l >= u && r <= v) return st[id];
            int mid = l+ r >> 1;
            return max(get (id << 1, l, mid, u, v), get (id << 1 | 1, mid + 1, r, u, v));
        }
        void update (int u, int v) {
            return update (1, 1, Range, u, v);
        }
        int get (int L, int R) {
            return get (1, 1, Range, L, R);
        }
    }ST;

    void solution() {
        rep (i, 1, n) a[i] = b[i];
        ST.init(n);
        ST.build(1, 1, n);
        rep (i ,1, Q) {
            if (qr[i].typ == 0) ST.update(qr[i].u, i);
            else {
                if (ST.get(qr[i].u, qr[i].v - 1) == INF) cout << 0 <<"\n";
                else cout << i - ST.get(qr[i].u, qr[i].v - 1) <<"\n";
            }
        }
    }
}
void solution() {
    cin >> n >> Q;
    string s; cin >> s;
    rep (i, 1, n) a[i] = s[i - 1] - '0', b[i] = a[i];
    rep (i, 1, Q) {
        string s;
        cin >> s;
        if (s == "toggle") cin >> qr[i].u, qr[i].typ = 0;
        else cin >> qr[i].u >> qr[i].v, qr[i].typ = 1;
    }
    if (n <= 100 && Q <= 100)
        sub1 :: solution();
    else
        if (sub2 :: check())
        sub2 :: solution();
    else
        sub3 :: solution();
}
#define file(name) freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);
int main () {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//    file ("PAINT");
    int num_Test = 1;
//    cin >> num_Test;
    while (num_Test--)
        solution();
}

/*
5 7
11011
query 1 2
query 1 2
query 1 2
query 3 4
toggle 3
query 3 4
query 1 2



3 2
0 1 5
0 2 5
1
1 2

*/

Compilation message

street_lamps.cpp: In member function 'void sub3::Segment_Tree::build(int, int, int)':
street_lamps.cpp:106:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  106 |             int mid = l + r >> 1;
      |                       ~~^~~
street_lamps.cpp: In member function 'void sub3::Segment_Tree::update(int, int, int, int, int)':
street_lamps.cpp:119:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  119 |             int mid = l + r >> 1;
      |                       ~~^~~
street_lamps.cpp: In member function 'int sub3::Segment_Tree::get(int, int, int, int, int)':
street_lamps.cpp:127:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  127 |             int mid = l+ r >> 1;
      |                       ~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 7508 KB Output is correct
2 Correct 44 ms 7508 KB Output is correct
3 Correct 44 ms 7504 KB Output is correct
4 Correct 50 ms 8864 KB Output is correct
5 Correct 49 ms 9188 KB Output is correct
6 Correct 59 ms 8932 KB Output is correct
7 Correct 58 ms 8872 KB Output is correct
8 Correct 61 ms 10212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 70 ms 12456 KB Output is correct
6 Correct 89 ms 12656 KB Output is correct
7 Correct 110 ms 12772 KB Output is correct
8 Correct 108 ms 20452 KB Output is correct
9 Correct 46 ms 10324 KB Output is correct
10 Correct 49 ms 10324 KB Output is correct
11 Correct 48 ms 10548 KB Output is correct
12 Correct 106 ms 18916 KB Output is correct
13 Correct 142 ms 20372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Incorrect 1 ms 4444 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 46 ms 7508 KB Output is correct
9 Correct 44 ms 7508 KB Output is correct
10 Correct 44 ms 7504 KB Output is correct
11 Correct 50 ms 8864 KB Output is correct
12 Correct 49 ms 9188 KB Output is correct
13 Correct 59 ms 8932 KB Output is correct
14 Correct 58 ms 8872 KB Output is correct
15 Correct 61 ms 10212 KB Output is correct
16 Correct 1 ms 4440 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 70 ms 12456 KB Output is correct
21 Correct 89 ms 12656 KB Output is correct
22 Correct 110 ms 12772 KB Output is correct
23 Correct 108 ms 20452 KB Output is correct
24 Correct 46 ms 10324 KB Output is correct
25 Correct 49 ms 10324 KB Output is correct
26 Correct 48 ms 10548 KB Output is correct
27 Correct 106 ms 18916 KB Output is correct
28 Correct 142 ms 20372 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Incorrect 1 ms 4444 KB Output isn't correct
31 Halted 0 ms 0 KB -