直接模拟。。。从最低的开始向两边拓展= =
1 /************************************************************** 2 Problem: 1595 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:328 ms 7 Memory:3932 kb 8 ****************************************************************/ 9 10 #include <cstdio> 11 12 using namespace std; 13 typedef long long ll; 14 const int N = 1e5 + 5; 15 const ll inf = (ll) 1e18; 16 17 inline int read(); 18 19 struct data { 20 ll w, h; 21 int pre, nxt; 22 23 inline void get(int x) { 24 w = read(), h = read(); 25 pre = x - 1, nxt = x + 1; 26 } 27 } a[N]; 28 29 int n, now; 30 ll now_ans, ans[N]; 31 32 int main() { 33 int i; 34 n = read(); 35 for (i = now = 1; i <= n; ++i) { 36 a[i].get(i); 37 if (a[i].h < a[now].h) now = i; 38 } 39 a[0].w = a[n + 1].w = 0, a[0].h = a[n + 1].h = inf, a[0].nxt = 1, a[n + 1].pre = n; 40 #define Pre a[now].pre 41 #define Nxt a[now].nxt 42 for (i = 1; i <= n; ++i) { 43 while (a[Pre].h < a[now].h) now = Pre; 44 while (a[Nxt].h < a[now].h) now = Nxt; 45 ans[now] = now_ans + a[now].w; 46 a[Pre].nxt = Nxt, a[Nxt].pre = Pre; 47 if (a[Pre].h < a[Nxt].h) { 48 now_ans += a[now].w * (a[Pre].h - a[now].h); 49 a[Pre].w += a[now].w, now = Pre; 50 } else { 51 now_ans += a[now].w * (a[Nxt].h - a[now].h); 52 a[Nxt].w += a[now].w, now = Nxt; 53 } 54 } 55 for (i = 1; i <= n; ++i) 56 printf("%lld\n", ans[i]); 57 return 0; 58 } 59 60 inline int read() { 61 static int x; 62 static char ch; 63 x = 0, ch = getchar(); 64 while (ch < ‘0‘ || ‘9‘ < ch) 65 ch = getchar(); 66 while (‘0‘ <= ch && ch <= ‘9‘) { 67 x = x * 10 + ch - ‘0‘; 68 ch = getchar(); 69 } 70 return x; 71 }
时间: 2024-12-13 12:31:28