답안 #389983

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
389983 2021-04-15T03:27:04 Z talant117408 Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
90 ms 4640 KB
/*
    Code written by Talant I.D.
*/
#include <bits/stdc++.h>
 
using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
 
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) int((v).size())
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define OK cout << "OK" << endl;
 
const int mod = 1e9+7;
 
ll mode(ll a) {
    a %= mod;
    if (a < 0) a += mod;
    return a;
}
 
ll subt(ll a, ll b) {
    return mode(mode(a)-mode(b));
}
 
ll add(ll a, ll b) {
    return mode(mode(a)+mode(b));
}
 
ll mult(ll a, ll b) {
    return mode(mode(a)*mode(b));
}
 
ll binpow(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b&1) res = mult(res, a);
        a = mult(a, a);
        b >>= 1;
    }
    return res;
}

const int N = 1e5+7;
ll h[N], w[N], dp[N];
int n;

int main() {
	do_not_disturb
	
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> h[i];
	}
	for (int i = 1; i <= n; i++) {
		cin >> w[i];
	}
	
	stack <pii> st;
	ll ans = 0;
	ll sum = 0;
	
	for (int i = 1; i <= n; i++) {
		ll additional = 0;
		while (!st.empty() && st.top().first > h[i]) {
			additional = add(additional, st.top().second);
			sum = subt(sum, mult(mult(st.top().first, st.top().first+1), mult(st.top().second, binpow(2, mod-2))));
			st.pop();
		}
		ans = add(ans, mult(mult(mult(h[i], h[i]+1), mult(w[i], w[i]+1)), binpow(4, mod-2)));
		ans = add(ans, mult(sum, w[i]));
		ans = add(ans, mult(mult(additional, w[i]), mult(h[i], mult(h[i]+1, binpow(2, mod-2)))));
		sum = add(sum, mult(mult(h[i], mult(h[i]+1, binpow(2, mod-2))), add(additional, w[i])));
		st.push(mp(h[i], add(additional, w[i])));
	}
	
	cout << ans;
	
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 41 ms 1260 KB Output is correct
4 Correct 81 ms 2468 KB Output is correct
5 Correct 88 ms 2156 KB Output is correct
6 Correct 83 ms 1868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 9 ms 460 KB Output is correct
3 Correct 44 ms 1604 KB Output is correct
4 Correct 87 ms 2672 KB Output is correct
5 Correct 88 ms 2628 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 9 ms 460 KB Output is correct
4 Correct 44 ms 1520 KB Output is correct
5 Correct 89 ms 2668 KB Output is correct
6 Correct 90 ms 2696 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 9 ms 576 KB Output is correct
9 Correct 44 ms 1480 KB Output is correct
10 Correct 84 ms 2624 KB Output is correct
11 Correct 86 ms 2584 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 42 ms 1840 KB Output is correct
12 Correct 82 ms 3624 KB Output is correct
13 Correct 83 ms 3268 KB Output is correct
14 Correct 82 ms 3052 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 10 ms 716 KB Output is correct
17 Correct 43 ms 2484 KB Output is correct
18 Correct 86 ms 4580 KB Output is correct
19 Correct 89 ms 4640 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 10 ms 716 KB Output is correct
22 Correct 44 ms 2404 KB Output is correct
23 Correct 86 ms 4420 KB Output is correct
24 Correct 86 ms 4580 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 380 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 348 KB Output is correct
30 Correct 9 ms 692 KB Output is correct
31 Correct 9 ms 696 KB Output is correct
32 Correct 43 ms 2000 KB Output is correct
33 Correct 45 ms 2064 KB Output is correct
34 Correct 88 ms 3592 KB Output is correct
35 Correct 86 ms 3624 KB Output is correct
36 Correct 90 ms 3808 KB Output is correct
37 Correct 89 ms 3804 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 87 ms 3732 KB Output is correct
40 Correct 88 ms 3752 KB Output is correct
41 Correct 88 ms 3880 KB Output is correct
42 Correct 88 ms 4232 KB Output is correct