結果

問題 No.1975 Zigzag Sequence
ユーザー asaringoasaringo
提出日時 2022-06-11 13:01:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 236 ms / 2,000 ms
コード長 2,784 bytes
コンパイル時間 2,499 ms
コンパイル使用メモリ 218,472 KB
実行使用メモリ 14,040 KB
最終ジャッジ日時 2023-10-21 20:46:17
合計ジャッジ時間 8,250 ms
ジャッジサーバーID
(参考情報)
judge9 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 22 ms
4,616 KB
testcase_04 AC 9 ms
4,348 KB
testcase_05 AC 222 ms
13,484 KB
testcase_06 AC 130 ms
8,340 KB
testcase_07 AC 24 ms
4,616 KB
testcase_08 AC 140 ms
8,340 KB
testcase_09 AC 202 ms
13,484 KB
testcase_10 AC 143 ms
8,340 KB
testcase_11 AC 157 ms
13,484 KB
testcase_12 AC 35 ms
5,900 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 162 ms
14,040 KB
testcase_19 AC 161 ms
14,040 KB
testcase_20 AC 174 ms
13,776 KB
testcase_21 AC 176 ms
13,776 KB
testcase_22 AC 165 ms
13,484 KB
testcase_23 AC 165 ms
13,484 KB
testcase_24 AC 179 ms
13,484 KB
testcase_25 AC 180 ms
13,484 KB
testcase_26 AC 236 ms
13,484 KB
testcase_27 AC 232 ms
13,484 KB
testcase_28 AC 173 ms
13,484 KB
testcase_29 AC 171 ms
13,484 KB
testcase_30 AC 172 ms
13,484 KB
testcase_31 AC 171 ms
13,484 KB
testcase_32 AC 164 ms
13,512 KB
testcase_33 AC 166 ms
13,512 KB
testcase_34 AC 218 ms
13,484 KB
testcase_35 AC 215 ms
13,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std ;
#define fast_input_output ios::sync_with_stdio(false); cin.tie(nullptr);
typedef long long ll ;
typedef long double ld ;
typedef pair<ll,ll> P ;
typedef tuple<ll,ll,ll> TP ;
#define chmin(a,b) a = min(a,b)
#define chmax(a,b) a = max(a,b)
#define bit_count(x) __builtin_popcountll(x)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a / gcd(a,b) * b
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define rrep(i,a,b) for(int i = a ; i < b ; i++)
#define endl "\n"

const int mod = 1000000007 ;

ll powmod(ll x , ll n){
    ll res = 1 ;
    while(n > 0){
        if(n & 1) (res *= x) %= mod ;
        (x *= x) %= mod ;
        n >>= 1 ;
    }
    return res ;
}

template<typename T>
struct SegmentTree{
    int n , n_;
    vector<T> dat ;
    SegmentTree(int _n){
        n_ = _n ;
        n = 1 ;
        while(n < _n) n *= 2 ;
        dat.resize(2 * n - 1,0) ;
    }
    void add(int k , T x){
        k += n - 1 ;
        (dat[k] += x) % mod ;
        while(k > 0){
            k = (k - 1) / 2 ;
            dat[k] = (dat[2*k+1] + dat[2*k+2]) % mod ;
        }
    }
    T sub_query(int a , int b , int k , int l , int r){
        if(r <= a || b <= l) return 0 ;
        if(a <= l && r <= b) return dat[k] ;
        T lef = sub_query(a,b,2*k+1,l,(l+r)/2) ;
        T rig = sub_query(a,b,2*k+2,(l+r)/2,r) ;
        return (lef + rig) % mod ;
    }
    T query(int a , int b){
        return sub_query(a,b,0,0,n) ;
    }
};

int n ;

int main(){
    fast_input_output
    cin >> n ;
    SegmentTree<ll> up_lef(n) , up_rig(n) , down_lef(n) , down_rig(n) ;
    vector<P> A ;
    rep(i,n){
        int a ;
        cin >> a ;
        A.push_back(P(a,i)) ;
    }
    sort(A.begin(),A.end()) ;
    ll res = 0 ;
    int i = 0 ;
    while(i < n){
        auto [val,id] = A[n-1-i] ;
        vector<int> vec ;
        while(i < n && val == A[n-1-i].first){
            auto [v,k] = A[n-1-i] ;
            vec.push_back(k) ;
            ll rig = down_rig.query(k+1,n) ;
            ll lef = down_lef.query(0,k) ;
            res += rig * lef % mod ;
            res %= mod ;
            i++ ;
        }
        for(int u : vec){
            down_rig.add(u,powmod(2,n-1-u)) ;
            down_lef.add(u,powmod(2,u)) ;
        }
    }
    i = 0 ;
    while(i < n){
        auto [val,id] = A[i] ;
        vector<int> vec ;
        while(i < n && val == A[i].first){
            auto [v,k] = A[i] ;
            vec.push_back(k) ;
            ll rig = up_rig.query(k+1,n) ;
            ll lef = up_lef.query(0,k) ;
            res += rig * lef % mod ;
            res %= mod ;
            i++ ;
        }
        for(int u : vec){
            up_rig.add(u,powmod(2,n-1-u)) ;
            up_lef.add(u,powmod(2,u)) ;
        }
    }
    cout << res << endl ;
}
0