답안 #966247

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
966247 2024-04-19T15:10:17 Z steveonalex 가로등 (APIO19_street_lamps) C++17
100 / 100
2217 ms 111164 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
 
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
 
ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b){a = b; return true;}
        return false;
    }
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b){a = b; return true;}
        return false;
    }
template <class T>
    void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){
        for(auto i: a) out << i << separator;
        out << finish;
    }
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

struct FenwickTree{
    int n;
    vector<vector<int>> a, val;

    FenwickTree(int _n){
        n = _n;
        a.resize(n+1);
        val.resize(n+1);
    }

    void fake_update(int i, int j){
        while(i <= n){
            val[i].push_back(j);
            i += LASTBIT(i);
        }
    }

    void build_tree(){
        for(int i = 1; i<=n; ++i){
            val[i].push_back(0);
            remove_dup(val[i]);
            a[i].resize(val[i].size());
        }
    }

    void update(int i, int j, int v){
        while(i <= n){
            int idx = lower_bound(ALL(val[i]), j) - val[i].begin();
            while(idx < val[i].size()){
                a[i][idx] += v;
                idx += LASTBIT(idx);
            }
            i += LASTBIT(i);
        }
    }

    int get(int i, int j){
        int ans = 0;
        while(i > 0){
            int idx = upper_bound(ALL(val[i]), j) - val[i].begin() - 1;
            while(idx > 0){
                ans += a[i][idx];
                idx -= LASTBIT(idx);
            }
            i -= LASTBIT(i);
        }
        return ans;
    }
};


const int N = 3e5 + 69;
int a[N], b[N];

void fake_add(int i, set<int> &S, vector<pair<int, int>> &range){
    int l = -1, r = -1;
    auto it = S.upper_bound(i);
    if (it != S.end()) r = *it;
    if (it != S.begin()){
        it--;
        l = *it;
    }
    S.insert(i);
    if (l > -1) range.push_back({l, i});
    if (r > -1) range.push_back({i, r});
}

void fake_del(int i, set<int> &S, vector<pair<int, int>> &range){
    S.erase(i);
    int l = -1, r = -1;
    auto it = S.upper_bound(i);
    if (it != S.end()) r = *it;
    if (it != S.begin()){
        it--;
        l = *it;
    }
    if (l > -1 && r > -1) range.push_back({l, r});
}

void add_sigma(int i, int j, set<int> &S, FenwickTree &bit){
    int l = -1, r = -1;
    auto it = S.upper_bound(j);
    if (it != S.end()) r = *it;
    if (it != S.begin()){
        it--;
        l = *it;
    }
    S.insert(j);

    if (l > -1 && r > -1) bit.update(r, l, i);
    if (l > -1) bit.update(j, l, -i);
    if (r > -1) bit.update(r, j, -i);
}

void del_sigma(int i, int j, set<int> &S, FenwickTree &bit){
    S.erase(j);
    int l = -1, r = -1;
    auto it = S.upper_bound(j);
    if (it != S.end()) r = *it;
    if (it != S.begin()){
        it--;
        l = *it;
    }
    cout.flush();

    if (l > -1) bit.update(j, l, i);
    if (r > -1) bit.update(r, j, i);
    if (l > -1 && r > -1) bit.update(r, l, -i);
}

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    // freopen("input.inp", "r", stdin);

    int n, q; cin >> n >> q;
    string s; cin >> s;
    for(int i= 1; i<=n; ++i) a[i+1] = (s[i-1] == '1');
    n++;

    vector<vector<int>> vt;
    for(int i = 1; i<=q; ++i){
        string type; cin >> type;
        if (type == "query"){
            int l, r; cin >> l >> r;
            l++;
            vt.push_back({2, l, r});
        }
        else{
            int j; cin >> j;
            j++;
            vt.push_back({1, j});
        }
    }

    set<int> S;
    vector<pair<int, int>> range;
    for(int i = 1; i<=n; ++i) if (a[i] == 0) fake_add(i, S, range);

    for(int i = 1; i<=n; ++i) b[i] = a[i];

    for(int i = 0; i<q; ++i){
        if (vt[i][0] == 1){
            int j = vt[i][1];
            if (b[j] == 1) fake_add(j, S, range);
            else fake_del(j, S, range);
            b[j] ^= 1;
        }
    }

    FenwickTree bit(n);
    for(pair<int, int> i: range) bit.fake_update(i.second, i.first);
    bit.build_tree();

    S.clear();
    for(int i = 1; i<=n; ++i) if (a[i] == 0) S.insert(i);

    for(int i = 1; i<=n; ++i) b[i] = a[i];
    for(int i= 0; i<q; ++i){
        if (vt[i][0] == 1){
            int j = vt[i][1];
            if (b[j] == 1){
                add_sigma(i+1, j, S, bit);
            }
            else{
                del_sigma(i+1, j, S, bit);
            }
            b[j] ^= 1;
        }
        else{
            int l = vt[i][1], r = vt[i][2];
            int ans = bit.get(r, l-1) - bit.get(l-1, l-1);
            auto it = S.lower_bound(l);
            if (it != S.end() && (*it) <= r) ans += i+1;

            ans = (i + 1) - ans;

            cout << ans << "\n";
        }
    }
 
    return 0;
}

Compilation message

street_lamps.cpp: In member function 'void FenwickTree::update(int, int, int)':
street_lamps.cpp:74:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |             while(idx < val[i].size()){
      |                   ~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 266 ms 31808 KB Output is correct
2 Correct 324 ms 30696 KB Output is correct
3 Correct 489 ms 35644 KB Output is correct
4 Correct 1560 ms 93400 KB Output is correct
5 Correct 1270 ms 81412 KB Output is correct
6 Correct 1521 ms 95168 KB Output is correct
7 Correct 911 ms 101996 KB Output is correct
8 Correct 222 ms 61996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2648 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 2 ms 2648 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 2217 ms 111164 KB Output is correct
6 Correct 1880 ms 97132 KB Output is correct
7 Correct 1279 ms 81240 KB Output is correct
8 Correct 276 ms 60180 KB Output is correct
9 Correct 94 ms 19372 KB Output is correct
10 Correct 99 ms 27892 KB Output is correct
11 Correct 92 ms 26368 KB Output is correct
12 Correct 1158 ms 101056 KB Output is correct
13 Correct 258 ms 60488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2648 KB Output is correct
2 Correct 2 ms 2648 KB Output is correct
3 Correct 2 ms 2780 KB Output is correct
4 Correct 3 ms 2656 KB Output is correct
5 Correct 918 ms 78856 KB Output is correct
6 Correct 1246 ms 87100 KB Output is correct
7 Correct 1630 ms 95256 KB Output is correct
8 Correct 2116 ms 104096 KB Output is correct
9 Correct 294 ms 32748 KB Output is correct
10 Correct 230 ms 36208 KB Output is correct
11 Correct 300 ms 32928 KB Output is correct
12 Correct 227 ms 37100 KB Output is correct
13 Correct 289 ms 32892 KB Output is correct
14 Correct 238 ms 36028 KB Output is correct
15 Correct 1179 ms 101828 KB Output is correct
16 Correct 270 ms 62336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2648 KB Output is correct
8 Correct 266 ms 31808 KB Output is correct
9 Correct 324 ms 30696 KB Output is correct
10 Correct 489 ms 35644 KB Output is correct
11 Correct 1560 ms 93400 KB Output is correct
12 Correct 1270 ms 81412 KB Output is correct
13 Correct 1521 ms 95168 KB Output is correct
14 Correct 911 ms 101996 KB Output is correct
15 Correct 222 ms 61996 KB Output is correct
16 Correct 3 ms 2648 KB Output is correct
17 Correct 2 ms 2652 KB Output is correct
18 Correct 2 ms 2648 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 2217 ms 111164 KB Output is correct
21 Correct 1880 ms 97132 KB Output is correct
22 Correct 1279 ms 81240 KB Output is correct
23 Correct 276 ms 60180 KB Output is correct
24 Correct 94 ms 19372 KB Output is correct
25 Correct 99 ms 27892 KB Output is correct
26 Correct 92 ms 26368 KB Output is correct
27 Correct 1158 ms 101056 KB Output is correct
28 Correct 258 ms 60488 KB Output is correct
29 Correct 2 ms 2648 KB Output is correct
30 Correct 2 ms 2648 KB Output is correct
31 Correct 2 ms 2780 KB Output is correct
32 Correct 3 ms 2656 KB Output is correct
33 Correct 918 ms 78856 KB Output is correct
34 Correct 1246 ms 87100 KB Output is correct
35 Correct 1630 ms 95256 KB Output is correct
36 Correct 2116 ms 104096 KB Output is correct
37 Correct 294 ms 32748 KB Output is correct
38 Correct 230 ms 36208 KB Output is correct
39 Correct 300 ms 32928 KB Output is correct
40 Correct 227 ms 37100 KB Output is correct
41 Correct 289 ms 32892 KB Output is correct
42 Correct 238 ms 36028 KB Output is correct
43 Correct 1179 ms 101828 KB Output is correct
44 Correct 270 ms 62336 KB Output is correct
45 Correct 116 ms 18336 KB Output is correct
46 Correct 164 ms 20464 KB Output is correct
47 Correct 694 ms 43328 KB Output is correct
48 Correct 1615 ms 93268 KB Output is correct
49 Correct 108 ms 28156 KB Output is correct
50 Correct 106 ms 27120 KB Output is correct
51 Correct 110 ms 28044 KB Output is correct
52 Correct 115 ms 27636 KB Output is correct
53 Correct 115 ms 26624 KB Output is correct
54 Correct 112 ms 26876 KB Output is correct