답안 #959940

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
959940 2024-04-09T10:40:35 Z penguin133 Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
230 ms 42184 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pi pair <int, int> 
#define pii pair <int, pi>

int n, H[1000005], W[1000005], P[1000005], A[100005];
stack <pi> s;
const int mod = 1e9 + 7;
vector <int> v;
struct node{
	int s, e, m, val, lz, val2;
	node *l, *r;
	node(int _s, int _e){
		s = _s, e = _e, m = (s + e) >> 1;
		val = lz = val2 = 0;
		l = r = nullptr;
	}
	
	void mc(){
		if(s == e || l != nullptr)return;
		l = new node(s, m);
		r = new node(m+1, e);
	}
	
	void prop(){
		if(lz){
			int brr = v[e] * (v[e] + 1) / 2;
			brr %= mod;
			int brr2 = v[s - 1] * (v[s - 1] + 1) / 2;
			brr2 %= mod;
			brr = (brr - brr2 + mod) % mod;
			val = lz * (brr) % mod;
			if(s != e){
				mc();
				l->lz = lz; r->lz = lz;
			}
			lz = 0;
		}
	}
	
	void upd(int a, int b, int c){
		if(a > b)return;
		prop();
		if(a == s && b == e)lz = c;
		else{
			mc();
			if(b <= m)l->upd(a, b, c);
			else if(a > m)r->upd(a, b, c);
			else l->upd(a, m, c), r->upd(m+1, b, c);
			l->prop(), r->prop();
			val = l->val + r->val;
			val %= mod;
		}
	}
	
	int qry(int a, int b){
		if(a > b)return 0;
		prop();
		if(a == s && b == e)return val;
		if(l == nullptr)return 0;
		if(b <= m)return l->qry(a, b);
		if(a > m)return r->qry(a, b);
		return (l->qry(a, m) + r->qry(m+1, b)) % mod;
	}
}*root;

void solve(){
	cin >> n;
	v.push_back(1e9);
	v.push_back(0);
	for(int i = 1; i <= n; i++)cin >> H[i], v.push_back(H[i]), v.push_back(H[i] + 1);
	for(int i = 1; i <= n; i++)cin >> W[i];
	sort(v.begin(), v.end());
	v.erase(unique(v.begin(), v.end()), v.end());
	root = new node(1, (int)v.size());
	s.push({0, 0});
	int ans = 0;
	root = new node(0, 1e9);
	for(int i = 1; i <= n; i++){
		P[i] = P[i - 1] + W[i];
		P[i] %= mod;
		int x = lower_bound(v.begin(), v.end(), H[i] + 1) - v.begin();
		root->upd(x, (int)v.size(), P[i]);
		//for(int j = H[i] + 1; j <= 100; j++)lst[j] = P[i];
		/*
		for(int j = 1; j <= H[i]; j++){
			ans += j * W[i] % mod * (P[i - 1] - lst[j] + mod) % mod;
			ans %= mod;
		}
		*/
		root->prop();
		x--;
		int tot = root->qry(1, x);
		int tot2 = H[i] * (H[i] + 1) / 2;
		tot2 %= mod;
		tot2 *= P[i - 1];
		tot2 %= mod;
		tot2 -= tot;
		tot2 = (tot2 + mod) % mod;
		ans += tot2 * W[i] % mod;
		ans %= mod;
		int a = W[i] * (W[i] + 1) / 2;
		a %= mod;
		int b = H[i] * (H[i] + 1) / 2;
		b %= mod;
		ans += (a * b % mod);
		ans %= mod;
		//cout << ans << '\n';
	}
	cout << ans;
}

main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int tc = 1;
	//cin >> tc;
	while(tc--)solve();
}

Compilation message

fancyfence.cpp:116:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  116 | main(){
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 29 ms 7896 KB Output is correct
4 Correct 51 ms 8904 KB Output is correct
5 Correct 52 ms 9060 KB Output is correct
6 Correct 50 ms 8900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 6 ms 4928 KB Output is correct
3 Correct 27 ms 7696 KB Output is correct
4 Correct 53 ms 8852 KB Output is correct
5 Correct 50 ms 8852 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 6 ms 4828 KB Output is correct
4 Correct 27 ms 7928 KB Output is correct
5 Correct 53 ms 8904 KB Output is correct
6 Correct 60 ms 8904 KB Output is correct
7 Correct 2 ms 4696 KB Output is correct
8 Correct 12 ms 8160 KB Output is correct
9 Correct 48 ms 14708 KB Output is correct
10 Correct 126 ms 41416 KB Output is correct
11 Correct 156 ms 42176 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 2 ms 4700 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4568 KB Output is correct
13 Correct 2 ms 4700 KB Output is correct
14 Correct 2 ms 4744 KB Output is correct
15 Correct 2 ms 4700 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 2 ms 4700 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4504 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 29 ms 8400 KB Output is correct
12 Correct 53 ms 9928 KB Output is correct
13 Correct 52 ms 9684 KB Output is correct
14 Correct 57 ms 9672 KB Output is correct
15 Correct 1 ms 4440 KB Output is correct
16 Correct 6 ms 5088 KB Output is correct
17 Correct 36 ms 8656 KB Output is correct
18 Correct 54 ms 10184 KB Output is correct
19 Correct 53 ms 10216 KB Output is correct
20 Correct 2 ms 4696 KB Output is correct
21 Correct 12 ms 8156 KB Output is correct
22 Correct 47 ms 14768 KB Output is correct
23 Correct 126 ms 41412 KB Output is correct
24 Correct 128 ms 42184 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 2 ms 4700 KB Output is correct
28 Correct 2 ms 4568 KB Output is correct
29 Correct 2 ms 4696 KB Output is correct
30 Correct 15 ms 7648 KB Output is correct
31 Correct 16 ms 8160 KB Output is correct
32 Correct 38 ms 8720 KB Output is correct
33 Correct 95 ms 24172 KB Output is correct
34 Correct 206 ms 38852 KB Output is correct
35 Correct 83 ms 10696 KB Output is correct
36 Correct 230 ms 42004 KB Output is correct
37 Correct 166 ms 23236 KB Output is correct
38 Correct 1 ms 4440 KB Output is correct
39 Correct 145 ms 19908 KB Output is correct
40 Correct 175 ms 40904 KB Output is correct
41 Correct 143 ms 41672 KB Output is correct
42 Correct 124 ms 39876 KB Output is correct