Submission #527384

#TimeUsernameProblemLanguageResultExecution timeMemory
527384idasFancy Fence (CEOI20_fancyfence)C++11
43 / 100
139 ms15116 KiB
#include <bits/stdc++.h> #define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr) #define FOR(i, begin, end) for(int i = (begin); i < (end); i++) #define TSTS int ttt; cin >> ttt; while(ttt--) solve() #define all(x) (x).begin(), (x).end() #define le(vec) vec[vec.size()-1] #define sz(x) ((int)((x).size())) #define pb push_back #define s second #define f first using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef map<int, int> mii; typedef pair<int, int> pii; typedef pair<long long, long long> pll; typedef pair<long double, long double> pdd; const int INF=1e9, MOD=1e9+7, mod=998244353; const ll LINF=1e18; void setIO() { FAST_IO; } void setIO(string s) { FAST_IO; freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); } const int N=1e5+10; int n; ll h[N], w[N]; ll calc(ll hh, ll L, ll wd) { return ((2*hh-L+1)*L/2%MOD)*((wd+1)*wd/2%MOD)%MOD; } int main() { setIO(); cin >> n; FOR(i, 0, n) cin >> h[i]; FOR(i, 0, n) cin >> w[i]; ll lst=h[0], tot=0; vi nh, nw; FOR(i, 0, n) { if(h[i]==lst){ tot+=w[i]; tot%=MOD; } else{ nh.pb(lst); nw.pb(tot); lst=h[i]; tot=w[i]; } } nh.pb(lst); nw.pb(tot); n=sz(nh); FOR(i, 0, n) { h[i]=nh[i]; w[i]=nw[i]; } set<int> pin, nin; FOR(i, 0, n) { pin.insert(i); nin.insert(-i); } queue<int> q; vector<pii> hgts; FOR(i, 0, n) { hgts.pb({h[i],i}); } sort(all(hgts), greater<pii>()); FOR(i, 0, n) { q.push(hgts[i].s); } ll ans=0; while(!q.empty()){ int i=q.front(); q.pop(); //cout << i << endl; if(!pin.count(i)) continue; int l=-*nin.upper_bound(-i), r=*pin.upper_bound(i); bool canL=nin.upper_bound(-i)!=nin.end(), canR=pin.upper_bound(i)!=pin.end(); if(!canL && !canR) continue; else if(canR){ if(h[r]>h[i]) continue; ll dif=h[i]-h[r]; pin.erase(r); nin.erase(-r); ans+=calc(h[i], dif, w[i]); ans%=MOD; w[i]+=w[r]; w[i]%=MOD; h[i]=h[r]; } else if(canL){ if(h[l]>h[i]) continue; ll dif=h[i]-h[l]; pin.erase(l); nin.erase(-l); ans+=calc(h[i], dif, w[i]); ans%=MOD; w[i]+=w[l]; w[i]%=MOD; h[i]=h[l]; } else{ if(h[l]>h[i]||h[r]>h[i]) continue; if(h[l]>h[r]){ ll dif=h[i]-h[l]; pin.erase(l); nin.erase(-l); ans+=calc(h[i], dif, w[i]); ans%=MOD; w[i]+=w[l]; w[i]%=MOD; h[i]=h[l]; } else if(h[r]>h[l]){ ll dif=h[i]-h[r]; pin.erase(r); pin.erase(-r); ans+=calc(h[i], dif, w[i]); ans%=MOD; w[i]+=w[r]; w[i]%=MOD; h[i]=h[r]; } else{ ll dif=h[i]-h[l]; pin.erase(l); pin.erase(r); nin.erase(-l); nin.erase(-r); ans+=calc(h[i], dif, w[i]); w[i]+=w[r]; w[i]%=MOD; w[i]+=w[l]; w[i]%=MOD; h[i]=h[l]; } } q.push(i); } int in=*pin.begin(); ans+=((h[in]+1)*h[in]/2%MOD)*((w[in]+1)*w[in]/2%MOD)%MOD; ans%=MOD; cout << ans; }

Compilation message (stderr)

fancyfence.cpp: In function 'void setIO(std::string)':
fancyfence.cpp:32:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |     freopen((s+".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |     freopen((s+".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...