Submission #945430

# Submission time Handle Problem Language Result Execution time Memory
945430 2024-03-13T19:06:22 Z amirhoseinfar1385 Fancy Fence (CEOI20_fancyfence) C++17
0 / 100
4 ms 4816 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=1;
	}
	void ins(long long u){
		par[u]=u;
		wh[u]=w[u];
		res*=c2(w[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*=mypow(c2(wh[rootu]+1),mod-2);
			res%=mod;
			res*=mypow(c2(wh[rootv]+1),mod-2);
			res%=mod;
			wh[rootu]+=wh[rootv];
			res*=c2(wh[rootu]+1);
			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();
		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;
	}
	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();
}

Compilation message

fancyfence.cpp: In function 'int main()':
fancyfence.cpp:101:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |  freopen("inp.txt","r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 4816 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 4704 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 4700 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 4700 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 4700 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 4700 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 4816 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -