답안 #757452

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
757452 2023-06-13T07:55:38 Z gagik_2007 Fancy Fence (CEOI20_fancyfence) C++17
12 / 100
3 ms 2776 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define ff first
#define ss second

ll ttt;
const ll INF=1e18;
const ll MOD=1e9+7;
const ll N=1e5+7;
ll n,m,k;
ll p[N];
ll sz[N];
ll ww[N];
ll ans[N];
vector<ll>h(N,0);
vector<ll>pf(N,0);
vector<ll>w(N,0);

ll calc_ans(ll w, ll h){
    ll res=1;
    if(w%2==0){
        res*=(w/2)%MOD;
        res%=MOD;
        res*=(w+1)%MOD;
        res%=MOD;
    }
    else{
        w++;
        res*=(w/2)%MOD;
        res%=MOD;
        res*=(w-1)%MOD;
        res%=MOD;
    }
    if(h%2==0){
        res*=(h/2)%MOD;
        res%=MOD;
        res*=(h+1)%MOD;
        res%=MOD;
    }
    else{
        h++;
        res*=(h/2)%MOD;
        res%=MOD;
        res*=(h-1)%MOD;
        res%=MOD;
    }
    return res;
}

void make_set(int v){
    p[v]=v;
    sz[v]=1;
    ww[v]=w[v];
    ans[v]=calc_ans(w[v],h[v]);
}

int find_set(int v){
    if(p[v]==v)return v;
    return p[v]=find_set(p[v]);
}

void union_sets(int v, int u){
    v=find_set(v);
    u=find_set(u);
    // cout<<v<<" "<<u<<endl;
    if(u!=v){
        if(sz[u]>sz[v])swap(u,v);
        p[u]=v;
        sz[v]+=sz[u];
        sz[u]=0;
        ww[v]+=ww[u];
        ww[u]=0;
        ans[v]+=ans[u];
        ans[u]=0;
    }
    // cout<<"SYO"<<endl;
}

void upd_ans(int v, ll new_ans){
    v=find_set(v);
    ans[v]=new_ans;
}

ll get_ww(int v){
    return ww[find_set(v)];
}

ll get_sum(int l, int r){
    if(l>r)return 0;
    if(l<0)l=0;
    if(r>n+1)r=n+1;
    if(l==0)return pf[r];
    return pf[r]-pf[l];
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>h[i];
    }
    for(int i=1;i<=n;i++){
        cin>>w[i];
        pf[i]=pf[i-1]+w[i];
    }
    vector<int>ord;
    for(int i=1;i<=n;i++){
        ord.push_back(i);
    }
    sort(ord.begin(),ord.end(),[](int x, int y){
        if(h[x]==h[y])return x<y;
        return h[x]>h[y];
    });
    // cout<<"OK"<<endl;
    for(int i:ord){
        // cout<<i<<endl;
        make_set(i);
        ll cw=w[i];
        ll cans=0;
        if(h[i-1]>=h[i]){
            cw+=get_ww(i-1);
            cans-=calc_ans(get_ww(i-1),h[i]);
        }
        if(h[i+1]>h[i]){
            cw+=get_ww(i+1);
            cans-=calc_ans(get_ww(i+1),h[i]);
        }
        cans+=calc_ans(cw,h[i]);
        // cout<<cans<<endl;
        while(cans<0)cans+=MOD;
        cans%=MOD;
        upd_ans(i,cans);
        // cout<<"OK"<<endl;
        if(h[i-1]>=h[i]){
            union_sets(i-1,i);
        }
        if(h[i+1]>h[i]){
            union_sets(i+1,i);
        }
    }
    cout<<ans[find_set(1)]<<endl;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2704 KB Output is correct
2 Incorrect 2 ms 2776 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Incorrect 3 ms 2644 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2644 KB Output is correct
2 Incorrect 2 ms 2688 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Incorrect 2 ms 2708 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2704 KB Output is correct
2 Incorrect 2 ms 2776 KB Output isn't correct
3 Halted 0 ms 0 KB -