結果

問題 No.956 Number of Unbalanced
ユーザー milanis48663220milanis48663220
提出日時 2023-07-20 22:56:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,946 bytes
コンパイル時間 2,702 ms
コンパイル使用メモリ 121,552 KB
実行使用メモリ 8,976 KB
最終ジャッジ日時 2023-10-21 01:07:56
合計ジャッジ時間 30,330 ms
ジャッジサーバーID
(参考情報)
judge10 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,348 KB
testcase_01 AC 4 ms
6,348 KB
testcase_02 AC 3 ms
6,352 KB
testcase_03 AC 3 ms
6,352 KB
testcase_04 AC 3 ms
6,352 KB
testcase_05 AC 3 ms
6,352 KB
testcase_06 AC 1,565 ms
7,560 KB
testcase_07 AC 1,745 ms
7,280 KB
testcase_08 AC 1,863 ms
7,528 KB
testcase_09 AC 1,272 ms
7,088 KB
testcase_10 TLE -
testcase_11 AC 751 ms
8,976 KB
testcase_12 AC 1,721 ms
7,540 KB
testcase_13 AC 183 ms
7,524 KB
testcase_14 AC 1,252 ms
8,348 KB
testcase_15 AC 300 ms
7,500 KB
testcase_16 AC 796 ms
7,932 KB
testcase_17 AC 185 ms
7,500 KB
testcase_18 AC 1,250 ms
8,356 KB
testcase_19 AC 193 ms
7,784 KB
testcase_20 AC 1,250 ms
8,356 KB
testcase_21 AC 183 ms
7,524 KB
testcase_22 AC 188 ms
7,500 KB
testcase_23 AC 797 ms
7,932 KB
testcase_24 AC 193 ms
7,488 KB
testcase_25 AC 1,869 ms
8,136 KB
testcase_26 AC 1,296 ms
8,032 KB
testcase_27 AC 1,057 ms
8,092 KB
testcase_28 AC 1,953 ms
8,144 KB
testcase_29 AC 1,250 ms
8,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

//1-indexed
template <typename T>
struct bit{
    int n;
    vector<T> data;

    bit(int n_){
        n = 1;
        while(n < n_) n *= 2;
        data = vector<T>(n+1);
        for(int i = 0; i <= n; i++) data[i] = 0;
    }
    
    T sum(int i){
        T ret = 0;
        while(i > 0){
            ret += data[i];
            i -= i&-i;
        }
        return ret;
    }

    void add(int i, T x){
        while(i <= n){
            data[i] += x;
            i += i&-i;
        }
    }
};

const int N = 100000;

int a[N];
vector<int> indices[N];

int count(int x, int l, int r){
    return lower_bound(indices[x].begin(), indices[x].end(), r) - lower_bound(indices[x].begin(), indices[x].end(), l);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; cin >> n;
    for(int i = 0; i < n; i++){
        int x; cin >> x; x--;
        a[i] = x;
        indices[x].push_back(i);
    }
    ll ans = 0;
    int th = sqrt(n);
    for(int x = 0; x < N; x++){
        if(indices[x].size() < th) continue;
        bit<int> bt(2*n+1);
        int sum = n+1;
        bt.add(sum, 1);
        for(int i = 0; i < n; i++){
            if(a[i] == x) sum++;
            else sum--;
            ans += bt.sum(sum-1);
            bt.add(sum, 1);
        }
    }
    for(int l = 0; l < n; l++){
        int r = l;
        int cnt = 0;
        int candidate = -1;
        while(r < l+th*2+10 && r < n){
            if(cnt == 0) candidate = a[r];
            if(a[r] == candidate) cnt++;
            else cnt--;
            r++;
            if(indices[candidate].size() < th) {
                int len = r-l;
                if(count(candidate, l, r)*2 > len) ans++;
            }
        }
    }
    cout << ans << endl;
}
0