답안 #1038086

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1038086 2024-07-29T12:52:12 Z vladilius 가로등 (APIO19_street_lamps) C++17
100 / 100
793 ms 248044 KB
#include <bits/stdc++.h>
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:51:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<ST::node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |         for (int i = 1; i < t.size(); i++){
      |                         ~~^~~~~~~~~~
street_lamps.cpp: In member function 'void ST::build()':
street_lamps.cpp:118:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<ST::node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |         for (int i = 1; i < t.size(); i++){
      |                         ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 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 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 168 ms 41724 KB Output is correct
2 Correct 189 ms 43816 KB Output is correct
3 Correct 246 ms 56300 KB Output is correct
4 Correct 621 ms 220480 KB Output is correct
5 Correct 709 ms 223584 KB Output is correct
6 Correct 565 ms 213940 KB Output is correct
7 Correct 666 ms 241164 KB Output is correct
8 Correct 669 ms 242608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 2 ms 988 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 2 ms 1116 KB Output is correct
5 Correct 525 ms 201520 KB Output is correct
6 Correct 691 ms 214292 KB Output is correct
7 Correct 723 ms 229288 KB Output is correct
8 Correct 751 ms 244916 KB Output is correct
9 Correct 137 ms 43840 KB Output is correct
10 Correct 157 ms 49224 KB Output is correct
11 Correct 155 ms 49424 KB Output is correct
12 Correct 793 ms 246180 KB Output is correct
13 Correct 762 ms 248044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1116 KB Output is correct
2 Correct 1 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 774 ms 242868 KB Output is correct
6 Correct 717 ms 234920 KB Output is correct
7 Correct 600 ms 218980 KB Output is correct
8 Correct 465 ms 201908 KB Output is correct
9 Correct 182 ms 39612 KB Output is correct
10 Correct 135 ms 33532 KB Output is correct
11 Correct 178 ms 40900 KB Output is correct
12 Correct 135 ms 32752 KB Output is correct
13 Correct 182 ms 41028 KB Output is correct
14 Correct 144 ms 31848 KB Output is correct
15 Correct 749 ms 246572 KB Output is correct
16 Correct 734 ms 246704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 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 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 168 ms 41724 KB Output is correct
9 Correct 189 ms 43816 KB Output is correct
10 Correct 246 ms 56300 KB Output is correct
11 Correct 621 ms 220480 KB Output is correct
12 Correct 709 ms 223584 KB Output is correct
13 Correct 565 ms 213940 KB Output is correct
14 Correct 666 ms 241164 KB Output is correct
15 Correct 669 ms 242608 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 2 ms 988 KB Output is correct
18 Correct 1 ms 1116 KB Output is correct
19 Correct 2 ms 1116 KB Output is correct
20 Correct 525 ms 201520 KB Output is correct
21 Correct 691 ms 214292 KB Output is correct
22 Correct 723 ms 229288 KB Output is correct
23 Correct 751 ms 244916 KB Output is correct
24 Correct 137 ms 43840 KB Output is correct
25 Correct 157 ms 49224 KB Output is correct
26 Correct 155 ms 49424 KB Output is correct
27 Correct 793 ms 246180 KB Output is correct
28 Correct 762 ms 248044 KB Output is correct
29 Correct 1 ms 1116 KB Output is correct
30 Correct 1 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 774 ms 242868 KB Output is correct
34 Correct 717 ms 234920 KB Output is correct
35 Correct 600 ms 218980 KB Output is correct
36 Correct 465 ms 201908 KB Output is correct
37 Correct 182 ms 39612 KB Output is correct
38 Correct 135 ms 33532 KB Output is correct
39 Correct 178 ms 40900 KB Output is correct
40 Correct 135 ms 32752 KB Output is correct
41 Correct 182 ms 41028 KB Output is correct
42 Correct 144 ms 31848 KB Output is correct
43 Correct 749 ms 246572 KB Output is correct
44 Correct 734 ms 246704 KB Output is correct
45 Correct 85 ms 22596 KB Output is correct
46 Correct 121 ms 28620 KB Output is correct
47 Correct 306 ms 93888 KB Output is correct
48 Correct 664 ms 224640 KB Output is correct
49 Correct 177 ms 55620 KB Output is correct
50 Correct 180 ms 54328 KB Output is correct
51 Correct 180 ms 56896 KB Output is correct
52 Correct 196 ms 61200 KB Output is correct
53 Correct 193 ms 60952 KB Output is correct
54 Correct 182 ms 61240 KB Output is correct