This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> ii;
typedef pair<int, ii> iii;
struct seg{
vector<int> extra, qnt;
int n;
seg(){};
seg(int x){n = x; qnt.assign(4*(x+1), 0); extra.assign(4*(x+1), 0);}
void addQnt(int pos, int i, int f, int l, int r){
if(l<=i and f<=r){
qnt[pos] += 1;
// cout << i << ' '<< f << ' '<< qnt[pos] << endl;
return;
}
int meio = (i+f)/2;
if(l <=meio) addQnt(2*pos+1, i, meio, l, r);
if(r > meio) addQnt(2*pos+2, meio+1, f, l, r);
return;
}
void addExtra(int pos, int i, int f, int l, int r){
if(l <= i and f<= r){
extra[pos] += 1;
//cout << i << ' '<< f << ' '<< extra[pos] << endl;
return;
}
int meio = (i+f)/2;
if(l <= meio) addExtra(2*pos+1, i, meio, l, r);
if(r > meio) addExtra(2*pos+2, meio+1, f, l, r);
return;
}
pair<int,int> query(int pos, int i, int f, int idx){
// cout << i << " "<< f << " "<< qnt[pos] << endl;
if(i == f and i == idx){
return {extra[pos], qnt[pos]};
}
int meio = (i+f)/2;
pair<int, int> res;
if(idx <= meio) res = query(2*pos+1, i, meio, idx);
else res = query(2*pos+2, meio + 1, f, idx);
res.first += extra[pos];
res.second += qnt[pos];
// cout << i << " "<< f << " "<< res.second << endl;
return res;
}
void addQnt(int l, int r){ addQnt(0,0,n,l,r); return;}
void addExtra(int l, int r) {addExtra(0,0,n,l,r); return;}
pair<int,int> query(int idx){
return query(0,0,n,idx);
}
};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,k,x; cin >> n >> k >> x;
vector<int> aux;
vector<iii> v;
for(int i=0; i<n; i++){
int l,t,r; cin >> l >> t >> r;
aux.push_back(l); aux.push_back(l+t-1); aux.push_back(l + t); aux.push_back(r); aux.push_back(r + 1);
v.push_back({l, {t,r}});
}
sort(aux.begin(), aux.end());
int cont = 0;
map<int,int> comp;
for(auto i: aux){
if(comp[i] == 0){
cont++;
comp[i] = cont;
}
}
seg tree(cont);
for(auto i: v){
int l = i.first, t = i.second.first, r = i.second.second;
//cout << endl << endl;
tree.addQnt(comp[l],comp[r]);
tree.addExtra(comp[l+t], comp[r]);
}
int maior = 0;
int sum = 0;
int i=0, f = 1;
ii aa = tree.query(comp[aux[0]]);
if(aa.second >= k){sum += aa.first; maior = sum;}
while(f< aux.size()){
int l = aux[i], r = aux[f];
if(aux[f] == aux[f-1]){
f++;
continue;
}
if(r - l >= x){
int anterior = aux[f-1];
int sobra = anterior-l + 1;
int outro = sum;
sobra = x - sobra;
ii valorAnterior = tree.query(comp[anterior]);
if(valorAnterior.second >= k){
outro += sobra*valorAnterior.first;
}
maior = max(maior, outro);
sobra = aux[i+1] - aux[i];
valorAnterior = tree.query(comp[aux[i]]);
if(valorAnterior.second >= k){
sum -= sobra*valorAnterior.first;
}
i++;
}
else{
int anterior = aux[f-1];
int sobra = aux[f] - aux[f-1] -1;
ii valorAnterior = tree.query(comp[aux[f-1]]);
if(valorAnterior.second >= k){
sum += sobra*valorAnterior.first;
}
ii valorAtual = tree.query(comp[aux[f]]);
if(valorAtual.second >= k){
sum += valorAtual.first;
}
maior = max(sum, maior);
f++;
}
}
cout << maior << endl;
}
Compilation message (stderr)
autobahn.cpp: In function 'int main()':
autobahn.cpp:104:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | while(f< aux.size()){
| ~^~~~~~~~~~~~
autobahn.cpp:134:17: warning: unused variable 'anterior' [-Wunused-variable]
134 | int anterior = aux[f-1];
| ^~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |