Submission #551828

# Submission time Handle Problem Language Result Execution time Memory
551828 2022-04-21T16:43:20 Z Jarif_Rahman Street Lamps (APIO19_street_lamps) C++17
100 / 100
1596 ms 47768 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

struct BIT{
    int n;
    vector<ll> sm;
    BIT(int _n){
        n = _n;
        sm.resize(n);
    }
    void add(int in, ll x){
        in++;
        while(in <= n) sm[in-1]+=x, in+=in&-in;
    }
    ll sum(int in){
        in++;
        ll s = 0;
        while(in >= 1) s+=sm[in-1], in-=in&-in;
        return s;
    }
    ll sum(int l, int r){
        return sum(r)-(l == 0? 0:sum(l-1));
    }
};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, q; cin >> n >> q;
    str s; cin >> s;

    set<tuple<int, int, int>> segments;
    vector<tuple<int, int, int, int>> queries;
    vector<ll> ans;
    vector<int> qqtt;

    int ls = 0;
    for(int i = 0; i < n; i++){
        if(s[i] == '0'){
            if(ls < i){
                segments.insert({ls, i-1, 0});
                queries.pb({ls, 1, i-1, 0});
            }
            ls = i+1;
        }
    }
    if(ls != n){
        segments.insert({ls, n-1, 0});
        queries.pb({ls, 1, n-1, 0});
    }

    for(int qq = 1; qq <= q; qq++){
        str tt; cin >> tt;
        if(tt == "toggle"){
            int i; cin >> i; i--;
            if(s[i] == '0'){
                auto it = segments.lower_bound(make_tuple(i, 0, 0));
                if(it != segments.end() && get<0>(*it) == i+1 &&
                    it != segments.begin() && get<1>(*prev(it)) == i-1){
                    auto [l, r0, t1] = *prev(it);
                    auto [l0, r, t2] = *it;

                    it = segments.erase(prev(it));
                    segments.erase(it);
                    queries.pb({l, 2, r0, t1});
                    queries.pb({l0, 2, r, t2});

                    queries.pb({l, 0, r0, qq-t1});
                    queries.pb({l0, 0, r, qq-t2});

                    segments.insert({l, r, qq});
                    queries.pb({l, 1, r, qq});
                }
                else if(it != segments.end() && get<0>(*it) == i+1){
                    auto [l, r, t] = *it;

                    segments.erase(it);
                    queries.pb({l, 2, r, t});

                    queries.pb({l, 0, r, qq-t});

                    segments.insert({l-1, r, qq});
                    queries.pb({l-1, 1, r, qq});
                }
                else if(it != segments.begin() && get<1>(*prev(it)) == i-1){
                    it = prev(it);
                    auto [l, r, t] = *it;

                    segments.erase(it);
                    queries.pb({l, 2, r, t});

                    queries.pb({l, 0, r, qq-t});

                    segments.insert({l, r+1, qq});
                    queries.pb({l, 1, r+1, qq});
                }
                else{
                    segments.insert({i, i, qq});
                    queries.pb({i, 1, i, qq});
                }
                s[i] = '1';
            }
            else{
                auto it = segments.lower_bound(make_tuple(i, -1, -1));
                if(it == segments.end() || get<0>(*it) != i) it--;
                auto [l, r, t] = *it;

                segments.erase(it);
                queries.pb({l, 2, r, t});

                queries.pb({l, 0, r, qq-t});

                if(l != i) segments.insert({l, i-1, qq}), queries.pb({l, 1, i-1, qq});
                if(r != i) segments.insert({i+1, r, qq}), queries.pb({i+1, 1, r, qq});
                s[i] = '0';
            }
        }
        else{
            int a, b; cin >> a >> b; a--, b-=2;
            queries.pb({a, 3, b, ans.size()});
            ans.pb(0);
            qqtt.pb(qq);
        }
    }

    BIT bit1(n), bit2(n), bit3(n);

    function<void(int, int)> cdq_dnc = [&](int l, int r){
        if(l == r) return;
        int md = (l+r)/2;
        cdq_dnc(l, md);
        cdq_dnc(md+1, r);
        vector<tuple<int, int, int, int>> sth;
        for(int i = l; i <= md; i++) if(get<1>(queries[i]) != 3) sth.pb(queries[i]);  
        for(int i = md+1; i <= r; i++) if(get<1>(queries[i]) == 3) sth.pb(queries[i]);
        sort(sth.begin(), sth.end());

        for(auto [l, t, r, c]: sth){
            if(t == 0) bit1.add(r, c);
            else if(t == 1) bit2.add(r, 1), bit3.add(r, c);
            else if(t == 2) bit2.add(r, -1), bit3.add(r, -c);
            else{
                ans[c]+=bit1.sum(r, n-1);
                ans[c]+=bit2.sum(r, n-1)*qqtt[c]-bit3.sum(r, n-1);
            }
        }
        for(auto [l, t, r, c]: sth){
            if(t == 0) bit1.add(r, -c);
            else if(t == 1) bit2.add(r, -1), bit3.add(r, -c);
            else if(t == 2) bit2.add(r, 1), bit3.add(r, c);
        }
    };

    cdq_dnc(0, int(queries.size())-1);

    for(ll x: ans) cout << x << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 856 ms 26720 KB Output is correct
2 Correct 905 ms 26316 KB Output is correct
3 Correct 940 ms 30328 KB Output is correct
4 Correct 1227 ms 45232 KB Output is correct
5 Correct 1305 ms 45732 KB Output is correct
6 Correct 1267 ms 46768 KB Output is correct
7 Correct 440 ms 29912 KB Output is correct
8 Correct 485 ms 31240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 468 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1564 ms 43548 KB Output is correct
6 Correct 1553 ms 46248 KB Output is correct
7 Correct 1352 ms 44960 KB Output is correct
8 Correct 531 ms 31296 KB Output is correct
9 Correct 206 ms 15176 KB Output is correct
10 Correct 239 ms 19736 KB Output is correct
11 Correct 231 ms 19876 KB Output is correct
12 Correct 473 ms 29804 KB Output is correct
13 Correct 474 ms 31256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 640 ms 41168 KB Output is correct
6 Correct 1078 ms 45068 KB Output is correct
7 Correct 1301 ms 46300 KB Output is correct
8 Correct 1596 ms 47768 KB Output is correct
9 Correct 1018 ms 31960 KB Output is correct
10 Correct 1157 ms 32392 KB Output is correct
11 Correct 1000 ms 32016 KB Output is correct
12 Correct 1147 ms 32352 KB Output is correct
13 Correct 1010 ms 32008 KB Output is correct
14 Correct 1138 ms 32348 KB Output is correct
15 Correct 473 ms 30004 KB Output is correct
16 Correct 460 ms 31364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 856 ms 26720 KB Output is correct
9 Correct 905 ms 26316 KB Output is correct
10 Correct 940 ms 30328 KB Output is correct
11 Correct 1227 ms 45232 KB Output is correct
12 Correct 1305 ms 45732 KB Output is correct
13 Correct 1267 ms 46768 KB Output is correct
14 Correct 440 ms 29912 KB Output is correct
15 Correct 485 ms 31240 KB Output is correct
16 Correct 3 ms 468 KB Output is correct
17 Correct 3 ms 468 KB Output is correct
18 Correct 3 ms 468 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1564 ms 43548 KB Output is correct
21 Correct 1553 ms 46248 KB Output is correct
22 Correct 1352 ms 44960 KB Output is correct
23 Correct 531 ms 31296 KB Output is correct
24 Correct 206 ms 15176 KB Output is correct
25 Correct 239 ms 19736 KB Output is correct
26 Correct 231 ms 19876 KB Output is correct
27 Correct 473 ms 29804 KB Output is correct
28 Correct 474 ms 31256 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
31 Correct 3 ms 468 KB Output is correct
32 Correct 3 ms 468 KB Output is correct
33 Correct 640 ms 41168 KB Output is correct
34 Correct 1078 ms 45068 KB Output is correct
35 Correct 1301 ms 46300 KB Output is correct
36 Correct 1596 ms 47768 KB Output is correct
37 Correct 1018 ms 31960 KB Output is correct
38 Correct 1157 ms 32392 KB Output is correct
39 Correct 1000 ms 32016 KB Output is correct
40 Correct 1147 ms 32352 KB Output is correct
41 Correct 1010 ms 32008 KB Output is correct
42 Correct 1138 ms 32348 KB Output is correct
43 Correct 473 ms 30004 KB Output is correct
44 Correct 460 ms 31364 KB Output is correct
45 Correct 497 ms 17456 KB Output is correct
46 Correct 549 ms 17836 KB Output is correct
47 Correct 670 ms 23292 KB Output is correct
48 Correct 1216 ms 44852 KB Output is correct
49 Correct 284 ms 21032 KB Output is correct
50 Correct 289 ms 20924 KB Output is correct
51 Correct 283 ms 21064 KB Output is correct
52 Correct 282 ms 21436 KB Output is correct
53 Correct 296 ms 21456 KB Output is correct
54 Correct 289 ms 21396 KB Output is correct