# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1272999 | hihihihaw | Fancy Fence (CEOI20_fancyfence) | C++20 | 2 ms | 584 KiB |
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define pii pair<int,int>
#define sz(v) (int)v.size()
#define fi first
#define se second
#define INF 1223372036854775807
#define INF2 122337203
#define MOD 1000000007
#define cint(x) int x;cin>>x;
#define cinarr(a,n) int a[n];for (int i=0;i<n;i++) cin>>a[i];
#define coutarr(a) for (auto d:a)cout<<d<<" "; cout<<endl;
#define coutarrD(a) for (auto d:a) cout<<d.fi<<","<<d.se<<" "; cout<<endl;
#define BERKAY_TUP ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define endl '\n'
#define ld long double
#define mid (start+end)/2
#define vvi vector<vector<int>>
int t=1;
int interactive=0;
int usaco=0;
int testCase=0;
int tree[100023];
void update(int l,int r,int x){
r++;
while (l<100023){
tree[l]+=x;
l+=l&-l;
}
while (r<100023){
tree[r]-=x;
r+=r&-r;
}
}
int query(int x){
int ans=0;
while (x>0){
ans+=tree[x];
x-=x&-x;
}
return ans;
}
void solve(){
int n;
cin>>n;
cinarr(h,n);
cinarr(w,n);
vector<pii> hh;
int z[n];
int sm=0;
for (int i=0;i<n;i++){
hh.pb({h[i],i});
sm+=w[i];
z[i]=sm;
}
sort(hh.begin(),hh.end());
int nr[n];
int nl[n];
stack<pii> s;
for (int i=0;i<n;i++){
while (!s.empty() && s.top().fi>=h[i]) s.pop();
if (s.empty()) nl[i]=-1;
else nl[i]=s.top().se;
s.push({h[i],i});
}
stack<pii> s1;
for (int i=n-1;i>=0;i--){
while (!s1.empty() && s1.top().fi>=h[i]) s1.pop();
if (s1.empty()) nr[i]=n;
else nr[i]=s1.top().se;
s1.push({h[i],i});
}
int ans=0;
for (int i=0;i<n;i++){
int c=hh[i].se;
int g=query(c+1);
if (g==h[c]) continue;
int a=(((g+h[c])*(h[c]-g+1))/2-g)%MOD;
int b;
if (nl[c]==-1) b=z[nr[c]-1];
else b=z[nr[c]-1]-z[nl[c]];
b=((b*(b+1))/2)%MOD;
update(nl[c]+2,nr[c],h[c]-g);
ans=(ans+a*b)%MOD;
}
cout<<ans<<endl;
}
int32_t main(){
//BERKAY_TUP;
if (usaco){
freopen("team.in", "r", stdin);
freopen("team.out", "w", stdout);
}
if (!interactive){
#ifdef Local
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
//freopen("wormsort.out", "w", stdout);
#endif
}
if (t==1) solve();
else{
cin>>t;
while (t--){testCase++;solve();}
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |