kingofcoding1's blog

By kingofcoding1, history, 13 months ago, In English

https://www.aiasoft.ge/problem/72 if someone goes here and solove this riddle i am gonna be so heppy cause it took me huge amount of time to get the correct answer but failled on test n2 #include <bits/stdc++.h> using namespace std; using ll = long long;

int main() { ios::sync_with_stdio(false); cin.tie(nullptr);

ll h, w;
cin >> h >> w;
int N;
cin >> N;

vector<pair<ll,ll>> pts(N);
for(int i = 0; i < N; i++) {
    cin >> pts[i].first >> pts[i].second;
}

vector<ll> Ys;
Ys.reserve(N + 2);
Ys.push_back(0);
Ys.push_back(h);
for(auto &p : pts) Ys.push_back(p.second);
sort(Ys.begin(), Ys.end());
Ys.erase(unique(Ys.begin(), Ys.end()), Ys.end());

auto idx = [&](ll y) {
    return int(lower_bound(Ys.begin(), Ys.end(), y) - Ys.begin());
};

int M = Ys.size();
vector<vector<ll>> addEv(M), remEv(M);
for(int i = 0; i + 1 < N; i++) {
    auto A = pts[i], B = pts[i+1];
    if(A.second == B.second) continue;
    if(A.second > B.second) swap(A, B);
    int lo = idx(A.second), hi = idx(B.second);
    addEv[lo].push_back(A.first);
    remEv[hi].push_back(A.first);
}

multiset<ll> active;
long double uncleaned = 0.0L;

for(int i = 0; i + 1 < M; i++) {
    ll y0 = Ys[i], y1 = Ys[i+1], dy = y1 - y0;
    if(dy == 0) continue;

    for(ll x : addEv[i]) active.insert(x);
    for(ll x : remEv[i]) {
        auto it = active.find(x);
        if(it != active.end()) active.erase(it);
    }

    long double width;
    size_t k = active.size();

    if(k == 0) {
        width = (long double)w;
    }
    else if(k == 1) {
        ll x0 = *active.begin();
        width = (long double)w - x0;
    }
    else if(k == 2) {
        auto it = active.begin();
        ll x1 = *++it;
        width = (long double)w - x1;
    }
    else if(k == 3) {
        auto it = active.begin();
        ++it; ++it;
        ll x2 = *it;
        width = (long double)w - x2;
    }
    else if(k == 4) {
        auto it = active.end();
        ll x3 = *--it;
        width = (long double)w - x3;
    }
    else if(k == 5) {
        auto it = active.end();
        ll x4 = *--it;
        width = (long double)w - x4;
    }
    else {
        ll xmax = *active.rbegin();
        width = (long double)w - xmax;
    }

    uncleaned += width * dy;
}

ll answer = llround(uncleaned);
cout << answer << "\n";
return 0;

} here is my code but it faiils so help

  • Vote: I like it
  • -7
  • Vote: I do not like it