답안 #945671

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
945671 2024-03-14T06:13:34 Z hmm789 Fancy Fence (CEOI20_fancyfence) C++14
0 / 100
7 ms 9120 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long 
#define INF 1000000000000000000
#define MOD 1000000007

struct node {
	int s, e, m, mx, sm, lz;
	node *l, *r;
	node(int _s, int _e) {
		s = _s, e = _e, m = (s+e)/2, mx = sm = 0, lz = -1;
		l = nullptr; r = nullptr;
	}
	void prop() {
		if(lz == -1) return;
		sm = lz*(e-s+1) % MOD;
		mx = lz;
		if(s != e) {
			if(l == nullptr) l = new node(s, m);
			if(r == nullptr) r = new node(m+1, e);
			l->lz = lz;
			r->lz = lz;
		}
		lz = -1;
	}
	void rset(int x, int y, int val) {
		if(x > y) return;
		prop();
		if(x <= s && e <= y) {lz = val; return;}
		else if(x > m) {
			if(r == nullptr) r = new node(m+1, e);
			r->rset(x, y, val);
		} else if(y <= m) {
			if(l == nullptr) l = new node(s, m);
			l->rset(x, y, val);
		}
		else {
			if(l == nullptr) l = new node(s, m);
			if(r == nullptr) r = new node(m+1, e);
			l->rset(x, y, val), r->rset(x, y, val);
		}
		if(l != nullptr) l->prop(); 
		if(r != nullptr) r->prop();
		if(l == nullptr) sm = r->sm, mx = r->mx;
		else if(r == nullptr) sm = l->sm, mx = l->mx;
		else sm = (l->sm + r->sm)%MOD, mx = max(l->mx, r->mx);
	}
	int rsum(int x, int y) {
		if(x > y) return 0;
		prop();
		if(x <= s && e <= y) return sm;
		else if(x > m) return r==nullptr?0:r->rsum(x, y);
		else if(y <= m) return l==nullptr?0:l->rsum(x, y);
		else return ((l==nullptr?0:l->rsum(x, y)) + (r==nullptr?0:r->rsum(x, y))) % MOD;
	}
	int rmax(int x, int y) {
		if(x > y) return -INF;
		prop();
		if(x <= s && e <= y) return mx;
		else if(x > m) return r==nullptr?-INF:r->rmax(x, y);
		else if(y <= m) return l==nullptr?-INF:l->rmax(x, y);
		else return max((l==nullptr?-INF:l->rmax(x, y)), (r==nullptr?-INF:r->rmax(x, y)));
	}
} *root;

int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	int h[n], w[n], dp[n], dpsm = 0;
	for(int i = 0; i < n; i++) cin >> h[i];
	for(int i = 0; i < n; i++) cin >> w[i];
	root = new node(0, INF);
	int cur = 0;
	for(int i = 0; i < n; i++) {
		int l = 0, r = cur, m;
		while(l < r) {
			m = (l+r)/2;
			if(root->rmax(m, i) <= h[i]) l = m+1;
			else r = m;
		}
		root->rset(l, cur-1, (h[i]+1)*h[i]/2 % MOD);
		//cout << "a " << i << " " << l << " "  << cur << endl;
		int num = (w[i]+1)*w[i]/2 % MOD, num2 = (h[i]+1)*h[i]/2 % MOD;
		dp[i] = num * num2 % MOD + dpsm + w[i]*root->rsum(0, cur-1);
		dp[i] %= MOD;
		dpsm += dp[i];
		dpsm %= MOD;
		cur += w[i];
		root->rset(cur-w[i], cur-1, (h[i]+1)*h[i]/2 % MOD);
		//cout << i << " " << dp[i] << endl;
	}
	cout << dp[n-1];
}
	
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 7 ms 9048 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 7 ms 9120 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 9052 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Incorrect 7 ms 9116 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 7 ms 9024 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 7 ms 9048 KB Output isn't correct
3 Halted 0 ms 0 KB -