Submission #592133

#TimeUsernameProblemLanguageResultExecution timeMemory
592133jasminFancy Fence (CEOI20_fancyfence)C++14
100 / 100
206 ms28972 KiB
#include<bits/stdc++.h> using namespace std; #define int long long const int mod=1e9+7; struct node{ int left=0; int right=0; int ans=0; int sum=1; }; node zero={0, 0, 0, 0}; struct segtree{ vector<node> tree; segtree(int n){ tree.resize(n*4); build(0, n, 0); }; node build(int l, int r, int v){ if(l+1==r){ tree[v].left=tree[v].right=0; tree[v].sum=1; tree[v].ans=0; return tree[v]; } int m=l+(r-l)/2; return tree[v]=merge(build(l, m, v*2+1), build(m, r, v*2+2)); } node merge(node a, node b){ node ret; ret.sum=(a.sum+b.sum)%mod; ret.ans=(a.ans+b.ans)%mod; ret.left=a.left; ret.right=b.right; int middle=(a.right+b.left)%mod; if(a.left==a.sum){ middle=0; ret.left=(a.sum+b.left)%mod; } if(b.right==b.sum){ middle=0; ret.right=(a.right+b.sum)%mod; } ret.ans+=(middle*(middle+1)/2)%mod; ret.ans%=mod; return ret; } node update(int l, int r, int v, int x, int val){ //cout << "update " << l << " " << r << " " << v << " " << x << " " << val << "\n"; if(x<l || r<=x) { //cout << l << " " << r << " " << v << ": " << tree[v].left << " " << tree[v].right << " " << tree[v].sum << " " << tree[v].ans << "\n"; return tree[v]; } if(l+1==r){ tree[v].left=val; tree[v].right=val; tree[v].sum=val; tree[v].ans=0; //cout << l << " " << r << " " << v << ": " << tree[v].left << " " << tree[v].right << " " << tree[v].sum << " " << tree[v].ans << "\n"; return tree[v]; } int m=l+(r-l)/2; tree[v]=merge(update(l, m, v*2+1, x, val), update(m, r, v*2+2, x, val)); //cout << l << " " << r << " " << v << ": " << tree[v].left << " " << tree[v].right << " " << tree[v].sum << " " << tree[v].ans << "\n"; return tree[v]; } node query(int l, int r, int v, int ql, int qr){ if(qr<=l || r<=ql) return zero; if(ql<=l && r<=qr) return tree[v]; int m=l+(r-l)/2; return merge(query(l, m, v*2+1, ql, qr), query(m, r, v*2+2, ql, qr)); } }; signed main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> h(n); map<int,int> ind; int x=1; vector<pair<int, vector<int> > >sorted; sorted.push_back({0, {}}); for(int i=0; i<n; i++){ cin >> h[i]; if(ind[h[i]]==0){ sorted.push_back({h[i], {}}); ind[h[i]]=x; x++; } sorted[ind[h[i]]].second.push_back(i); } sort(sorted.begin(), sorted.end()); reverse(sorted.begin(), sorted.end()); vector<int> w(n); for(int i=0; i<n; i++){ cin >> w[i]; } int ans=0; segtree seg(n); for(int i=0; i<(int)sorted.size()-1; i++){ int h=sorted[i].first; int next=sorted[i+1].first; vector<int> ele=sorted[i].second; for(auto e: ele){ seg.update(0, n, 0, e, w[e]); } node res=seg.query(0, n, 0, 0, n); //cout << res.ans << " " << res.left << " " << res.right << "\n"; int s=res.ans; s+= (res.left*(res.left+1)/2)%mod; s%=mod; if(res.right!=res.sum){ s+=(res.right*(res.right+1)/2)%mod; s%=mod; } int factor=((h*(h+1))/2 - (next*(next+1))/2)%mod; //cout << factor << " " << s << "\n"; ans+=(s*factor)%mod; ans%=mod; } cout << ans << "\n"; }
#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...