// Author : FatihCihan
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
void solve(){
int n,k,x;
cin >> n >> k >> x;
int l[n] , t[n] , r[n];
vector < int > comp = {0};
for(int i = 0;i<n;i++){
cin >> l[i] >> t[i] >> r[i];
t[i] = min(t[i] , r[i] - l[i] + 1);
comp.push_back(l[i] - 1);
comp.push_back(l[i]);
comp.push_back(l[i] + 1);
comp.push_back(l[i] + t[i] - 1);
comp.push_back(l[i] + t[i]);
comp.push_back(l[i] + t[i] + 1);
comp.push_back(r[i] - 1);
comp.push_back(r[i]);
comp.push_back(r[i] + 1);
}
sort(all(comp));
comp.resize(unique(all(comp)) - comp.begin());
auto fnd = [&](int x){
return lower_bound(all(comp),x) - comp.begin();
};
int m = sz(comp);
comp.push_back(comp.back());
int st[m+5]={0} , fault[m+5]={0} , en[m+5]={0};
for(int i = 0;i<n;i++){
st[fnd(l[i])]++;
fault[fnd(l[i] + t[i] - 1) + 1]++;
en[fnd(r[i]) + 1]++;
}
int cur_valid=0 , cur_fault=0 , charge[m]={0};
for(int i = 0;i<m;i++){
cur_valid += st[i];
cur_valid -= fault[i];
cur_fault += fault[i];
cur_fault -= en[i];
if(cur_valid + cur_fault >= k){
charge[i] = cur_fault;
}
}
// for(int i = 0;i<m;i++){
// cout << comp[i] << " : " << charge[i] << endl;
// }
int ans = 0 , cur_ans = 0 , rp = 0;
for(int i = 0;i<m;i++){
while(rp < m and comp[rp] - comp[i] < x){
cur_ans += charge[rp] * (comp[rp+1] - comp[rp]);
rp++;
}
int reel_ans = cur_ans - charge[rp-1] * (comp[rp] - comp[i] - x);
ans = max(ans , reel_ans);
// cout << comp[i] << " " << comp[i] + x << " : " << reel_ans << endl;
cur_ans -= charge[i] * (comp[i+1] - comp[i]);
}
// cout << endl;
cur_ans = 0;
int lp = m-1;
for(int i = m-1;i>=0;i--){
while(lp > 0 and comp[i] - comp[lp] < x){
cur_ans += charge[lp] * (comp[lp] - comp[lp-1]);
lp--;
}
int reel_ans = cur_ans - charge[lp+1] * (comp[i] - x - comp[lp]);
ans = max(ans , reel_ans);
// cout << comp[i] - x << " " << comp[i] << " : " << reel_ans << endl;
if(i)cur_ans -= charge[i] * (comp[i] - comp[i-1]);
}
cout << ans << endl;
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int testcase = 1;//cin >> testcase;
while(testcase--)solve();
cerr << 1000.0 * clock() / CLOCKS_PER_SEC << " ms" << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
504 KB |
Output is correct |
12 |
Correct |
1 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
592 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
2 ms |
592 KB |
Output is correct |
18 |
Correct |
2 ms |
592 KB |
Output is correct |
19 |
Correct |
2 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
504 KB |
Output is correct |
12 |
Correct |
1 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
592 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
592 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
2 ms |
592 KB |
Output is correct |
18 |
Correct |
2 ms |
592 KB |
Output is correct |
19 |
Correct |
2 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
592 KB |
Output is correct |
21 |
Correct |
159 ms |
41900 KB |
Output is correct |
22 |
Correct |
162 ms |
42008 KB |
Output is correct |
23 |
Correct |
155 ms |
41904 KB |
Output is correct |
24 |
Correct |
154 ms |
41904 KB |
Output is correct |
25 |
Correct |
173 ms |
41900 KB |
Output is correct |
26 |
Correct |
146 ms |
41908 KB |
Output is correct |
27 |
Correct |
147 ms |
41916 KB |
Output is correct |
28 |
Correct |
157 ms |
41916 KB |
Output is correct |
29 |
Correct |
152 ms |
41904 KB |
Output is correct |