Submission #701521

#TimeUsernameProblemLanguageResultExecution timeMemory
701521primenumber_zzFancy Fence (CEOI20_fancyfence)C++14
100 / 100
64 ms8608 KiB
#include<bits/stdc++.h> using namespace std; constexpr int mod = 1000000007; void mpl(int &x,int y) { x += y; if(x >= mod) x -= mod; } struct UnionFind { vector<int>par,size,sum; UnionFind(int n,vector<int>w) { par.resize(n); size.resize(n); sum.resize(n); for(int i = 0; i < n; i++) { par[i] = i; sum[i] = w[i]; } } int find(int x) { if(par[x] == x) { return x; } return par[x] = find(par[x]); } void unite(int u,int v) { u = find(u); v = find(v); if(u == v) { return; } if(size[u] > size[v]) swap(u,v); size[v] += size[u]; mpl(sum[v],sum[u]); par[u] = v; } int consum(int x) { return sum[find(x)]; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<int>h(N),w(N),cmp; for(int i = 0; i < N; i++) { cin >> h[i]; cmp.push_back(h[i]); } for(int i = 0; i < N; i++) { cin >> w[i]; } cmp.push_back(0); sort(cmp.begin(),cmp.end()); cmp.erase(unique(cmp.begin(),cmp.end()),cmp.end()); vector<vector<int>>tmp(cmp.size()); for(int i = 0; i < N; i++) { h[i] = lower_bound(cmp.begin(),cmp.end(),h[i])-cmp.begin(); tmp[h[i]].push_back(i); } UnionFind uf(N,w); int ans = 0,sum = 0; for(int i = cmp.size()-1; i >= 1; i--) { for(int j = 0; j < tmp[i].size(); j++) { mpl(sum,1ll*(1+w[tmp[i][j]])*(w[tmp[i][j]])/2%mod); if(tmp[i][j] && h[tmp[i][j]-1] >= h[tmp[i][j]]) { int sum1 = uf.consum(tmp[i][j]-1); int sum2 = uf.consum(tmp[i][j]); uf.unite(tmp[i][j]-1,tmp[i][j]); mpl(sum,1ll*sum1*sum2%mod); } if(tmp[i][j]+1 < N && h[tmp[i][j]+1] > h[tmp[i][j]]) { int sum1 = uf.consum(tmp[i][j]); int sum2 = uf.consum(tmp[i][j]+1); uf.unite(tmp[i][j],tmp[i][j]+1); mpl(sum,1ll*sum1*sum2%mod); } } int res = 1ll*(1+cmp[i])*cmp[i]/2%mod; mpl(res,mod-1ll*(1+cmp[i-1])*cmp[i-1]/2%mod); mpl(ans,1ll*res*sum%mod); } cout << ans << "\n"; }

Compilation message (stderr)

fancyfence.cpp: In function 'int main()':
fancyfence.cpp:68:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |         for(int j = 0; j < tmp[i].size(); j++) {
      |                        ~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...