Advertisement
_no0B

Untitled

Dec 27th, 2021
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. #define N ((int)1e5 + 8)
  4. #define MAX ((int)1e9 + 8)
  5. #define fastio ios_base::sync_with_stdio(false),cin.tie(NULL)
  6.  
  7. using namespace std;
  8.  
  9. int GetLeft(int n)
  10. {
  11.     n = FindPar(n);
  12.     return leftIndex[n];
  13. }
  14.  
  15. int GetMax(int n)
  16. {
  17.     n = FindPar(n);
  18.     return maxH[n];
  19. }
  20.  
  21. void Merge(int a , int b)
  22. {
  23.     a = FindPar(a) , b = FindPar(b);
  24.     par[a] = b;
  25.     maxH[b] = max(maxH[a] , maxH[b]);
  26.     lefIndex[b] = min(leftIndex[a] , leftIndex[b]);
  27. }
  28.  
  29. int main()
  30. {
  31.     /// seg(i) => dp[i-1] + max(i , ... , cur)
  32.     dp[0] = 0;
  33.  
  34.     for(int i = 1 ; i <= n ; i++){
  35.         leftIndex[i] = i;
  36.         par[i] = i;
  37.         maxH[i] = arr[i];
  38.     }
  39.  
  40.     leftEnd[1] = 1;
  41.     vis[type[1]] = 1;
  42.     for(int i = 2  ; i <= n ; i++){
  43.         if(vis[type[i]] == 0) leftEnd[i] = leftEnd[i-1];
  44.         else{
  45.             for(int j = leftEnd[i-1] ; j <= i ; j++){
  46.                 if(vis[type[i]] == 0){
  47.                     leftEnd[i] = j;
  48.                     break;
  49.                 }
  50.                 vis[type[j]] = 0;
  51.             }
  52.         }
  53.  
  54.         vis[type[i]] = 1;
  55.     }
  56.  
  57.     for(int cur = 1 ; cur <= n ; cur++){
  58.         for(int i = cur-1 ; i > 0 && GetMax(i) <= arr[cur]; i = GetLeft(i) - 1){
  59.             int from = GetLeft(i);
  60.             Update(from , i , -GetMax(i) + arr[cur]);
  61.             Merge(i , cur);
  62.         }
  63.         Update(cur , cur , arr[cur] + dpp[cur-1]);
  64.         dpp[i] = Query(leftEnd[cur] , cur);
  65.     }
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement