Facebook
From Lim Rui Yuan, 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 257
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define all(x) (x).begin(), (x).end()
  4. #define sz(x) (int) (x).size()
  5. typedef long long lint;
  6. typedef pair<lint,lint> ii;
  7.  
  8. int n;
  9. lint A[500005], B[500005];
  10. lint ans[500005];
  11.  
  12. void add(int l, int r, lint val){
  13.         ans[l] += val;
  14.         ans[r+1] -= val;
  15. }
  16.  
  17. struct sumtool{
  18.         lint total = 0;
  19.         vector<lint> v, psum;
  20.        
  21.         void push(lint x){
  22.                 if(v.empty()) v.push_back(x);
  23.                 else v.push_back(max(v.back(), x));
  24.                 total += v.back();
  25.                 psum.push_back(total);
  26.         }
  27.  
  28.         lint query(lint x){
  29.                 int pos = upper_bound(all(v), x) - v.begin();
  30.                 lint res = total;
  31.                 if(pos != 0) res -= psum[pos-1];
  32.                 res += x * pos;
  33.                 return res;
  34.         }
  35. };
  36. struct thing{ lint val, total, cnt; };
  37.  
  38. lint val[500005], total[500005], tempans[500005];
  39.  
  40. void dnc(int l, int r){
  41.         if(l == r){
  42.                 add(0, l-1, A[l]);
  43.                 add(l, n, B[l]);
  44.                 return;
  45.         }
  46.        
  47.         lint res = 0;
  48.         int m = (l+r)/2;
  49.        
  50.         ///compute for [l, m] first
  51.         sumtool R;
  52.         for(int i = m+1;i <= r;i++) R.push(A[i]);
  53.        
  54.         lint acc = 0;
  55.         for(int i = m;i >= l;i--){
  56.                 acc = max(acc, A[i]);
  57.                 val[i] = acc;
  58.                
  59.                 total[i] = R.query(acc);
  60.                 res += total[i];
  61.         }
  62.                
  63.         tempans[l-1] = res;
  64.        
  65.         stack<thing> S;
  66.         for(int i = l;i <= m;i++){
  67.                 res -= total[i];
  68.                 val[i] = max(val[i], B[i]);
  69.                 int cnt = 1;
  70.                 while(!S.empty()){
  71.                         if(S.top().val <= val[i]){
  72.                                 cnt += S.top().cnt;
  73.                                 res -= S.top().total * S.top().cnt;
  74.                                 S.pop();
  75.                         }
  76.                         else break;
  77.                 }
  78.                        
  79.                 lint total = R.query(val[i]);
  80.                 res += total * cnt;
  81.                 S.push({val[i], total, cnt});
  82.                
  83.                 tempans[i] = res;
  84.         }
  85.        
  86.         ///compute for [m+1, R] first
  87.         sumtool L;
  88.         while(!S.empty()) S.pop();
  89.         for(int i = m;i >= l;i--) L.push(B[i]);
  90.                
  91.         acc = 0, res = 0;
  92.         for(int i = m+1;i <= r;i++){
  93.                 acc = max(acc, A[i]);
  94.                 val[i] = acc;
  95.                
  96.                 total[i] = L.query(acc);
  97.                 res += total[i];
  98.         }
  99.        
  100.         for(int i = r;i >= m+1;i--){
  101.                 if(S.empty() || S.top().val > val[i]) S.push({val[i], total[i], 1});
  102.                 else S.top().cnt++;
  103.         }
  104.                        
  105.         for(int i = m+1;i <= r;i++){
  106.                 res -= S.top().total;
  107.                 if(S.top().cnt == 1) S.pop();
  108.                 else S.top().cnt--;
  109.                
  110.                 val[i] = max(val[i], B[i]);
  111.                 if(i != m+1) val[i] = max(val[i], val[i-1]);
  112.                
  113.                 int cnt = 1;
  114.                 while(!S.empty()){
  115.                         if(S.top().val <= val[i]){
  116.                                 cnt += S.top().cnt;
  117.                                 res -= S.top().total * S.top().cnt;
  118.                                 S.pop();
  119.                         }
  120.                         else break;
  121.                 }
  122.                
  123.                 lint total = L.query(val[i]);
  124.                 res += total * cnt;
  125.                 S.push({val[i], total, cnt});
  126.                
  127.                 tempans[i] = res;
  128.                
  129.                 if(S.top().cnt == 1) S.pop();
  130.                 else S.top().cnt--;
  131.         }
  132.        
  133.         add(0, l-1, tempans[l-1]);
  134.         add(r, n, tempans[r]);
  135.         for(int i = l;i < r;i++) add(i, i, tempans[i]);
  136.        
  137.         dnc(l, m); dnc(m+1, r);
  138. }
  139.  
  140. int main(){
  141.         ios_base::sync_with_stdio(false); cin.tie(0);
  142.        
  143.         cin >> n;
  144.         for(int i = 1;i <= n;i++) cin >> A[i];
  145.         for(int i = 1;i <= n;i++) cin >> B[i], B[i] += A[i];
  146.  
  147.         dnc(1, n);
  148.        
  149.         lint acc = 0;
  150.         for(int i = 0;i <= n;i++){
  151.                 acc += ans[i];
  152.                 cout << acc << "\n";
  153.         }
  154. }
captcha