This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// 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++){
// cout << "st : " << i << " " << rp << " : " << cur_ans << endl;
while(rp < m and comp[rp] - comp[i] < x){
cur_ans += charge[rp] * (comp[rp+1] - comp[rp]);
rp++;
// cout << i << " " << rp << " : " << cur_ans << endl;
}
int reel_ans = cur_ans - charge[rp-1] * (comp[rp] - comp[i] - x);
ans = max(ans , reel_ans);
// cout << "en : " << i << " " << rp << " : " << reel_ans << endl;
cur_ans -= charge[i] * (comp[i+1] - comp[i]);
}
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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |