fork download
  1. #include <bits/stdc++.h>
  2. typedef long long ll;
  3. using namespace std;
  4. #define mp make_pair
  5. long long n,x;
  6. vector<pair<ll,pair<pair<ll,ll>,ll > > > v;
  7. vector<ll> da;
  8. int main()
  9. {
  10. ios_base::sync_with_stdio(false);
  11. cin.tie(NULL);
  12. cin >> n >> x;
  13. bool fo = false;
  14. v.resize(n);
  15. da.resize(n);
  16. for(int i =0;i<n;i++)
  17. {
  18. long long a,b,c;
  19. cin >> a >> b >> c;
  20. v[i] = mp(b-a+1,mp(mp(a,b),c));
  21. }
  22. sort(v.begin(),v.end());
  23. for(int i=0;i<n;i++) da[i] = v[i].first;
  24. ll ans = 1e16;
  25. for(int i=0;i<n;i++)
  26. {
  27. ll d = v[i].first;
  28. if(d == x)
  29. {
  30. ans = min(ans,v[i].second.second); continue;
  31. }
  32. if(d > x)
  33. {
  34. break;
  35. }
  36. vector<ll>::iterator it = lower_bound(da.begin()+i,da.end(),x - d);
  37.  
  38. ll pos = it - da.begin();
  39.  
  40. if(*it == x - d)
  41. {
  42.  
  43. int j = pos;
  44.  
  45. while(da[j] == x - d && j < n)
  46. {
  47. if(v[i].second.first.second < v[j].second.first.first || v[j].second.first.second < v[i].second.first.first)
  48. {
  49. fo = true;
  50. ans = min(ans,v[i].second.second + v[j].second.second);
  51. }
  52. j++;
  53. }
  54. }
  55. }
  56. //cout << ans << endl;
  57. if(fo)
  58. cout << ans << endl;
  59. else
  60. cout << "-1\n";
  61. return 0;
  62. }
  63.  
Success #stdin #stdout 0s 15248KB
stdin
Standard input is empty
stdout
-1