ALAKE - Hồ nhân tạo
Tác giả: ll931110
Ngôn ngữ: C++
#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <stack>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
int w[100010],h[100010],n;
int L[100010],R[100010];
long long ret[100010];
int main()
{
// freopen("alake.5.in","r",stdin);
// freopen("alake.out","w",stdout);
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d %d", &w[i], &h[i]);
h[0] = h[n + 1] = 1 << 30;
int j = 1;
L[1] = 0;
for (int i = 2; i <= n; i++) if (h[i] > h[i - 1]) L[i] = j; else
{
L[i] = i - 1; j = i;
}
j = n;
R[n] = n + 1;
for (int i = n - 1; i; i--) if (h[i] > h[i + 1]) R[i] = j; else
{
R[i] = i + 1; j = i;
}
int pos = 1;
long long T = 0;
for (int i = 2; i <= n; i++) if (h[i] < h[pos]) pos = i;
int cnt = n;
while (cnt > 1)
{
cnt--;
if (h[pos] > h[L[pos]]) pos = L[pos];
else if (h[pos] > h[R[pos]]) pos = R[pos];
ret[pos] = T + w[pos];
if (h[L[pos]] > h[R[pos]])
{
T += 1LL * (h[R[pos]] - h[pos]) * w[pos];
w[R[pos]] += w[pos];
L[R[pos]] = L[pos];
pos = R[pos];
}
else
{
T += 1LL * (h[L[pos]] - h[pos]) * w[pos];
w[L[pos]] += w[pos];
R[L[pos]] = R[pos];
pos = L[pos];
}
}
ret[pos] = T + w[pos];
// for (int i = 1; i <= n; i++) cout << ret[i] << endl;
for (int i = 1; i <= n; i++) printf("%lld\n", ret[i]);
}