|
#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; |
|
} |
|
}; |
|
|
|
|
|
|
|
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; |
|
} |
|
|