Submission #1099517

# Submission time Handle Problem Language Result Execution time Memory
1099517 2024-10-11T14:07:45 Z Zero_OP Street Lamps (APIO19_street_lamps) C++14
100 / 100
614 ms 35556 KB
#include <bits/stdc++.h>

using namespace std;

#define rep(i, l, r) for(int i = (l), _r = (r); i < _r; ++i)
#define FOR(i, l, r) for(int i = (l), _r = (r); i <= _r; ++i)
#define ROF(i, r, l) for(int i = (r), _l = (l); i >= _l; --i)
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define sz(v) (int)v.size()
#define dbg(x) "[" #x " = " << (x) << "]"

template<typename T>
bool minimize(T& a, const T& b){
    if(a > b) return a = b, true; return false;
}

template<typename T>
bool maximize(T& a, const T& b){
    if(a < b) return a = b, true; return false;
}

using ll = long long;
using ld = long double;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T random_int(T l, T r){ return uniform_int_distribution<T>(l, r)(rng); }
template<typename T> T random_real(T l, T r){ return uniform_real_distribution<T>(l, r)(rng); }

const int MAX = 3e5 + 5;
const int inf = 1e9 + 9;

int n, q;
string s;
set<pair<int, int>> onSegments;
ll ans[MAX];

struct event{
    int t, type, l, r; //t is order of query
};

struct Fenwick{
    vector<int> bit_cnt;
    vector<ll> bit_sum;

    Fenwick(int n) : bit_cnt(n + 1, 0), bit_sum(n + 1, 0) {}

    void upd(int id, int cnt, int sum){
        for(; id > 0; id -= id & (-id)){
            bit_cnt[id] += cnt;
            bit_sum[id] += sum;
        }
    }

    pair<int, ll> qry(int id){
        int total_cnt = 0;
        ll total_sum = 0;
        for(; id < sz(bit_cnt); id += id & (-id)){
            total_cnt += bit_cnt[id];
            total_sum += bit_sum[id];
        }
        return {total_cnt, total_sum};
    }
};

void cdq(int l, int r, vector<event>& events, Fenwick& fenwick){
    if(l >= r) return;
    int mid = l + r >> 1;

    vector<int> upds, qrys;

    FOR(i, l, mid) if(events[i].type != 0) upds.push_back(i);
    FOR(i, mid + 1, r) if(events[i].type == 0) qrys.push_back(i);

//    cout << "conquer : " << l << ' ' << r << '\n';

    if(upds.empty() || qrys.empty());
    else{
        sort(all(upds), [&](int i, int j){ return events[i].l < events[j].l; });
        sort(all(qrys), [&](int i, int j){ return events[i].l < events[j].l; });

        vector<tuple<int, int, int>> saves;
        int j = 0;
        rep(i, 0, sz(qrys)){
            while(j < sz(upds) && events[upds[j]].l <= events[qrys[i]].l){
                fenwick.upd(events[upds[j]].r, events[upds[j]].type, events[upds[j]].t * events[upds[j]].type);
                saves.push_back({events[upds[j]].r, events[upds[j]].type, events[upds[j]].t * events[upds[j]].type});
//                cout << saves.back().first << ' ' << saves.back().second << '\n';
                ++j;
            }

            int num_cnt; ll num_sum;
            tie(num_cnt, num_sum) = fenwick.qry(events[qrys[i]].r);
            ans[events[qrys[i]].t] += 1LL * num_cnt * events[qrys[i]].t - num_sum;
//            cout << dbg(events[qrys[i]].t) << dbg(num_cnt) << ' ' << num_sum << '\n';
        }

         for(auto [pos, cnt, sum] : saves){
            fenwick.upd(pos, -cnt, -sum);
         }
    }

//    cout << "end layer\n\n";

    cdq(l, mid, events, fenwick);
    cdq(mid + 1, r, events, fenwick);
}

void testcase(){
    cin >> n >> q >> s;
    s = '?' + s;

    vector<event> events;
    FOR(i, 1, n){
        if(s[i] == '1'){
            int j = i;
            while(j < n && s[j + 1] == '1') ++j;

            onSegments.insert({i, j});
            events.push_back({0, +1, i, j});
            i = j;
        }
    }

    vector<int> queries;
    FOR(t, 1, q){
        string type;
        cin >> type;
        if(type == "toggle"){
            int i;
            cin >> i;

            if(s[i] == '1'){ //off
                auto it = onSegments.upper_bound({i, inf}); --it;
                int x, y;
                tie(x, y) = *it;

                onSegments.erase(it);
                events.push_back({t, -1, x, y});

                if(s[i - 1] == '1'){
                    onSegments.insert({x, i - 1});
                    events.push_back({t, +1, x, i - 1});
                }

                if(s[i + 1] == '1'){
                    onSegments.insert({i + 1, y});
                    events.push_back({t, +1, i + 1, y});
                }

                s[i] = '0';
            } else{ //on
                int x = i, y = i;

                if(s[i - 1] == '1'){
                    auto it = onSegments.upper_bound({i - 1, inf}); --it;
                    x = it -> first;
                    events.push_back({t, -1, x, i - 1});
                    onSegments.erase(it);
                }

                if(s[i + 1] == '1'){
                    auto it = onSegments.upper_bound({i + 1, inf}); --it;
                    y = it -> second;
                    events.push_back({t, -1, i + 1, y});
                    onSegments.erase(it);
                }

                events.push_back({t, +1, x, y});
                onSegments.insert({x, y});

                s[i] = '1';
            }
        } else{
            int l, r;
            cin >> l >> r;
            --r;

            events.push_back({t, 0, l, r});
            queries.push_back(t);
        }
    }

//    for(auto it : events){
//        cout << dbg(it.t) << dbg(it.l) << dbg(it.r) << dbg(it.type) << '\n';
//    }

    Fenwick fenwick(n);
    cdq(0, sz(events) - 1, events, fenwick);

    for(int i : queries){
        cout << ans[i] << '\n';
    }


}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    #define filename "task"
    if(fopen(filename".inp", "r")){
        freopen(filename".inp", "r", stdin);
        freopen(filename".out", "w", stdout);
    }

    int T = 1; //cin >> T;
    while(T--) testcase();

    return 0;
}

Compilation message

street_lamps.cpp: In function 'bool minimize(T&, const T&)':
street_lamps.cpp:15:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   15 |     if(a > b) return a = b, true; return false;
      |     ^~
street_lamps.cpp:15:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   15 |     if(a > b) return a = b, true; return false;
      |                                   ^~~~~~
street_lamps.cpp: In function 'bool maximize(T&, const T&)':
street_lamps.cpp:20:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   20 |     if(a < b) return a = b, true; return false;
      |     ^~
street_lamps.cpp:20:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   20 |     if(a < b) return a = b, true; return false;
      |                                   ^~~~~~
street_lamps.cpp: In function 'void cdq(int, int, std::vector<event>&, Fenwick&)':
street_lamps.cpp:68:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   68 |     int mid = l + r >> 1;
      |               ~~^~~
street_lamps.cpp:98:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   98 |          for(auto [pos, cnt, sum] : saves){
      |                   ^
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:204:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  204 |         freopen(filename".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:205:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  205 |         freopen(filename".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 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 348 ms 20176 KB Output is correct
2 Correct 374 ms 20928 KB Output is correct
3 Correct 434 ms 21644 KB Output is correct
4 Correct 542 ms 31688 KB Output is correct
5 Correct 556 ms 30756 KB Output is correct
6 Correct 300 ms 35340 KB Output is correct
7 Correct 90 ms 18628 KB Output is correct
8 Correct 121 ms 22176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 504 ms 34020 KB Output is correct
6 Correct 614 ms 35556 KB Output is correct
7 Correct 544 ms 29604 KB Output is correct
8 Correct 133 ms 22176 KB Output is correct
9 Correct 87 ms 12192 KB Output is correct
10 Correct 97 ms 14068 KB Output is correct
11 Correct 91 ms 14312 KB Output is correct
12 Correct 90 ms 18596 KB Output is correct
13 Correct 135 ms 22432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 171 ms 28796 KB Output is correct
6 Correct 250 ms 31136 KB Output is correct
7 Correct 296 ms 34768 KB Output is correct
8 Correct 349 ms 34444 KB Output is correct
9 Correct 170 ms 20916 KB Output is correct
10 Correct 160 ms 23964 KB Output is correct
11 Correct 172 ms 21000 KB Output is correct
12 Correct 166 ms 24060 KB Output is correct
13 Correct 172 ms 21020 KB Output is correct
14 Correct 166 ms 23960 KB Output is correct
15 Correct 88 ms 18680 KB Output is correct
16 Correct 138 ms 22188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 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 348 ms 20176 KB Output is correct
9 Correct 374 ms 20928 KB Output is correct
10 Correct 434 ms 21644 KB Output is correct
11 Correct 542 ms 31688 KB Output is correct
12 Correct 556 ms 30756 KB Output is correct
13 Correct 300 ms 35340 KB Output is correct
14 Correct 90 ms 18628 KB Output is correct
15 Correct 121 ms 22176 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 504 ms 34020 KB Output is correct
21 Correct 614 ms 35556 KB Output is correct
22 Correct 544 ms 29604 KB Output is correct
23 Correct 133 ms 22176 KB Output is correct
24 Correct 87 ms 12192 KB Output is correct
25 Correct 97 ms 14068 KB Output is correct
26 Correct 91 ms 14312 KB Output is correct
27 Correct 90 ms 18596 KB Output is correct
28 Correct 135 ms 22432 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 171 ms 28796 KB Output is correct
34 Correct 250 ms 31136 KB Output is correct
35 Correct 296 ms 34768 KB Output is correct
36 Correct 349 ms 34444 KB Output is correct
37 Correct 170 ms 20916 KB Output is correct
38 Correct 160 ms 23964 KB Output is correct
39 Correct 172 ms 21000 KB Output is correct
40 Correct 166 ms 24060 KB Output is correct
41 Correct 172 ms 21020 KB Output is correct
42 Correct 166 ms 23960 KB Output is correct
43 Correct 88 ms 18680 KB Output is correct
44 Correct 138 ms 22188 KB Output is correct
45 Correct 185 ms 12748 KB Output is correct
46 Correct 209 ms 12748 KB Output is correct
47 Correct 303 ms 16808 KB Output is correct
48 Correct 524 ms 31372 KB Output is correct
49 Correct 96 ms 15348 KB Output is correct
50 Correct 101 ms 15344 KB Output is correct
51 Correct 99 ms 15596 KB Output is correct
52 Correct 93 ms 15852 KB Output is correct
53 Correct 93 ms 15856 KB Output is correct
54 Correct 95 ms 15684 KB Output is correct