Submission #1038097

# Submission time Handle Problem Language Result Execution time Memory
1038097 2024-07-29T12:55:12 Z vladilius Street Lamps (APIO19_street_lamps) C++17
100 / 100
880 ms 243928 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define ins insert
const int inf = numeric_limits<int> :: max();
 
struct FT{
    vector<int> bit;
    int n;
    void init(int ns){
        n = ns;
        bit.resize(n + 1);
    }
    int get(int v){
        int out = 0;
        while (v > 0){
            out += bit[v];
            v = (v & (v + 1)) - 1;
        }
        return out;
    }
    void upd(int v, int k){
        while (v <= n){
            bit[v] += k;
            v |= (v + 1);
        }
    }
    void updr(int l, int r, int k){
        if (l > r) return;
        upd(l, k);
        upd(r + 1, -k);
    }
};
 
struct ST{
    struct node{
        vector<int> x;
        int lg;
        FT T;
    };
    vector<node> t;
    vector<vector<int>> mp;
    int n, q;
    ST(int ns, int qs){
        n = ns; q = qs;
        t.resize(4 * n);
        for (int i = 1; i < t.size(); i++){
            t[i].x.pb(0);
        }
        mp.resize(q + 1);
        int lg = log2(n) + 5;
        for (int i = 1; i <= q; i++){
            mp[i].resize(lg + 1);
        }
        bld(1, 1, n, 0);
    }
    void bld(int v, int tl, int tr, int k){
        t[v].lg = k++;
        if (tl == tr) return;
        int tm = (tl + tr) / 2, vv = 2 * v;
        bld(vv, tl, tm, k);
        bld(vv + 1, tm + 1, tr, k);
    }
    vector<int> :: iterator it1, it2;
    void upd(int v, int tl, int tr, int& l, int& r, int& lr, int& rr, int& x){
        if (l > tr || r < tl) return;
        if (l <= tl && tr <= r){
            it1 = lower_bound(t[v].x.begin(), t[v].x.end(), lr);
            it2 = upper_bound(t[v].x.begin(), t[v].x.end(), rr);
            if (it1 == it2) return;
            it2--;
            int l1 = (int) (it1 - t[v].x.begin()), r1 = (int) (it2 - t[v].x.begin());
            t[v].T.updr(max(1, l1), r1, x);
            return;
        }
        int tm = (tl + tr) / 2, vv = 2 * v;
        upd(vv, tl, tm, l, r, lr, rr, x);
        upd(vv + 1, tm + 1, tr, l, r, lr, rr, x);
    }
    void upd(int l1, int r1, int l2, int r2, int x){
        upd(1, 1, n, l1, r1, l2, r2, x);
        // cout<<x<<" -> "<<l1<<" "<<r1<<" "<<l2<<" "<<r2<<"\n";
    }
    int get(int v, int tl, int tr, int& l, int& i){
        int out = t[v].T.get(mp[i][t[v].lg]);
        if (tl == tr){
            return out;
        }
        int tm = (tl + tr) / 2, vv = 2 * v;
        if (l <= tm){
            return out + get(vv, tl, tm, l, i);
        }
        return out + get(vv + 1, tm + 1, tr, l, i);
    }
    int get(int l, int i){
        return get(1, 1, n, l, i);
    }
    void add(int v, int tl, int tr, int& l, int& r, int& i){
        t[v].x.pb(r);
        mp[i][t[v].lg] = (int) t[v].x.size() - 1;
        if (tl == tr) return;
        int tm = (tl + tr) / 2, vv = 2 * v;
        if (l <= tm){
            add(vv, tl, tm, l, r, i);
        }
        else {
            add(vv + 1, tm + 1, tr, l, r, i);
        }
    }
    void add(int l, int r, int i){
        add(1, 1, n, l, r, i);
    }
    void build(){
        for (int i = 1; i < t.size(); i++){
            t[i].T.init((int) t[i].x.size() - 1);
        }
    }
};
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n, q; cin>>n>>q;
    string s; cin>>s; s = '#' + s;
    vector<pii> qs = {{}};
    vector<int> rr;
    for (int qq = 1; qq <= q; qq++){
        string type; cin>>type;
        if (type == "toggle"){
            int k; cin>>k;
            qs.pb({k, 0});
        }
        else {
            int l, r; cin>>l>>r;
            qs.pb({l, --r});
            rr.pb(qq);
        }
    }
    
    auto cmp = [&](int i, int j){
        if (qs[i].ss == qs[j].ss){
            return qs[i].ff < qs[j].ff;
        }
        return qs[i].ss < qs[j].ss;
    };
    sort(rr.begin(), rr.end(), cmp);
    
    ST T(n, q);
    for (int i: rr){
        T.add(qs[i].ff, qs[i].ss, i);
    }
    T.build();
    
    set<pii> st;
    int i = 1;
    while (i <= n){
        if (s[i] == '0'){
            i++;
            continue;
        }
        int j = i;
        while (j <= n && s[j] == '1'){
            j++;
        }
        st.ins({i, j - 1});
        i = j;
    }
 
    for (int i = 1; i <= q; i++){
        auto& [a, b] = qs[i];
        if (!b){
            if (s[a] == '0'){
                st.ins({a, a});
                auto it = st.find({a, a});
                vector<pii> add;
                if (it != st.begin()){
                    auto it1 = prev(it);
                    auto [l, r] = *it1;
                    if (r == (a - 1)){
                        add.pb({l, r});
                    }
                }
                if (it != prev(st.end())){
                    auto it1 = next(it);
                    auto [l, r] = *it1;
                    if (l == (a + 1)){
                        add.pb({l, r});
                    }
                }
                if (add.size() == 2){
                    st.erase(it);
                    for (auto p: add) st.erase(p);
                    pii R = {add[0].ff, add[1].ss};
                    T.upd(R.ff, a, a, R.ss, -i);
                    st.ins(R);
                }
                else if (add.size() == 1){
                    st.erase(it);
                    for (auto p: add) st.erase(p);
                    pii R = ((add[0].ff > a) ? make_pair(a, add[0].ss) : make_pair(add[0].ff, a));
                    T.upd(R.ff, a, a, R.ss, -i);
                    st.ins(R);
                }
                else {
                    T.upd(a, a, a, a, -i);
                }
                s[a] = '1';
            }
            else {
                auto it = st.lower_bound({a + 1, -inf}); it--;
                auto [l, r] = *it;
                T.upd(l, a, a, r, i);
                st.erase(it);
                if (l < a) st.ins({l, a - 1});
                if (a < r) st.ins({a + 1, r});
                s[a] = '0';
            }
        }
        else {
            int out = T.get(qs[i].ff, i);
            if (!st.empty()){
                auto it = st.lower_bound({a + 1, -inf});
                if (it != st.begin()){
                    it--;
                    if ((*it).ff <= a && b <= (*it).ss){
                        out += i;
                    }
                }
            }
            cout<<out<<"\n";
        }
    }
}

Compilation message

street_lamps.cpp: In constructor 'ST::ST(int, int)':
street_lamps.cpp:53:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<ST::node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |         for (int i = 1; i < t.size(); i++){
      |                         ~~^~~~~~~~~~
street_lamps.cpp: In member function 'void ST::build()':
street_lamps.cpp:120:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<ST::node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |         for (int i = 1; i < t.size(); i++){
      |                         ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 173 ms 40956 KB Output is correct
2 Correct 204 ms 44024 KB Output is correct
3 Correct 261 ms 56888 KB Output is correct
4 Correct 747 ms 220080 KB Output is correct
5 Correct 749 ms 224200 KB Output is correct
6 Correct 646 ms 214148 KB Output is correct
7 Correct 766 ms 241860 KB Output is correct
8 Correct 667 ms 242604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1112 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 1096 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 528 ms 198572 KB Output is correct
6 Correct 679 ms 209312 KB Output is correct
7 Correct 769 ms 223920 KB Output is correct
8 Correct 784 ms 240932 KB Output is correct
9 Correct 174 ms 41028 KB Output is correct
10 Correct 201 ms 45880 KB Output is correct
11 Correct 172 ms 46696 KB Output is correct
12 Correct 790 ms 240164 KB Output is correct
13 Correct 871 ms 242028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1112 KB Output is correct
2 Correct 2 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 870 ms 243928 KB Output is correct
6 Correct 880 ms 228876 KB Output is correct
7 Correct 714 ms 215468 KB Output is correct
8 Correct 491 ms 197812 KB Output is correct
9 Correct 216 ms 37308 KB Output is correct
10 Correct 175 ms 29688 KB Output is correct
11 Correct 204 ms 36628 KB Output is correct
12 Correct 145 ms 30108 KB Output is correct
13 Correct 217 ms 37596 KB Output is correct
14 Correct 177 ms 29192 KB Output is correct
15 Correct 853 ms 241068 KB Output is correct
16 Correct 833 ms 240708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 173 ms 40956 KB Output is correct
9 Correct 204 ms 44024 KB Output is correct
10 Correct 261 ms 56888 KB Output is correct
11 Correct 747 ms 220080 KB Output is correct
12 Correct 749 ms 224200 KB Output is correct
13 Correct 646 ms 214148 KB Output is correct
14 Correct 766 ms 241860 KB Output is correct
15 Correct 667 ms 242604 KB Output is correct
16 Correct 1 ms 1112 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 1096 KB Output is correct
19 Correct 1 ms 1116 KB Output is correct
20 Correct 528 ms 198572 KB Output is correct
21 Correct 679 ms 209312 KB Output is correct
22 Correct 769 ms 223920 KB Output is correct
23 Correct 784 ms 240932 KB Output is correct
24 Correct 174 ms 41028 KB Output is correct
25 Correct 201 ms 45880 KB Output is correct
26 Correct 172 ms 46696 KB Output is correct
27 Correct 790 ms 240164 KB Output is correct
28 Correct 871 ms 242028 KB Output is correct
29 Correct 2 ms 1112 KB Output is correct
30 Correct 2 ms 1116 KB Output is correct
31 Correct 1 ms 1116 KB Output is correct
32 Correct 1 ms 860 KB Output is correct
33 Correct 870 ms 243928 KB Output is correct
34 Correct 880 ms 228876 KB Output is correct
35 Correct 714 ms 215468 KB Output is correct
36 Correct 491 ms 197812 KB Output is correct
37 Correct 216 ms 37308 KB Output is correct
38 Correct 175 ms 29688 KB Output is correct
39 Correct 204 ms 36628 KB Output is correct
40 Correct 145 ms 30108 KB Output is correct
41 Correct 217 ms 37596 KB Output is correct
42 Correct 177 ms 29192 KB Output is correct
43 Correct 853 ms 241068 KB Output is correct
44 Correct 833 ms 240708 KB Output is correct
45 Correct 114 ms 20800 KB Output is correct
46 Correct 149 ms 26620 KB Output is correct
47 Correct 330 ms 90812 KB Output is correct
48 Correct 664 ms 220072 KB Output is correct
49 Correct 193 ms 50744 KB Output is correct
50 Correct 217 ms 51000 KB Output is correct
51 Correct 169 ms 52028 KB Output is correct
52 Correct 230 ms 58688 KB Output is correct
53 Correct 206 ms 57612 KB Output is correct
54 Correct 208 ms 58160 KB Output is correct