Submission #851296

# Submission time Handle Problem Language Result Execution time Memory
851296 2023-09-19T12:38:53 Z Jawad_Akbar_JJ Fancy Fence (CEOI20_fancyfence) C++14
100 / 100
255 ms 31720 KB
#include <bits/stdc++.h>
 
using namespace std;
#define int long long
const int N = 1<<17;
int r[N][3];
int w[N];
int h[N];
int pre[N];
int ans = 0;
int mod = 1e9 + 7;

struct node{
	int l,r;
	node* left;
	node* right;
	int ind;
	bool lft,rgt;

	node(int ll,int rr){
		l = ll;
		r = rr;
		left = right = NULL;
		lft = rgt = false;
		ind = 0;
	}

	void insert(int i){
		if(i<l or i>=r)
			return;
		if (r-l==1){
			ind = i;
			return; 
		}
		if (!lft)
			left = new node(l,(l+r)/2);
		if (!rgt)
			right = new node((l+r)/2,r);
		lft = rgt = true;
		left->insert(i);
		right->insert(i);
		if (h[left->ind]<h[right->ind])
			ind = left->ind;
		else
			ind = right->ind;
	}

	int get(int ll,int rr){
		// if (ll==rr)
		// 	cout<<ll<<" "<<rr<<endl;
		ll = max(ll,l);
		rr = min(rr,r);
		if (ll>=r or rr<=l)
			return 0;
		if (l==ll and r==rr)
			return ind;
		int lind = 0;
		int rind = 0;
		if (lft)
			lind = left->get(ll,rr);
		if (rgt)
			rind = right->get(ll,rr);
		if (h[lind]<h[rind])
			return lind;
		return rind;
	}
};



int power(int a,int b){
	if (b==0)
		return 1;
	int ans = power(a,b/2);
	ans = ans*ans;
	ans %= mod;
	if (b%2)
		ans = ans*a;
	return ans%mod;
}
 
int mod_inv(int k){
	return power(k,mod-2);
}
 
int asd(int a){
	int ans = (a*(a+1));
	ans = ans%mod;
	ans = ans*mod_inv(2);
	ans = ans% mod;
	return ans;
}

int answer(int W,int H){
	int ans = asd(W);
	int ans2 = asd(H);
	return (ans*ans2)%mod ;
}
node* root;
void solve(int l,int r,int height){
	if (r<l or l>r)
		return;
	int ind = root->get(l,r+1);
	int width = pre[r]-pre[l-1];
	if (width<0)
		width += mod;
	ans -= answer(width,height);
	if (ans<0)
		ans += mod;
	ans += answer(width,h[ind]);
	ans %= mod;
	solve(l,ind-1,h[ind]);
	solve(ind+1,r,h[ind]);
}

signed main(){
	h[0] = 2e9;
	root = new node(1,N+1);
	int n;
	cin>>n;
	for (int i=1;i<=n;i++){
		cin>>h[i];
		root->insert(i);
	}
	for (int i=1;i<=n;i++){
		cin>>w[i];
		pre[i] = pre[i-1] + w[i];
		pre[i] %= mod;
	}
	solve(1LL,n,0LL);

	cout<<ans<<endl;

}



# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 3 ms 2648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 3 ms 2652 KB Output is correct
3 Correct 110 ms 13336 KB Output is correct
4 Correct 224 ms 27216 KB Output is correct
5 Correct 221 ms 23392 KB Output is correct
6 Correct 223 ms 19604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2652 KB Output is correct
2 Correct 24 ms 4988 KB Output is correct
3 Correct 120 ms 16976 KB Output is correct
4 Correct 255 ms 29556 KB Output is correct
5 Correct 248 ms 29776 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 3 ms 2648 KB Output is correct
3 Correct 27 ms 4956 KB Output is correct
4 Correct 122 ms 17016 KB Output is correct
5 Correct 245 ms 29524 KB Output is correct
6 Correct 241 ms 29720 KB Output is correct
7 Correct 3 ms 2396 KB Output is correct
8 Correct 24 ms 3676 KB Output is correct
9 Correct 122 ms 14604 KB Output is correct
10 Correct 233 ms 17216 KB Output is correct
11 Correct 236 ms 17232 KB Output is correct
12 Correct 0 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 3 ms 2648 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 3 ms 2652 KB Output is correct
9 Correct 3 ms 2748 KB Output is correct
10 Correct 3 ms 2648 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 2 ms 2396 KB Output is correct
13 Correct 3 ms 2396 KB Output is correct
14 Correct 3 ms 2392 KB Output is correct
15 Correct 4 ms 2396 KB Output is correct
16 Correct 1 ms 2408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 3 ms 2648 KB Output is correct
3 Correct 1 ms 2408 KB Output is correct
4 Correct 3 ms 2408 KB Output is correct
5 Correct 1 ms 2408 KB Output is correct
6 Correct 1 ms 2408 KB Output is correct
7 Correct 1 ms 2600 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 3 ms 2652 KB Output is correct
11 Correct 110 ms 13288 KB Output is correct
12 Correct 222 ms 27308 KB Output is correct
13 Correct 218 ms 23388 KB Output is correct
14 Correct 218 ms 19540 KB Output is correct
15 Correct 3 ms 2648 KB Output is correct
16 Correct 25 ms 5264 KB Output is correct
17 Correct 119 ms 18000 KB Output is correct
18 Correct 239 ms 31568 KB Output is correct
19 Correct 242 ms 31720 KB Output is correct
20 Correct 3 ms 2648 KB Output is correct
21 Correct 25 ms 3932 KB Output is correct
22 Correct 128 ms 15584 KB Output is correct
23 Correct 233 ms 19044 KB Output is correct
24 Correct 243 ms 19116 KB Output is correct
25 Correct 1 ms 2468 KB Output is correct
26 Correct 2 ms 2396 KB Output is correct
27 Correct 3 ms 2652 KB Output is correct
28 Correct 3 ms 2512 KB Output is correct
29 Correct 3 ms 2652 KB Output is correct
30 Correct 24 ms 3932 KB Output is correct
31 Correct 24 ms 3932 KB Output is correct
32 Correct 121 ms 12092 KB Output is correct
33 Correct 118 ms 11920 KB Output is correct
34 Correct 247 ms 18908 KB Output is correct
35 Correct 240 ms 19024 KB Output is correct
36 Correct 236 ms 19148 KB Output is correct
37 Correct 238 ms 19160 KB Output is correct
38 Correct 1 ms 2396 KB Output is correct
39 Correct 248 ms 19084 KB Output is correct
40 Correct 237 ms 19164 KB Output is correct
41 Correct 243 ms 20536 KB Output is correct
42 Correct 241 ms 25424 KB Output is correct