Submission #914151

# Submission time Handle Problem Language Result Execution time Memory
914151 2024-01-21T09:44:57 Z vjudge1 Inspections (NOI23_inspections) C++17
100 / 100
720 ms 39472 KB
#include<bits/stdc++.h>
 
using namespace std;

#define int long long

vector<pair<int, int>> queries;
vector<long long> prefix;
vector<long long> substract;
vector<pair<int, int>> safety;
long long totalSum = 0;

long long sum(int l, int r){
    if(l > r) return 0;
    if(l > 0) return prefix[r] - prefix[l-1];
    else return prefix[r];
}

long long getDist(int i, int j, int ind){
    assert(i <= j);
    long long mid = sum(i+1, j-1);
    return mid + queries[i].second - ind + ind - queries[j].first;
}

void add(long long dist, int val){
    int lo = 0;
    int hi = (int)safety.size() - 1;
    int bst = (int)safety.size();
    while(lo <= hi){
        int mid = (lo + hi) / 2;
        if(safety[mid].first > dist){
            bst = mid;
            hi = mid - 1;
        }else lo = mid + 1;
    }
    // cout << "add: dist " << dist << " val "<< val << " bst "<< bst << "\n";
    totalSum += val;
    substract[bst] += val;
}

void solve(){
    int n, m, q; cin >> n >> m >> q;
    vector<vector<int>> start(n), ends(n+1);
    queries.resize(m); prefix.resize(m);
    for(int i = 0; i < m; i++){
        int l, r; cin >> l >> r; l--; r--;
        start[l].push_back(i);
        ends[r+1].push_back(i);
        if(i == 0) prefix[i] = r - l + 1;
        else prefix[i] = prefix[i-1] + r - l + 1;
        queries[i] = {l, r};
    }
    safety.resize(q);
    substract.assign(q+1, 0);
    for(int i = 0; i < q; i++){
        int s; cin >> s;
        safety[i] = {s, i};
    }
    sort(safety.begin(), safety.end());
    set<pair<int, long long>> s;
    for(int i = 0; i < n; i++){
        for(int x : ends[i]){
            auto it = s.lower_bound({x, -1});
            pair<int, long long> p = *it;
            add(p.second, -(n-i));
            int prev = -1, nxt = -1;
            if(it != s.begin()){
                it--;
                prev = (*it).first;
            }
            s.erase(p);
            it = s.lower_bound({x, -1});
            if(it != s.end()){
                nxt = (*it).first;
            }
            if(nxt != -1){
                it = s.lower_bound({nxt, -1});
                add((*it).second, -(n-i));
                s.erase(it);
                if(prev == -1){
                    s.insert({nxt, -1});
                    add(-1, n-i);
                }else{
                    long long dist = getDist(prev, nxt, i);
                    s.insert({nxt, dist});
                    add(dist, n-i);
                }
            }
        }
        for(int x : start[i]){
            if(s.empty()){
                s.insert({x, -1});
                add(-1, n-i);
                continue;
            }
            auto it = s.lower_bound({x, -1});
            if(it == s.end()){
                it--;
                long long dist = getDist((*it).first, x, i);
                add(dist, n-i);
                s.insert({x, dist});
            }else if(it == s.begin()){
                int y = (*it).first;
                long long dist = getDist(x, y, i);
                add(dist, n-i);
                s.erase(it);
                s.insert({y, dist});
                s.insert({x, -1});
                add(-1, n-i);
            }else{
                pair<int, long long> tp = (*it);
                it--;
                pair<int, long long> btm = (*it);
                s.erase(tp);
                add(tp.second, -(n-i));
                long long distTpMe = getDist(x, tp.first, i);
                long long distMeBtm = getDist(btm.first, x, i);
                add(distTpMe, n-i);
                add(distMeBtm, n-i);
                s.insert({x, distMeBtm});
                s.insert({tp.first, distTpMe});
            }
        }
        // cout << "remove"<< endl;
    }
    vector<long long> ans(q);
    for(int i = 0; i < q; i++){
        totalSum -= substract[i];
        ans[safety[i].second] = totalSum;
    }
    for(long long& x : ans) cout << x << " ";
    cout << "\n";
}

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

    // int tt; cin >> tt;
    int tt = 1;
    while(tt--) solve();
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 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 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 500 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 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 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 500 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 47 ms 7252 KB Output is correct
14 Correct 40 ms 7168 KB Output is correct
15 Correct 43 ms 7252 KB Output is correct
16 Correct 43 ms 7504 KB Output is correct
17 Correct 42 ms 7252 KB Output is correct
18 Correct 48 ms 7504 KB Output is correct
19 Correct 50 ms 7924 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 43 ms 7412 KB Output is correct
4 Correct 50 ms 17000 KB Output is correct
5 Correct 447 ms 39104 KB Output is correct
6 Correct 433 ms 37952 KB Output is correct
7 Correct 253 ms 30724 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 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 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 500 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 47 ms 7252 KB Output is correct
14 Correct 40 ms 7168 KB Output is correct
15 Correct 43 ms 7252 KB Output is correct
16 Correct 43 ms 7504 KB Output is correct
17 Correct 42 ms 7252 KB Output is correct
18 Correct 48 ms 7504 KB Output is correct
19 Correct 50 ms 7924 KB Output is correct
20 Correct 51 ms 17804 KB Output is correct
21 Correct 50 ms 17288 KB Output is correct
22 Correct 50 ms 18000 KB Output is correct
23 Correct 45 ms 16976 KB Output is correct
24 Correct 46 ms 16720 KB Output is correct
25 Correct 51 ms 16976 KB Output is correct
26 Correct 61 ms 17488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 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 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 500 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 47 ms 7252 KB Output is correct
14 Correct 40 ms 7168 KB Output is correct
15 Correct 43 ms 7252 KB Output is correct
16 Correct 43 ms 7504 KB Output is correct
17 Correct 42 ms 7252 KB Output is correct
18 Correct 48 ms 7504 KB Output is correct
19 Correct 50 ms 7924 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 43 ms 7412 KB Output is correct
23 Correct 50 ms 17000 KB Output is correct
24 Correct 447 ms 39104 KB Output is correct
25 Correct 433 ms 37952 KB Output is correct
26 Correct 253 ms 30724 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 512 KB Output is correct
29 Correct 51 ms 17804 KB Output is correct
30 Correct 50 ms 17288 KB Output is correct
31 Correct 50 ms 18000 KB Output is correct
32 Correct 45 ms 16976 KB Output is correct
33 Correct 46 ms 16720 KB Output is correct
34 Correct 51 ms 16976 KB Output is correct
35 Correct 61 ms 17488 KB Output is correct
36 Correct 720 ms 26324 KB Output is correct
37 Correct 672 ms 34968 KB Output is correct
38 Correct 529 ms 33748 KB Output is correct
39 Correct 254 ms 30288 KB Output is correct
40 Correct 434 ms 38936 KB Output is correct
41 Correct 278 ms 39380 KB Output is correct
42 Correct 240 ms 39472 KB Output is correct
43 Correct 225 ms 39256 KB Output is correct
44 Correct 222 ms 38308 KB Output is correct
45 Correct 437 ms 29860 KB Output is correct
46 Correct 402 ms 30520 KB Output is correct
47 Correct 293 ms 29456 KB Output is correct
48 Correct 596 ms 34896 KB Output is correct