The5threic1-1

Calci_region_sol

Apr 22nd, 2024
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | Source Code | 0 0
  1. #include<bits/stdc++.h>
  2. #define endl '\n'
  3. using namespace std;
  4. #ifdef  ONLINE_JUDGE
  5.     #define eprintf(...) 42
  6. #else
  7.         #define eprintf(...) {fprintf(stderr, __VA_ARGS__);fflush(stderr);}
  8. #endif
  9. clock_t startTime;
  10. double getCurrentTime()
  11. {
  12.     return (double)(clock() - startTime) / CLOCKS_PER_SEC;
  13. }
  14. class tracker_inter
  15. {
  16.     public:
  17.         set<pair<int, int>> forbidden;
  18.         int pt_add(int stuff)
  19.         {
  20.             pair<int, int> previn = {stuff, stuff};
  21.             if(!forbidden.empty() && forbidden.upper_bound({stuff, 2e9})!=forbidden.begin())
  22.             {
  23.                 previn = *prev(forbidden.upper_bound({stuff, 2e9}));
  24.             }
  25.             if(previn.second==stuff-1)
  26.             {
  27.                 forbidden.erase(previn);
  28.                 previn.second++;   
  29.             }
  30.             else
  31.             {
  32.                 previn = {stuff, stuff};
  33.             }  
  34.             forbidden.insert(previn);  
  35.             if(next(forbidden.find(previn))!=forbidden.end())
  36.             {
  37.                 auto thing = *next(forbidden.find(previn));
  38.                 if(thing.first==previn.second+1)
  39.                 {
  40.                     forbidden.erase(thing); forbidden.erase(previn);
  41.                     previn.second = thing.second;
  42.                     forbidden.insert(previn);
  43.                 }
  44.             }
  45.             return 1;      
  46.         }  
  47. };
  48. long long solve(vector<int> heights)
  49. {
  50.     tracker_inter Rail;
  51.     int n = heights.size();
  52.     long long ans = 0;
  53.     map<int, vector<int>> posi;
  54.     for(int i=0; i<n; i++)
  55.     {
  56.         posi[heights[i]].push_back(i); 
  57.     }
  58.     for(auto lin : posi)
  59.     {
  60.         for(auto stuff : lin.second)
  61.         {
  62.             Rail.pt_add(stuff);
  63.         }
  64.         for(auto stuff : lin.second)
  65.         {
  66.             auto thing = *prev(Rail.forbidden.upper_bound(make_pair(stuff, 2e5)));
  67.             ans += thing.second - thing.first + 1; 
  68.         }
  69.     }
  70.     return ans;
  71. }
  72. int main()
  73. {
  74.     ios::sync_with_stdio(false);
  75.     cin.tie(0);
  76.     int t=1;
  77.     //cin>>t;
  78.     startTime = clock();
  79.     while(t--)
  80.     {
  81.         startTime = clock();
  82.         int n;
  83.         cin>>n;
  84.         vector<int> heights(n);
  85.         for(int i=0; i<n; i++)
  86.         {
  87.             cin>>heights[i];
  88.         }
  89.         cout<<solve(heights)<<endl;
  90.     }
  91.     eprintf("time = %.5lf\n", getCurrentTime());
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment