File size: 1,973 Bytes
f7ba5f2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int LIM = 1000005;
const int MOD = 1000000007;
using LL = long long;
using vll = vector<LL>;
struct event {
int id;
int day;
int price;
char type;
bool operator<(const event &e) const {
if (day != e.day) {
return day > e.day;
}
return price > e.price;
}
};
// Merges two lists already sorted in reverse, trimmed to K elements.
// Optionally add a constant c2 to each element of v2 in the result.
vll merge(const vll &v1, const vll &v2, int K, LL c2 = 0) {
vll res;
int i = 0, j = 0, sz1 = v1.size(), sz2 = v2.size();
while (i < sz1 && j < sz2 && (int)res.size() < K) {
if (v1[i] >= v2[j] + c2) {
res.push_back(v1[i++]);
} else {
res.push_back(v2[j++] + c2);
}
}
while (i < sz1 && (int)res.size() < K) {
res.push_back(v1[i++]);
}
while (j < sz2 && (int)res.size() < K) {
res.push_back(v2[j++] + c2);
}
return res;
}
int solve() {
int N, K;
cin >> N >> K;
vector<event> E;
for (int i = 0, a, b, x, y; i < N; i++) {
cin >> a >> b >> x >> y;
E.push_back({i, a, x, 'B'});
E.push_back({i, b, y, 'S'});
}
sort(E.begin(), E.end());
int prevday = -1;
vector<vll> P(N, {0});
vll V;
for (const event &e : E) {
if (prevday != e.day) {
V.clear();
}
if (e.type == 'B') {
V = merge(V, P[e.id], K, e.price);
} else {
vll V2;
for (LL v : V) {
if (v > e.price) {
V2.push_back(v - e.price);
}
}
P[e.id] = merge(P[e.id], V2, K);
}
prevday = e.day;
}
V.clear();
for (int i = 0; i < N; i++) {
V = merge(V, P[i], K);
}
LL res = 0;
for (LL v : V) {
res = (res + v) % MOD;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
for (int t = 1; t <= T; t++) {
cout << "Case #" << t << ": " << solve() << endl;
}
return 0;
}
|