제출 #210620

#제출 시각아이디문제언어결과실행 시간메모리
210620triCake 3 (JOI19_cake3)C++14
컴파일 에러
0 ms0 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define pb push_back
#define f first
#define s second
namespace debug {
    const int DEBUG = true;

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x);

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x);

    template<class T>
    void pr(const vector<T> &x);

    template<class T>
    void pr(const set<T> &x);

    template<class T1, class T2>
    void pr(const map<T1, T2> &x);

    template<class T>
    void pr(const T &x) { if (DEBUG) cout << x; }

    template<class T, class... Ts>
    void pr(const T &first, const Ts &... rest) { pr(first), pr(rest...); }

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x) { pr("{", x.f, ", ", x.s, "}"); }

    template<class T>
    void prIn(const T &x) {
        pr("{");
        bool fst = 1;
        for (auto &a : x) {
            pr(fst ? "" : ", ", a), fst = 0;
        }
        pr("}");
    }

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x) { prIn(x); }

    template<class T>
    void pr(const vector<T> &x) { prIn(x); }

    template<class T>
    void pr(const set<T> &x) { prIn(x); }

    template<class T1, class T2>
    void pr(const map<T1, T2> &x) { prIn(x); }

    void ps() { pr("\n"), cout << flush; }

    template<class Arg, class... Args>
    void ps(const Arg &first, const Args &... rest) {
        pr(first, " ");
        ps(rest...);
    }
}
using namespace debug;


const int MAXN = 2e5 + 100;
const int LOGN = 20;
const ll INF = 1e15;
int N, K;

ll v[MAXN], c[MAXN];

ll bTotal = -INF;


template<class Compare = less<pl>>
struct lazyQueue {
    priority_queue<pl, vector<pl>, Compare> q;
    priority_queue<pl, vector<pl>, Compare> r;

    void add(pl x) {
        q.push(x);
    }

    void erase(pl x) {
        r.push(x);
    }

    void clear() {
        while (q.size() && r.size() && q.top() == r.top()) {
            q.pop();
            r.pop();
        }
    }

    pl top() {
        clear();
        return q.top();
    }

    void pop() {
        clear();
        q.pop();
    }

    int size() {
        return q.size() - r.size();
    }
};


pi sRange[LOGN];
ll sSum[LOGN];

lazyQueue<greater<pl>> select[LOGN];
lazyQueue<> extq[LOGN];

int grp[LOGN][MAXN];

void reRange(int level, pi nRange) {
    auto &cSelect = select[level];
    auto &cExt = extq[level];

    auto &cGrp = grp[level];
    pi &cRange = sRange[level];
    ll &cSum = sSum[level];

    while (cRange.s < nRange.s) {
        cExt.add({v[cRange.s + 1], cRange.s + 1});
        cGrp[cRange.s + 1] = 1;
        cRange.s++;
    }

    while (cRange.f < nRange.f) {
        if (cGrp[cRange.f] == 0) {
            cSelect.erase({v[cRange.f], cRange.f});
            cSum -= v[cRange.f];
        } else {
            cExt.erase({v[cRange.f], cRange.f});
        }
        cRange.f++;
    }

    while (cSelect.size() < K && cExt.size()) {
        cSum += cExt.top().f;
        cGrp[cExt.top().s] = 0;

        cSelect.add(cExt.top());
        cExt.pop();
    }

    while (cExt.size() && cSelect.top().f < cExt.top().f) {
        cSum -= cSelect.top().f;
        cSum += cExt.top().f;

        cGrp[cSelect.top().s] = 1;
        cGrp[cExt.top().s] = 0;

        cExt.add(cSelect.top());
        cSelect.add(cExt.top());

        cSelect.pop();
        cExt.pop();
    }

//    assert(cRange == nRange);
//    assert(cSet.size() == min(K, cRange.s - cRange.f + 1));
}

ll getTotal(int setI) {
    auto &cSelect = select[setI];
//    assert(cSet.size() <= K);
    if (cSelect.size() == K) {
        return sSum[setI];
    } else {
        return -INF;
    }
}

int call = 0;

void compute(int level, int l1, int l2, int r1, int r2) {
    int cL = (l1 + l2) / 2;
//    cout << call++ << endl;

    ll maxTotal = -INF;
    int maxR = -1;

//    assert(0 <= l1 && 0 <= r1);
    for (int cR = max(cL + K - 1, r1); cR <= r2; cR++) {
        reRange(level, {cL, cR});
        ll cTotal = getTotal(level) + c[cL] - c[cR];
        if (cTotal > maxTotal) {
            maxTotal = cTotal;
            maxR = cR;
        }
    }
//    assert(maxR != -1);
    bTotal = max(bTotal, maxTotal);

    if (l1 < cL) {
        compute(level + 1, l1, cL - 1, r1, maxR);
    }
    if (cL < l2) {
        compute(level + 1, cL + 1, l2, maxR, r2);
    }
}

pi items[MAXN];

int main() {
//    freopen("cake3.in", "r", stdin);

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> N >> K;

//    cout << "start" << endl;

    for (int i = 0; i < N; i++) {
        int x, y;
        cin >> x >> y;
        items[i] = {y, x};
    }

    sort(items, items + N);

    for (int i = 0; i < N; i++) {
        v[i] = items[i].s;
        c[i] = items[i].f;
        c[i] *= 2;
    }

    fill(sSum, sSum + LOGN, 0);
    fill(sRange, sRange + LOGN, make_pair(0, -1));

//    cout << "computing" << endl;

    compute(0, 0, N - K, 0, N - 1);
    cout << bTotal << endl;
}

컴파일 시 표준 에러 (stderr) 메시지

cake3.cpp:126:35: error: 'lazyQueue<std::greater<std::pair<long long int, long long int> > > select [20]' redeclared as different kind of symbol
 lazyQueue<greater<pl>> select[LOGN];
                                   ^
In file included from /usr/include/x86_64-linux-gnu/sys/types.h:219:0,
                 from /usr/include/stdlib.h:314,
                 from /usr/include/c++/7/bits/std_abs.h:38,
                 from /usr/include/c++/7/cmath:47,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:41,
                 from cake3.cpp:1:
/usr/include/x86_64-linux-gnu/sys/select.h:106:12: note: previous declaration 'int select(int, fd_set*, fd_set*, fd_set*, timeval*)'
 extern int select (int __nfds, fd_set *__restrict __readfds,
            ^~~~~~
cake3.cpp: In function 'void reRange(int, pi)':
cake3.cpp:132:33: warning: pointer to a function used in arithmetic [-Wpointer-arith]
     auto &cSelect = select[level];
                                 ^
cake3.cpp:147:21: error: request for member 'erase' in 'cSelect', which is of non-class type 'int(int, fd_set*, fd_set*, fd_set*, timeval*)'
             cSelect.erase({v[cRange.f], cRange.f});
                     ^~~~~
cake3.cpp:155:20: error: request for member 'size' in 'cSelect', which is of non-class type 'int(int, fd_set*, fd_set*, fd_set*, timeval*)'
     while (cSelect.size() < K && cExt.size()) {
                    ^~~~
cake3.cpp:159:17: error: request for member 'add' in 'cSelect', which is of non-class type 'int(int, fd_set*, fd_set*, fd_set*, timeval*)'
         cSelect.add(cExt.top());
                 ^~~
cake3.cpp:163:35: error: request for member 'top' in 'cSelect', which is of non-class type 'int(int, fd_set*, fd_set*, fd_set*, timeval*)'
     while (cExt.size() && cSelect.top().f < cExt.top().f) {
                                   ^~~
cake3.cpp:164:25: error: request for member 'top' in 'cSelect', which is of non-class type 'int(int, fd_set*, fd_set*, fd_set*, timeval*)'
         cSum -= cSelect.top().f;
                         ^~~
cake3.cpp:167:22: error: request for member 'top' in 'cSelect', which is of non-class type 'int(int, fd_set*, fd_set*, fd_set*, timeval*)'
         cGrp[cSelect.top().s] = 1;
                      ^~~
cake3.cpp:170:26: error: request for member 'top' in 'cSelect', which is of non-class type 'int(int, fd_set*, fd_set*, fd_set*, timeval*)'
         cExt.add(cSelect.top());
                          ^~~
cake3.cpp:171:17: error: request for member 'add' in 'cSelect', which is of non-class type 'int(int, fd_set*, fd_set*, fd_set*, timeval*)'
         cSelect.add(cExt.top());
                 ^~~
cake3.cpp:173:17: error: request for member 'pop' in 'cSelect', which is of non-class type 'int(int, fd_set*, fd_set*, fd_set*, timeval*)'
         cSelect.pop();
                 ^~~
cake3.cpp: In function 'll getTotal(int)':
cake3.cpp:182:32: warning: pointer to a function used in arithmetic [-Wpointer-arith]
     auto &cSelect = select[setI];
                                ^
cake3.cpp:184:17: error: request for member 'size' in 'cSelect', which is of non-class type 'int(int, fd_set*, fd_set*, fd_set*, timeval*)'
     if (cSelect.size() == K) {
                 ^~~~
cake3.cpp:189:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^