Submission #1340756

#TimeUsernameProblemLanguageResultExecution timeMemory
1340756SpyrosAlivMonsters (NOI25_monsters)C++20
100 / 100
182 ms18152 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int INF = 1e15;

void solve() {
    int n, k; cin >> n >> k;
    vector<pair<int, int>> a(n+1);
    for (int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second;
    a[0] = {-1, -1};
    sort(a.begin(), a.end());
    set<int> x, det;
    for (int i = 1; i <= k; i++) {
        int u; cin >> u;
        x.insert(u);
    }
    x.insert(-INF);
    x.insert(INF);
    int ans = 0;
    for (auto [currX, currH]: a) {
        if (currX == -1) continue;
        auto prev = x.lower_bound(currX);
        if (*prev == currX) {
            if (det.find(*prev) == det.end()) {
                ans++;
                det.insert(*prev);
            }
            continue;
        }
        prev--;
        auto aft = x.lower_bound(currX);
        int d1 = currX - *prev;
        int d2 = *aft - currX;
        int mn = min(d1, d2);
        if (currH <= mn) {
            ans += currH;
            continue;
        }
        if (d1 == d2) {
            if (det.find(*prev) != det.end()) {
                ans += d1;
                continue;
            }
            else {
                det.insert(*aft);
                ans += d2 + 1;
            }
        }
        else if (d1 < d2) {
            if (det.find(*prev) != det.end()) {
                ans += d1;
                continue;
            }
            else {
                ans += d1 + 1;
                det.insert(*prev);
            }
        }
        else if (d2 < d1) {
            if (det.find(*aft) != det.end()) {
                ans += d2;
                continue;
            }
            else {
                ans += d2 + 1;
                det.insert(*aft);
            }
        }
    }
    cout << ans << "\n";
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    while (t--) solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...