Advertisement
i_love_rao_khushboo

Weighted Merge Interval

Jun 29th, 2021
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.05 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define ld long double
  6. #define ull unsigned long long
  7. #define pb push_back
  8. #define ppb pop_back
  9. #define mp make_pair
  10. #define F first
  11. #define S second
  12. #define PI 3.1415926535897932384626
  13. #define sz(x) ((int)(x).size())
  14.  
  15. typedef pair<int, int> pii;
  16. typedef pair<ll, ll> pll;
  17. typedef vector<int> vi;
  18. typedef vector<ll> vll;
  19. typedef vector<ull> vull;
  20. typedef vector<bool> vb;
  21. typedef vector<char> vc;
  22. typedef vector<string> vs;
  23. typedef vector<pii> vpii;
  24. typedef vector<pll> vpll;
  25. typedef vector<vi> vvi;
  26. typedef vector<vll> vvll;
  27. typedef vector<vull> vvull;
  28. typedef vector<vb> vvb;
  29. typedef vector<vc> vvc;
  30. typedef vector<vs> vvs;
  31.  
  32. /************************************************** DEBUGGER *******************************************************************************************************/
  33.  
  34. #ifndef ONLINE_JUDGE
  35. #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
  36. #else
  37. #define debug(x)
  38. #endif
  39.  
  40. void _print(ll t) { cerr << t; }
  41. void _print(int t) { cerr << t; }
  42. void _print(string t) { cerr << t; }
  43. void _print(char t) { cerr << t; }
  44. void _print(ld t) { cerr << t; }
  45. void _print(double t) { cerr << t; }
  46. void _print(ull t) { cerr << t; }
  47.  
  48. template <class T, class V> void _print(pair <T, V> p);
  49. template <class T> void _print(vector <T> v);
  50. template <class T> void _print(vector <vector<T>> v);
  51. template <class T> void _print(set <T> v);
  52. template <class T, class V> void _print(map <T, V> v);
  53. template <class T> void _print(multiset <T> v);
  54. template <class T, class V> void _print(pair <T, V> p) { cerr << "{"; _print(p.F); cerr << ","; _print(p.S); cerr << "}"; }
  55. template <class T> void _print(vector <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  56. template <class T> void _print(vector <vector<T>> v) { cerr << "==>" << endl; for (vector<T> vec : v) { for(T i : vec) {_print(i); cerr << " "; } cerr << endl; } }
  57. template <class T> void _print(set <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  58. template <class T> void _print(multiset <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  59. template <class T, class V> void _print(map <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  60.  
  61. /*******************************************************************************************************************************************************************/
  62.  
  63. mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
  64. int rng(int lim) {
  65.     uniform_int_distribution<int> uid(0,lim-1);
  66.     return uid(rang);
  67. }
  68.  
  69. const int INF = 0x3f3f3f3f;
  70. const int mod = 1e9+7;
  71.  
  72. ll mod_exp(ll a, ll b) { a %= mod; if(a == 0) return 0LL; ll res = 1LL;
  73.                          while(b > 0) { if(b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }
  74.                          
  75. ll mod_inv(ll a) { return mod_exp(a, mod - 2); } // works only for prime value of "mod"
  76. ll GCD(ll a, ll b) { return (b == 0) ? a : GCD(b, a % b); }
  77.  
  78. /******************************************************************************************************************************/
  79.  
  80. vpii merge_intervals(vpii &v, int n) {
  81.     vpii res;
  82.     if(n == 0) return res;
  83.    
  84.     sort(v.begin(), v.end());
  85.     res.pb(v[0]);
  86.    
  87.     for(int i = 1; i < n; i++) {
  88.         int x1 = v[i].F;
  89.         int y1 = v[i].S;
  90.        
  91.         int x2 = res[res.size() - 1].F;
  92.         int y2 = res[res.size() - 1].S;
  93.        
  94.         if(y2 >= x1 or x1 == y2 + 1) res[res.size() - 1].S = max(y2, y1);
  95.         else res.pb({x1, y1});
  96.     }
  97.    
  98.     return res;
  99. }
  100.  
  101. vpii find_ans(vvi &v) {
  102.     int n = sz(v);
  103.     if(n == 0) return vpii();
  104.    
  105.     vvi tmp;
  106.    
  107.     for(int i = 0; i < n; i++) {
  108.         tmp.pb({v[i][0], -1, v[i][2]});
  109.         tmp.pb({v[i][1], 1, v[i][2]});
  110.     }
  111.    
  112.     sort(tmp.begin(), tmp.end());
  113.    
  114.     map<pii, int> m;
  115.    
  116.     int i = 0, wt = 0, st = -1, ed = -1;
  117.     n = sz(tmp);
  118.    
  119.     while(i + 1 < n) {
  120.         if(tmp[i][1] == -1) {
  121.             if(tmp[i+1][1] == -1) {
  122.                 if(tmp[i+1][0] == tmp[i][0]) {
  123.                     st = tmp[i][0];
  124.                     wt = tmp[i][2];
  125.                     while(i + 1 < n and tmp[i][0] == tmp[i+1][0] and tmp[i][1] == -1 and tmp[i+1][1] == -1) {
  126.                         wt += tmp[i+1][2];
  127.                         i++;
  128.                     }
  129.                    
  130.                     if(i < n) {
  131.                         if(tmp[i][1] == 1) {
  132.                             ed = tmp[i][1];
  133.                             wt += tmp[i][2];
  134.                            
  135.                             int j = i + 1;
  136.                             while(j + 1 < n and (tmp[j][0] == tmp[j+1][0]) and (tmp[j][1] == 1 and tmp[j+1][1] == 1)) {
  137.                                 wt += tmp[i+1][2];
  138.                                 j++;
  139.                             }
  140.                            
  141.                             m[{st, ed}] = wt;
  142.                             i = j;
  143.                             st = ed = -1;
  144.                             wt = 0;
  145.                         }
  146.                     }
  147.                 }
  148.                
  149.                 else i++;
  150.             }
  151.            
  152.             else {
  153.                 st = tmp[i][0];
  154.                 ed = tmp[i+1][0];
  155.                 wt = tmp[i][2] + tmp[i+1][2];
  156.                
  157.                 int j = i + 1;
  158.                 while(j + 1 < n and (tmp[j][0] == tmp[j+1][0]) and (tmp[j][1] == 1 and tmp[j+1][1] == 1)) {
  159.                     wt += tmp[j+1][2];
  160.                     j++;
  161.                 }
  162.                
  163.                 m[{st, ed}] = wt;
  164.                 i = j;
  165.                 st = ed = -1;
  166.                 wt = 0;
  167.             }
  168.         }
  169.        
  170.         else i++;
  171.     }
  172.    
  173.     for(int i = 0; i < sz(v); i++) {
  174.         if(m.find({v[i][0], v[i][1]}) == m.end()) {
  175.             m[{v[i][0], v[i][1]}] = v[i][2];
  176.         }
  177.     }
  178.    
  179.     int mx = -1;
  180.     for(auto x: m) mx = max(mx, x.S);
  181.    
  182.     vpii tmp2;
  183.     for(auto x: m) {
  184.         if(x.S == mx) tmp2.pb(x.F);
  185.     }
  186.    
  187.     return merge_intervals(tmp2, sz(tmp2));
  188. }
  189.  
  190. void solve()
  191. {
  192.     int n; cin >> n;
  193.     vvi v(n, vi(3));
  194.    
  195.     for(int i = 0; i < n; i++) {
  196.         cin >> v[i][0] >> v[i][1] >> v[i][2];
  197.     }
  198.    
  199.     vpii res = find_ans(v);
  200.     for(auto x: res) cout << x.F << ", " << x.S << "\n";
  201.     cout << "\n";
  202. }
  203.  
  204. int main()
  205. {
  206.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  207.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  208.  
  209.     // #ifndef ONLINE_JUDGE
  210.     //     freopen("input.txt", "r", stdin);
  211.     //     freopen("output.txt", "w", stdout);
  212.     // #endif
  213.    
  214.     // #ifndef ONLINE_JUDGE
  215.     //      freopen("error.txt", "w", stderr);
  216.     // #endif
  217.    
  218.     int t = 1;
  219.     // int test = 1;
  220.     // cin >> t;
  221.     while(t--) {
  222.         // cout << "Case #" << test++ << ": ";
  223.         solve();
  224.     }
  225.  
  226.     return 0;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement