結果

問題 No.1371 交換門松列・松
ユーザー 沙耶花沙耶花
提出日時 2021-01-29 22:13:21
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 120 ms / 4,000 ms
コード長 5,781 bytes
コンパイル時間 2,179 ms
コンパイル使用メモリ 212,124 KB
実行使用メモリ 22,004 KB
最終ジャッジ日時 2023-09-09 15:39:42
合計ジャッジ時間 5,148 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 55 ms
22,004 KB
testcase_19 AC 56 ms
21,920 KB
testcase_20 AC 62 ms
21,916 KB
testcase_21 AC 60 ms
21,844 KB
testcase_22 AC 60 ms
21,932 KB
testcase_23 AC 120 ms
19,816 KB
testcase_24 AC 114 ms
20,056 KB
testcase_25 AC 113 ms
20,144 KB
testcase_26 AC 110 ms
20,072 KB
testcase_27 AC 111 ms
20,072 KB
testcase_28 AC 115 ms
19,924 KB
testcase_29 AC 110 ms
19,896 KB
testcase_30 AC 116 ms
19,952 KB
testcase_31 AC 114 ms
19,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>


#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <vector>

namespace atcoder {

// Reference: https://en.wikipedia.org/wiki/Fenwick_tree
template <class T> struct fenwick_tree {
    using U = internal::to_unsigned_t<T>;

  public:
    fenwick_tree() : _n(0) {}
    fenwick_tree(int n) : _n(n), data(n) {}

    void add(int p, T x) {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n) {
            data[p - 1] += U(x);
            p += p & -p;
        }
    }

    T sum(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }

  private:
    int _n;
    std::vector<U> data;

    U sum(int r) {
        U s = 0;
        while (r > 0) {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};

}  // namespace atcoder

using namespace atcoder;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000

bool is_kadomatsu(long long a,long long b,long long c){
	if(a==b||b==c||c==a)return false;
	if(b>a&&b>c)return true;
	if(b<a&&b<c)return true;
	return false;
}

int main(){
	
	int N;
	cin>>N;
	
	vector<int> A(N);
	vector<int> ind(N);
	rep(i,N){
		scanf("%d",&A[i]);
		A[i]--;
		ind[i] = i;
	}
	
	sort(ind.begin(),ind.end(),[&](int a,int b){
		return A[a]<A[b];
	});
	
	vector<bool> larger(N,false);
	
	rep(i,N){
		if(i==0){
			if(A[1]<A[0])larger[i] = true;
		}
		else{
			if(!larger[i-1])larger[i] = true;
		}
	}
	
	fenwick_tree<long long> F(N);
	vector<vector<int>> Del(N+1),Add(N+1);
	
	rep(i,N){
		if(larger[i]){
			int m = 0;
			if(i!=0)m = max(m,A[i-1]);
			if(i!=N-1)m = max(m,A[i+1]);
			m++;
			Add[m].push_back(i);
		}
		else{
			int m = Inf;
			if(i!=0)m = min(m,A[i-1]);
			if(i!=N-1)m = min(m,A[i+1]);
			Del[m].push_back(i);
			F.add(A[i],1);
		}
	}
	
	long long ans = 0LL;
	
	rep(i,N){
		int t = A[ind[i]];
		rep(j,Del[t].size()){
			F.add(A[Del[t][j]],-1);
		}
		Del[t].clear();
		rep(j,Add[t].size()){
			F.add(A[Add[t][j]],1);
		}
		Add[t].clear();
		
		int ii = ind[i];
		if(larger[ii]){
			int m = 0;
			if(ii!=0)m = max(m,A[ii-1]);
			if(ii!=N-1)m = max(m,A[ii+1]);
			m++;
			ans += F.sum(m,N);
		}
		else{
			int m = Inf;
			if(ii!=0)m = min(m,A[ii-1]);
			if(ii!=N-1)m = min(m,A[ii+1]);
			ans += F.sum(0,m);
		}
		
	}
	ans -= N;
	ans /= 2;
	cout<<ans<<endl;
	
	
		
	
    return 0;
}
0