답안 #945435

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
945435 2024-03-13T19:18:02 Z amirhoseinfar1385 Fancy Fence (CEOI20_fancyfence) C++17
100 / 100
32 ms 8536 KB
#include<bits/stdc++.h>
using namespace std;
const long long maxn=100000+10;
long long h[maxn],w[maxn],n,vis[maxn],mod=1e9+7;

void vorod(){
	cin>>n;
	for(long long i=0;i<n;i++){
		cin>>h[i];
	}
	for(long long i=0;i<n;i++){
		cin>>w[i];
	}
}
long long mainres=0;

long long mypow(long long m,long long y){
	if(y==0){
		return 1;
	}
	long long p=mypow(m,(y>>1));
	p*=p;
	p%=mod;
	if(y&1){
		p*=m;
		p%=mod;
	}
	return p;
}

long long c2(long long a){
	return (a*(a-1)/2)%mod;
}

struct dsu{
	long long par[maxn],res,wh[maxn];
	dsu(){
		res=0;
	}
	void ins(long long u){
		par[u]=u;
		wh[u]=w[u];
		res+=c2(wh[u]+1);
		res%=mod;
	}	
	long long root(long long u){
		if(par[u]==u){
			return u;
		}
		return par[u]=root(par[u]);
	}
	void con(long long u,long long v){
		long long rootu=root(u),rootv=root(v);
		if(rootu!=rootv){
			par[rootv]=rootu;
			res+=(wh[rootu]%mod)*(wh[rootv]%mod)%mod;
			wh[rootu]+=wh[rootv];
			res%=mod;
		}
	}
}ds;

void solve(){
	vector<pair<long long,long long>>sor;
	for(long long i=0;i<n;i++){
		sor.push_back(make_pair(h[i],i));
	}	
	sort(sor.begin(),sor.end());
	long long last=sor.back().first;
	while((long long)sor.size()>0){
		mainres+=ds.res*((mod+mod+c2(last+1)-c2(sor.back().first+1))%mod)%mod;
		mainres%=mod;
		auto x=sor.back();
	//	cout<<mainres<<" "<<x.first<<" "<<x.second<<" "<<last<<" "<<ds.res<<endl;
		sor.pop_back();
		vis[x.second]=1;
		ds.ins(x.second);
		if(vis[x.second+1]==1){
			ds.con(x.second,x.second+1);
		}
		if(vis[x.second-1]==1){
			ds.con(x.second,x.second-1);
		}
		last=x.first;
	}
	//cout<<"wtf: "<<last<<" "<<c2(last+1)<<" "<<ds.res<<endl;
	mainres+=ds.res*(c2(last+1))%mod;
	mainres%=mod;
}

void khor(){
	cout<<mainres<<"\n";
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
//	freopen("inp.txt","r",stdin);
	vorod();
	solve();
	khor();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2480 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 17 ms 5340 KB Output is correct
4 Correct 25 ms 7384 KB Output is correct
5 Correct 26 ms 7376 KB Output is correct
6 Correct 25 ms 7384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 3 ms 3160 KB Output is correct
3 Correct 14 ms 5340 KB Output is correct
4 Correct 28 ms 7896 KB Output is correct
5 Correct 30 ms 8140 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 3 ms 3172 KB Output is correct
4 Correct 15 ms 5224 KB Output is correct
5 Correct 27 ms 8144 KB Output is correct
6 Correct 29 ms 8140 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 4 ms 3164 KB Output is correct
9 Correct 14 ms 5344 KB Output is correct
10 Correct 32 ms 8000 KB Output is correct
11 Correct 27 ms 8144 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2392 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2392 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2392 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 0 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2484 KB Output is correct
11 Correct 13 ms 4964 KB Output is correct
12 Correct 27 ms 7652 KB Output is correct
13 Correct 26 ms 7384 KB Output is correct
14 Correct 25 ms 7380 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 4 ms 3176 KB Output is correct
17 Correct 14 ms 5340 KB Output is correct
18 Correct 27 ms 7976 KB Output is correct
19 Correct 28 ms 8152 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 3 ms 3164 KB Output is correct
22 Correct 14 ms 5340 KB Output is correct
23 Correct 28 ms 8144 KB Output is correct
24 Correct 27 ms 8148 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2392 KB Output is correct
28 Correct 1 ms 2392 KB Output is correct
29 Correct 1 ms 2396 KB Output is correct
30 Correct 4 ms 3196 KB Output is correct
31 Correct 4 ms 3164 KB Output is correct
32 Correct 16 ms 5340 KB Output is correct
33 Correct 16 ms 5340 KB Output is correct
34 Correct 31 ms 7896 KB Output is correct
35 Correct 32 ms 7896 KB Output is correct
36 Correct 32 ms 7940 KB Output is correct
37 Correct 32 ms 8188 KB Output is correct
38 Correct 1 ms 2392 KB Output is correct
39 Correct 32 ms 8152 KB Output is correct
40 Correct 31 ms 8148 KB Output is correct
41 Correct 29 ms 8096 KB Output is correct
42 Correct 28 ms 8536 KB Output is correct