Submission #1179552

#TimeUsernameProblemLanguageResultExecution timeMemory
1179552ericl23302Aliens (IOI16_aliens)C++20
4 / 100
0 ms328 KiB
#include "aliens.h"

#include <iostream>
#include <algorithm>
#include <utility>
#include <climits>
#include <set>
using namespace std;
using ll = long long;

ll sq(ll a) {
    return a * a;
}
ll sq(int a) {
    return (ll)(a) * a;
}

ll getArea(pair<ll, ll> a) {
    return sq(a.second - a.first + 1);
}
ll getArea(ll a, ll b) {
    return sq(b - a + 1);
}

ll positiveArea(ll a, ll b) {
    ll sideLength = b - a + 1;
    return (sideLength > 0 ? sq(sideLength) : 0);
}

ll take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c) {
    vector<pair<ll, ll>> oriPoints;
    for (ll i = 0; i < n; ++i) {
        if (r[i] > c[i]) swap(r[i], c[i]);
        oriPoints.emplace_back(r[i], c[i]);
    }
    sort(oriPoints.begin(), oriPoints.end());
    oriPoints.emplace_back(-1, -1);
    
    set<pair<ll, pair<ll, ll>>> curPoints;
    set<pair<ll , ll>> costs, invCosts;
    pair<ll, ll> _prev = {-1, -1}, cur = {-1, -1};
    ll res = 0;
    for (ll i = 0; i <= n; ++i) {
        if (oriPoints[i].first == cur.first) cur.second = max(oriPoints[i].second, cur.second);
        else {
            if (((_prev.first != -1) && (cur.second > _prev.second)) || ((_prev.first == -1) && (cur.first != -1))) {
                if (_prev.first != -1) {
                    ll curCost = getArea(_prev.first, cur.second) - getArea(_prev) - getArea(cur) + positiveArea(cur.first, _prev.second);
                    costs.emplace(curPoints.size() - 1, curCost);
                    invCosts.emplace(curCost, curPoints.size() - 1);
                    res -= positiveArea(cur.first, _prev.second);
                }
                _prev = cur;
                curPoints.insert(make_pair(curPoints.size(), cur));
                res += getArea(cur);
            }
            cur = oriPoints[i];
        }
    }

    while (curPoints.size() > k) {
        auto cur = invCosts.begin();
        res -= cur->first;
        auto firstPoint = curPoints.lower_bound(make_pair(cur->second, make_pair(-1, -1))), secondPoint = next(firstPoint);
        pair<int, int> newPoint = {firstPoint->second.first, secondPoint->second.second};
        int newIndex = firstPoint->first;
        if (firstPoint != curPoints.begin()) {
            auto prevPoint = prev(firstPoint);
            auto costPair = costs.lower_bound(make_pair(prevPoint->first, -1));
            invCosts.erase(invCosts.lower_bound(make_pair(costPair->second, costPair->first)));
            costs.erase(costPair);
            int prevCost = getArea(prevPoint->second.first, newPoint.second) - getArea(prevPoint->second) - getArea(newPoint) + positiveArea(newPoint.first, prevPoint->second.second);
            costs.emplace(prevPoint->first, prevCost);
            invCosts.emplace(prevCost, prevPoint->first);
        } 
        if (next(secondPoint) != curPoints.end()) {
            auto nextPoint = next(secondPoint);
            auto costPair = costs.lower_bound(make_pair(nextPoint->first, -1));
            invCosts.erase(invCosts.lower_bound(make_pair(costPair->second, costPair->first)));
            costs.erase(costPair);
            int nextCost = getArea(newPoint.first, nextPoint->second.second) - getArea(newPoint) - getArea(nextPoint->second) + positiveArea(nextPoint->second.first, newPoint.second);
            costs.emplace(newIndex, nextCost);
            invCosts.emplace(nextCost, newIndex);
        }
        invCosts.erase(invCosts.lower_bound(make_pair(cur->second, cur->first)));
        costs.erase(cur);
        curPoints.erase(firstPoint);
        curPoints.erase(secondPoint);
        curPoints.insert(make_pair(newIndex, newPoint));
    }

    return res;
}

Compilation message (stderr)

aliens.h:1:9: warning: #pragma once in main file
    1 | #pragma once
      |         ^~~~
aliens_c.h:1:9: warning: #pragma once in main file
    1 | #pragma once
      |         ^~~~
#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...