답안 #527388

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
527388 2022-02-17T10:24:24 Z idas Fancy Fence (CEOI20_fancyfence) C++11
15 / 100
23 ms 1860 KB
#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(sz(pin)>1){
        for(auto i : pin){
            pin.erase(i); nin.erase(-i);
            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];
                }
            }
            pin.insert(i); nin.insert(-i);
        }
    }
    int in=*pin.begin();

    assert(sz(pin)==1);

    ans+=((h[in]+1)*h[in]/2%MOD)*((w[in]+1)*w[in]/2%MOD)%MOD;
    ans%=MOD;

    cout << ans;
}

Compilation message

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);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 460 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 460 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 460 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 11 ms 1100 KB Output is correct
4 Correct 20 ms 1860 KB Output is correct
5 Correct 23 ms 1820 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 460 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 460 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 460 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -