답안 #706080

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
706080 2023-03-05T20:26:24 Z fatemetmhr 가로등 (APIO19_street_lamps) C++17
100 / 100
2758 ms 251176 KB
// ~ Be Name Khoda ~

#include <bits/stdc++.h>
//#pragma GCC optimize ("Ofast")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,O3")

using namespace std;

typedef long long ll;

#define pb       push_back
#define mp       make_pair
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second

const int maxn  =  1e6   + 10;
const int maxn5 =  3e5   + 10;
const int maxnt =  1.2e6 + 10;
const int maxn3 =  1e3   + 10;
const int mod   =  1e9   +  7;
const ll  inf   =  1e18;

struct segment_tree{

    vector <int> lazy;
    int n;

    inline void add(int l, int r, int lq, int rq, int val, int v){
        if(rq <= l || r <= lq)
            return;
        if(lq <= l && r <= rq){
            lazy[v] += val;
            return;
        }
        int mid = (l + r) >> 1;
        add(l, mid, lq, rq, val, v * 2);
        add(mid, r, lq, rq, val, v * 2 + 1);
    }

    inline int get(int l, int r, int id, int v){
        if(r - l == 1)
            return lazy[v];
        int mid = (l + r) >> 1;
        if(id < mid)
            return get(l, mid, id, v * 2) + lazy[v];
        return get(mid, r, id, v * 2 + 1) + lazy[v];
    }

} seg[maxnt];

set <int> av;
string ty[maxn5];
int x[maxn5], y[maxn5];
vector <int> have[maxnt], req[maxn5];

void build(int l, int r, int v){
    if(r - l == 1){
        for(auto u : req[l])
            have[v].pb(u);
        sort(all(have[v]));
    }
    else{
        int mid = (l + r) >> 1;
        build(l, mid, v * 2);
        build(mid, r, v * 2 + 1);
        int pt1 = 0, pt2 = 0;
        while(pt1 < have[v * 2].size() || pt2 < have[v * 2 + 1].size()){
            if(pt1 < have[v * 2].size() && (pt2 == have[v * 2 + 1].size() || have[v * 2][pt1] < have[v * 2 + 1][pt2]))
                have[v].pb(have[v * 2][pt1++]);
            else
                have[v].pb(have[v * 2 + 1][pt2++]);
        }
    }
    have[v].resize(unique(all(have[v])) - have[v].begin());
    seg[v].n = have[v].size();
    seg[v].lazy.resize((seg[v].n << 2) + 5);
}

void add(int l, int r, int lq, int rq, int lq2, int rq2, int val, int v){
    if(rq <= l || r <= lq)
        return;
    if(lq <= l && r <= rq){
        int ptl = lower_bound(all(have[v]), lq2) - have[v].begin();
        int ptr = lower_bound(all(have[v]), rq2) - have[v].begin();
        if(ptl < ptr)
            seg[v].add(0, seg[v].n, ptl, ptr, val, 1);
        return;
    }
    int mid = (l + r) >> 1;
    add(l, mid, lq, rq, lq2, rq2, val, v * 2);
    add(mid, r, lq, rq, lq2, rq2, val, v * 2 + 1);
}

int get(int l, int r, int id1, int id2, int v){
    int pt = lower_bound(all(have[v]), id2) - have[v].begin();
    int ret = 0;
    if(pt < have[v].size() && have[v][pt] == id2)
        ret += seg[v].get(0, seg[v].n, pt, 1);
    if(r - l > 1){
        int mid = (l + r) >> 1;
        if(id1 < mid)
            ret += get(l, mid, id1, id2, v * 2);
        else
            ret += get(mid, r, id1, id2, v * 2 + 1);
    }
    return ret;
}

inline bool is_ok(int l, int r){
    auto it = av.lower_bound(l);
    if((*it) <= r)
        return false;
    return true;
}

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

    int n, q; cin >> n >> q;
    string s; cin >> s;
    for(int i = 0; i < q; i++){
        cin >> ty[i] >> x[i];
        x[i]--;
        if(ty[i] == "query"){
            cin >> y[i];
            y[i] -= 2;
            req[x[i]].pb(y[i]);
        }
    }
    build(0, n, 1);
    int last = 0;
    for(int i = 0; i < n; i++) if(s[i] == '0'){
        av.insert(i);
        if(last < i)
            add(0, n, last, i, last, i, q, 1);
        last = i + 1;
    }
    if(last < n)
        add(0, n, last, n, last, n, q, 1);
    av.insert(-1);
    av.insert(n);
    for(int i = 0; i < q; i++){
        if(ty[i] == "query")
            cout << get(0, n, x[i], y[i], 1) + (is_ok(x[i], y[i]) ? -1 : 0) * (q - i - 1) << '\n';
        else{
            bool cur = av.find(x[i]) == av.end();
            av.erase(x[i]);
            auto it = av.lower_bound(x[i]);
            int r = *it;
            it--;
            int l = *it;
            add(0, n, l + 1, x[i] + 1, x[i], r, (cur ? -1 : 1) * (q - i - 1), 1);
            if(cur)
                av.insert(x[i]);
        }
    }
}

Compilation message

street_lamps.cpp: In function 'void build(int, int, int)':
street_lamps.cpp:69:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |         while(pt1 < have[v * 2].size() || pt2 < have[v * 2 + 1].size()){
      |               ~~~~^~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:69:47: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |         while(pt1 < have[v * 2].size() || pt2 < have[v * 2 + 1].size()){
      |                                           ~~~~^~~~~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:70:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |             if(pt1 < have[v * 2].size() && (pt2 == have[v * 2 + 1].size() || have[v * 2][pt1] < have[v * 2 + 1][pt2]))
      |                ~~~~^~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:70:49: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |             if(pt1 < have[v * 2].size() && (pt2 == have[v * 2 + 1].size() || have[v * 2][pt1] < have[v * 2 + 1][pt2]))
      |                                             ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp: In function 'int get(int, int, int, int, int)':
street_lamps.cpp:99:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |     if(pt < have[v].size() && have[v][pt] == id2)
      |        ~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 82380 KB Output is correct
2 Correct 41 ms 82500 KB Output is correct
3 Correct 40 ms 82512 KB Output is correct
4 Correct 40 ms 82456 KB Output is correct
5 Correct 42 ms 82508 KB Output is correct
6 Correct 38 ms 82516 KB Output is correct
7 Correct 39 ms 82508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 231 ms 90148 KB Output is correct
2 Correct 264 ms 90836 KB Output is correct
3 Correct 501 ms 93452 KB Output is correct
4 Correct 1692 ms 174680 KB Output is correct
5 Correct 1817 ms 183868 KB Output is correct
6 Correct 1311 ms 165428 KB Output is correct
7 Correct 2736 ms 216236 KB Output is correct
8 Correct 2223 ms 204004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 82604 KB Output is correct
2 Correct 46 ms 82724 KB Output is correct
3 Correct 47 ms 82764 KB Output is correct
4 Correct 41 ms 82764 KB Output is correct
5 Correct 614 ms 122436 KB Output is correct
6 Correct 1237 ms 158212 KB Output is correct
7 Correct 1906 ms 195756 KB Output is correct
8 Correct 2477 ms 239216 KB Output is correct
9 Correct 206 ms 90744 KB Output is correct
10 Correct 204 ms 91376 KB Output is correct
11 Correct 229 ms 91852 KB Output is correct
12 Correct 2758 ms 251048 KB Output is correct
13 Correct 2299 ms 239320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 82764 KB Output is correct
2 Correct 40 ms 82828 KB Output is correct
3 Correct 41 ms 82728 KB Output is correct
4 Correct 39 ms 82612 KB Output is correct
5 Correct 2607 ms 239520 KB Output is correct
6 Correct 1926 ms 208600 KB Output is correct
7 Correct 1285 ms 171536 KB Output is correct
8 Correct 499 ms 116004 KB Output is correct
9 Correct 259 ms 91868 KB Output is correct
10 Correct 177 ms 86872 KB Output is correct
11 Correct 264 ms 91852 KB Output is correct
12 Correct 179 ms 86828 KB Output is correct
13 Correct 262 ms 91852 KB Output is correct
14 Correct 181 ms 86872 KB Output is correct
15 Correct 2683 ms 251176 KB Output is correct
16 Correct 2308 ms 239396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 82380 KB Output is correct
2 Correct 41 ms 82500 KB Output is correct
3 Correct 40 ms 82512 KB Output is correct
4 Correct 40 ms 82456 KB Output is correct
5 Correct 42 ms 82508 KB Output is correct
6 Correct 38 ms 82516 KB Output is correct
7 Correct 39 ms 82508 KB Output is correct
8 Correct 231 ms 90148 KB Output is correct
9 Correct 264 ms 90836 KB Output is correct
10 Correct 501 ms 93452 KB Output is correct
11 Correct 1692 ms 174680 KB Output is correct
12 Correct 1817 ms 183868 KB Output is correct
13 Correct 1311 ms 165428 KB Output is correct
14 Correct 2736 ms 216236 KB Output is correct
15 Correct 2223 ms 204004 KB Output is correct
16 Correct 37 ms 82604 KB Output is correct
17 Correct 46 ms 82724 KB Output is correct
18 Correct 47 ms 82764 KB Output is correct
19 Correct 41 ms 82764 KB Output is correct
20 Correct 614 ms 122436 KB Output is correct
21 Correct 1237 ms 158212 KB Output is correct
22 Correct 1906 ms 195756 KB Output is correct
23 Correct 2477 ms 239216 KB Output is correct
24 Correct 206 ms 90744 KB Output is correct
25 Correct 204 ms 91376 KB Output is correct
26 Correct 229 ms 91852 KB Output is correct
27 Correct 2758 ms 251048 KB Output is correct
28 Correct 2299 ms 239320 KB Output is correct
29 Correct 41 ms 82764 KB Output is correct
30 Correct 40 ms 82828 KB Output is correct
31 Correct 41 ms 82728 KB Output is correct
32 Correct 39 ms 82612 KB Output is correct
33 Correct 2607 ms 239520 KB Output is correct
34 Correct 1926 ms 208600 KB Output is correct
35 Correct 1285 ms 171536 KB Output is correct
36 Correct 499 ms 116004 KB Output is correct
37 Correct 259 ms 91868 KB Output is correct
38 Correct 177 ms 86872 KB Output is correct
39 Correct 264 ms 91852 KB Output is correct
40 Correct 179 ms 86828 KB Output is correct
41 Correct 262 ms 91852 KB Output is correct
42 Correct 181 ms 86872 KB Output is correct
43 Correct 2683 ms 251176 KB Output is correct
44 Correct 2308 ms 239396 KB Output is correct
45 Correct 112 ms 87372 KB Output is correct
46 Correct 171 ms 87740 KB Output is correct
47 Correct 809 ms 135292 KB Output is correct
48 Correct 1598 ms 184916 KB Output is correct
49 Correct 224 ms 92236 KB Output is correct
50 Correct 216 ms 91952 KB Output is correct
51 Correct 235 ms 92364 KB Output is correct
52 Correct 315 ms 98296 KB Output is correct
53 Correct 319 ms 98220 KB Output is correct
54 Correct 310 ms 98440 KB Output is correct