Advertisement
_no0B

Untitled

Dec 27th, 2021
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 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.     /// problem: https://lightoj.com/problem/save-the-trees
  32.    
  33.     /// seg(i) => dp[i-1] + max(i , ... , cur)
  34.    
  35.     dp[0] = 0;
  36.  
  37.     for(int i = 1 ; i <= n ; i++){
  38.         leftIndex[i] = i;
  39.         par[i] = i;
  40.         maxH[i] = arr[i];
  41.     }
  42.  
  43.     leftEnd[1] = 1;
  44.     vis[type[1]] = 1;
  45.     for(int i = 2  ; i <= n ; i++){
  46.         if(vis[type[i]] == 0) leftEnd[i] = leftEnd[i-1];
  47.         else{
  48.             for(int j = leftEnd[i-1] ; j <= i ; j++){
  49.                 if(vis[type[i]] == 0){
  50.                     leftEnd[i] = j;
  51.                     break;
  52.                 }
  53.                 vis[type[j]] = 0;
  54.             }
  55.         }
  56.  
  57.         vis[type[i]] = 1;
  58.     }
  59.  
  60.     for(int cur = 1 ; cur <= n ; cur++){
  61.         for(int i = cur-1 ; i > 0 && GetMax(i) <= arr[cur]; i = GetLeft(i) - 1){
  62.             int from = GetLeft(i);
  63.             Update(from , i , -GetMax(i) + arr[cur]);
  64.             Merge(i , cur);
  65.         }
  66.         Update(cur , cur , arr[cur] + dpp[cur-1]);
  67.         dpp[i] = Query(leftEnd[cur] , cur);
  68.     }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement