Guest User

Untitled

a guest
Feb 23rd, 2020
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. #ifdef DEBUG
  2. #define _GLIBCXX_DEBUG
  3. #endif
  4.  
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. typedef long double ld;
  10.  
  11. #ifdef DEBUG
  12. #define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
  13. #else
  14. #define eprintf(...) ;
  15. #endif
  16.  
  17. #define sz(x) ((int) (x).size())
  18. #define TASK "text"
  19.  
  20. const int inf = (int) 1.01e9;
  21. const long long infll = (long long) 1.01e18;
  22. const ld eps = 1e-9;
  23. const ld pi = acos((ld) -1);
  24.  
  25. #ifdef DEBUG
  26. mt19937 mrand(300);
  27. #else
  28. mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count());
  29. #endif
  30.  
  31. int rnd(int x) {
  32.   return mrand() % x;
  33. }
  34.  
  35. void precalc() {
  36. }
  37.  
  38. struct point {
  39.   long long x, y;
  40.  
  41.   point(): x(0), y(0) {}
  42.  
  43.   point(long long xx, long long yy): x(xx), y(yy) {}
  44.  
  45.   point operator + (const point &p) const {
  46.     return point(x + p.x, y + p.y);
  47.   }
  48.  
  49.   point operator - (const point &p) const {
  50.     return point(x - p.x, y - p.y);
  51.   }
  52.  
  53.   long long operator ^ (const point &p) const {
  54.     return x * p.y - y * p.x;
  55.   }
  56. };
  57.  
  58. bool operator < (const point &a, const point &b) {
  59.   return (a ^ b) > 0;
  60. }
  61.  
  62. bool operator > (const point &a, const point &b) {
  63.   return (a ^ b) < 0;
  64. }
  65.  
  66. const int maxn = (int) 2e5 + 5;
  67. int n;
  68. int a[maxn];
  69.  
  70. bool read() {
  71.   if (scanf("%d", &n) < 1) {
  72.     return false;
  73.   }
  74.   for (int i = 0; i < n; i++) {
  75.     scanf("%d", &a[i]);
  76.   }
  77.   return true;
  78. }
  79.  
  80. long long ans[maxn];
  81.  
  82. vector<point> ch[2];
  83. vector<point> s[2];
  84. vector<point> dels[maxn];
  85. long long res;
  86.  
  87. void del(int t) {
  88.   assert(!ch[t].empty());
  89.   point p = ch[t].back();
  90.   ch[t].pop_back();
  91.   s[t].pop_back();
  92.   int pos;
  93.   if (!t) {
  94.     pos = lower_bound(ch[!t].begin(), ch[!t].end(), p, greater<point>()) - ch[!t].begin();
  95.   } else {
  96.     pos = upper_bound(ch[!t].begin(), ch[!t].end(), p, greater<point>()) - ch[!t].begin();
  97.   }
  98.   res -= s[!t][pos].x * p.y;
  99.   res -= (s[!t].back().y - s[!t][pos].y) * p.x;
  100. }
  101.  
  102. void add(int t, const point &p) {
  103.   ch[t].push_back(p);
  104.   point curs = s[t].back() + p;
  105.   s[t].push_back(curs);
  106.   int pos;
  107.   if (!t) {
  108.     pos = lower_bound(ch[!t].begin(), ch[!t].end(), p, greater<point>()) - ch[!t].begin();
  109.   } else {
  110.     pos = upper_bound(ch[!t].begin(), ch[!t].end(), p, greater<point>()) - ch[!t].begin();
  111.   }
  112.   res += s[!t][pos].x * p.y;
  113.   res += (s[!t].back().y - s[!t][pos].y) * p.x;
  114. }
  115.  
  116. long long s0[maxn], s1[maxn];
  117.  
  118. void solve() {
  119.   ch[0] = {};
  120.   s[0] = {point()};
  121.   for (int i = 0; i < n; i++) {
  122.     point cur = point(1, a[i]);
  123.     dels[i].clear();
  124.     while (!ch[0].empty() && !(cur < ch[0].back())) {
  125.       dels[i].push_back(ch[0].back());
  126.       cur = cur + ch[0].back();
  127.       ch[0].pop_back();
  128.       s[0].pop_back();
  129.     }
  130.     ch[0].push_back(cur);
  131.     point curs = s[0].back() + cur;
  132.     s[0].push_back(curs);
  133.   }
  134.   ch[1] = {};
  135.   s[1] = {point()};
  136.   res = 0;
  137.   for (int i = n - 1; i >= 0; i--) {
  138.     del(0);
  139.     while (!dels[i].empty()) {
  140.       point cur = dels[i].back();
  141.       dels[i].pop_back();
  142.       add(0, cur);
  143.     }
  144.     ans[i] = res;
  145.     point cur = point(1, a[i]);
  146.     while (!ch[1].empty() && !(cur < ch[1].back())) {
  147.       cur = cur + ch[1].back();
  148.       del(1);
  149.     }
  150.     add(1, cur);
  151.   }
  152.   s0[0] = 0;
  153.   s1[0] = 0;
  154.   for (int i = 0; i < n; i++) {
  155.     s0[i + 1] = s0[i] + a[i];
  156.     s1[i + 1] = s1[i] + (long long) a[i] * i;
  157.   }
  158.   for (int i = 0; i < n; i++) {
  159.     ans[i] += n * (s0[n] - s0[i + 1]);
  160.     ans[i] -= s1[n] - s1[i + 1];
  161.     ans[i] += s0[i];
  162.     ans[i] += s1[i];
  163.     ans[i] += (long long) a[i] * (n - 1);
  164.   }
  165.   for (int i = 0; i < n; i++) {
  166.     printf("%lld ", ans[i]);
  167.   }
  168.   printf("\n");
  169. }
  170.  
  171. int main() {
  172.   precalc();
  173. #ifdef DEBUG
  174.   assert(freopen(TASK ".in", "r", stdin));
  175.   assert(freopen(TASK ".out", "w", stdout));
  176. #endif
  177.   while (read()) {
  178.     solve();
  179. #ifdef DEBUG
  180.     eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
  181. #endif
  182.   }
  183.   return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment