답안 #1044897

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1044897 2024-08-05T14:23:09 Z efedmrlr 가로등 (APIO19_street_lamps) C++17
0 / 100
155 ms 14328 KB
// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>

using namespace std;


#define lli long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int i = 0; (i) < (n); (i)++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()


void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}


const double EPS = 0.00001;
const int INF = 1e9+500;
const int N = 3e5+5;
const int ALPH = 26;
const int LGN = 25;
constexpr int MOD = 1e9+7;
int n,m,q;

struct SegT {
    vector<int> data, tag;
    int sz;
    void reset(int s) {
        sz = s;
        data.assign(4 * sz + 5, 0);
        tag.assign(4 * sz + 5, 0);
    }
    void push(int v) {
        tag[v << 1] += tag[v];
        tag[v << 1 ^ 1] += tag[v];

        data[v << 1] += tag[v];
        data[v << 1 ^ 1] += tag[v];
        tag[v] = 0;
    }
    void update(int tl, int tr, int v, int l, int r, int val) {
        if(tl >= l && tr <= r) {
            data[v] += val;
            tag[v] += val;
            return;
        }
        if(tl > r || tr < l) return;
        push(v);
        int tm = (tl + tr) >> 1;
        update(tl, tm, v << 1, l, r, val);
        update(tm + 1, tr, v << 1 ^ 1, l, r, val);
        data[v] = data[v << 1] + data[v << 1 ^ 1];
    }
    void update(int l, int r, int val) {
        update(0, sz, 1, l, r, val);
    }
    int query(int tl, int tr, int v, int l, int r) {
        if(tl >= l && tr <= r) {
            return data[v];
        }
        if(tl > r || tr < l) return 0;
        push(v);
        int tm = (tl + tr) >> 1;
        return query(tl, tm, v << 1, l, r) + query(tm + 1, tr, v << 1 ^ 1, l, r);
    }
    int query(int l, int r) {
        return query(0, sz, 1, l, r);
    }
};


inline void solve() {
    cin >> n >> q;
    string s;
    cin >> s;
    s = '$' + s;
    vector<vector<array<int, 2> > > upd(n + 2, vector<array<int, 2> >()); // (]
    vector<array<int, 3> > que;
    vector<int> tm(n + 2, 0);
    SegT st;
    st.reset(q + 2);
    for(int z = 1; z <= q; z++) {
        string t;
        cin >> t;
        if(t == "toggle") {
            int i;
            cin >> i;
            if(s[i] == '0') {
                tm[i] = z;
                s[i] = '1';
            }
            else {
                upd[i].pb({tm[i], z});
                s[i] = '0';
            }
        }   
        else {
            int a, b;
            cin >> a >> b;
            que.pb({a, b, z});
        }
    } 
    for(int i = 1; i <= n; i++) {
        if(s[i] == '1') {
            upd[i].pb({tm[i], q});
        }
    }
    // for(int i = 1; i <= n; i++) {
    //     cout << "i:" << i << "\n\n";
    //     auto &x = upd[i];
    //     for(auto &c : x) {
    //         cout << c[0] << " " << c[1] << "\n";
    //     }
    // }
    sort(all(que));
    vector<int> ans(q + 1, -1);
    int id = 0;
    for(int i = 1; i <= n; i++) {

        for(auto el : upd[i]) {
            st.update(el[0] + 1, el[1], 1);
        }

        while(id < que.size() && que[id][0] <= i) {
            ans[que[id][2]] = st.query(1, que[id][2]);
            id++;
        }

        for(auto &el : upd[i]) {
            st.update(el[0] + 1, el[1], -1);
        }
    }
    for(int i = 0; i <= q; i++) {
        if(ans[i] == -1) continue;
        cout << ans[i] << "\n";
    }


}
 
signed main() {

    fastio();
    int test = 1;
    //cin>>test;
    while(test--) {
        solve();
    }
    
}

Compilation message

street_lamps.cpp: In function 'void solve()':
street_lamps.cpp:129:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |         while(id < que.size() && que[id][0] <= i) {
      |               ~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 155 ms 14328 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -