結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー carrot46carrot46
提出日時 2020-04-18 12:32:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 301 ms / 3,500 ms
コード長 3,364 bytes
コンパイル時間 1,900 ms
コンパイル使用メモリ 177,068 KB
実行使用メモリ 18,048 KB
最終ジャッジ日時 2024-04-14 20:45:01
合計ジャッジ時間 13,831 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 9 ms
6,944 KB
testcase_04 AC 9 ms
6,940 KB
testcase_05 AC 9 ms
6,940 KB
testcase_06 AC 9 ms
6,940 KB
testcase_07 AC 8 ms
6,940 KB
testcase_08 AC 9 ms
6,940 KB
testcase_09 AC 9 ms
6,940 KB
testcase_10 AC 9 ms
6,944 KB
testcase_11 AC 226 ms
11,612 KB
testcase_12 AC 213 ms
11,448 KB
testcase_13 AC 292 ms
13,344 KB
testcase_14 AC 227 ms
12,060 KB
testcase_15 AC 271 ms
12,880 KB
testcase_16 AC 224 ms
11,392 KB
testcase_17 AC 262 ms
12,800 KB
testcase_18 AC 252 ms
12,640 KB
testcase_19 AC 272 ms
12,920 KB
testcase_20 AC 280 ms
13,592 KB
testcase_21 AC 265 ms
12,660 KB
testcase_22 AC 220 ms
11,520 KB
testcase_23 AC 228 ms
11,804 KB
testcase_24 AC 266 ms
12,956 KB
testcase_25 AC 293 ms
13,424 KB
testcase_26 AC 268 ms
12,604 KB
testcase_27 AC 238 ms
11,964 KB
testcase_28 AC 301 ms
13,436 KB
testcase_29 AC 291 ms
13,624 KB
testcase_30 AC 284 ms
13,516 KB
testcase_31 AC 290 ms
13,440 KB
testcase_32 AC 293 ms
13,440 KB
testcase_33 AC 290 ms
13,492 KB
testcase_34 AC 202 ms
18,048 KB
testcase_35 AC 206 ms
13,952 KB
testcase_36 AC 200 ms
14,336 KB
testcase_37 AC 208 ms
13,824 KB
testcase_38 AC 195 ms
14,264 KB
testcase_39 AC 184 ms
13,312 KB
testcase_40 AC 188 ms
13,532 KB
testcase_41 AC 194 ms
13,724 KB
testcase_42 AC 189 ms
14,080 KB
testcase_43 AC 180 ms
13,312 KB
testcase_44 AC 174 ms
13,184 KB
testcase_45 AC 173 ms
12,940 KB
testcase_46 AC 207 ms
14,592 KB
testcase_47 AC 204 ms
14,464 KB
testcase_48 AC 196 ms
15,536 KB
testcase_49 AC 212 ms
14,516 KB
testcase_50 AC 202 ms
14,336 KB
testcase_51 AC 211 ms
13,440 KB
testcase_52 AC 209 ms
13,516 KB
testcase_53 AC 212 ms
13,568 KB
testcase_54 AC 2 ms
6,940 KB
testcase_55 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,K,Q,A,B;
string S;
const ll MOD = 998244353;
//const ll MOD = (1e+9) + 7;
const ll INF = 1LL<<60;
typedef pair<ll, ll> P;

int main() {
    class BIT{
            ll n;
      		vector<ll> bitree;
        public:
        	BIT(unsigned long _n) : bitree(_n + 1, 0){
              n = _n;
            }
        	void add(ll id, ll x){
              while(id <= n){
                bitree[id] += x;
                id += id & -id;
              }
              return;
            }
        	ll sum(ll id){
              ll temp = 0;
              while(id > 0){
                temp += bitree[id];
                id -= id & -id;
              }
              return temp;
            }
      };
    class SEGTREE{
                ll n, dflt;
          		vector<ll> dat;
            public:
            	SEGTREE(unsigned long _n, ll _a) : dat(_n * 4, _a){
            	    n = 1;
            	    dflt = _a;
            	    while(n < _n) n *= 2;
                }
                void update(ll k, ll a){
                    k += n;
                    dat[k] = a;
                    while(k > 0){
                        k /= 2;
                        dat[k] = max(dat[k * 2], dat[k * 2 + 1]);
                    }
                }
                void init(vector<ll> &v){
            	    for(int i = 0; i < v.size(); ++i) dat[i + n] = v[i];
            	    for(int i = n - 1; i >= 1; --i) {
            	        dat[i] = max(dat[i * 2], dat[i * 2 + 1]);
            	    }
            	}
                ll Q(ll a, ll b){
            	    return query(a, b, 1, 0, n);
            	}
                ll query(ll a, ll b, ll k, ll l, ll r){
                    if(r <= a || b <= l) return dflt;
                    if(a <= l && r <= b) return dat[k];
                    ll vl = query(a, b, k * 2, l, (l + r) / 2);
                    ll vr = query(a, b, k * 2 + 1, (l + r) / 2, r);
                    return max(vl, vr);
                }
                //単項にアクセスする[]だが、書き換え厳禁
                //一応後でinitすれば書き換えてもよさそうだが…
                ll & operator [](int k) {return dat[k + n];}
        };
    cin>>N;
    vec p(N + 2), p_inv(N + 1);
    p[0] = p[N + 1] = N;
    p_inv[N] = 0;
    reps(i,1,N+1) {
        cin>>p[i];
        --p[i];
        p_inv[p[i]] = i;
    }
    ll ans = 0;
    rep(rp,2){
        BIT bit(N + 2);
        SEGTREE seg(N + 2, 0);
        set<ll> st1, st2;
        st1.insert(N);
        st2.insert(-1);
        reps(i,1,N+1){
            while(*st2.rbegin() > p[i]){
                bit.add(p_inv[*st2.rbegin()], -1);
                st2.erase(*st2.rbegin());
            }
            ans += bit.sum(i - 1) - bit.sum(seg.Q(*st1.upper_bound(p[i]), N + 1));
            bit.add(i, 1);
            seg.update(p[i], i);
            st1.insert(p[i]);
            st2.insert(p[i]);
        }
        reverse(ALL(p));
        reps(i,1,N + 1) p_inv[p[i]] = i;
    }
    cout<<ans<<endl;
}
0