結果

問題 No.956 Number of Unbalanced
ユーザー nmnmnmnmnmnmnmnmnmnmnmnmnmnm
提出日時 2019-12-09 20:58:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 276 ms / 2,000 ms
コード長 2,926 bytes
コンパイル時間 1,341 ms
コンパイル使用メモリ 101,348 KB
実行使用メモリ 40,564 KB
最終ジャッジ日時 2023-09-05 09:48:33
合計ジャッジ時間 8,940 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
39,112 KB
testcase_01 AC 48 ms
39,504 KB
testcase_02 AC 65 ms
39,120 KB
testcase_03 AC 66 ms
39,088 KB
testcase_04 AC 65 ms
39,112 KB
testcase_05 AC 65 ms
39,500 KB
testcase_06 AC 202 ms
40,200 KB
testcase_07 AC 230 ms
40,292 KB
testcase_08 AC 196 ms
40,432 KB
testcase_09 AC 174 ms
40,556 KB
testcase_10 AC 210 ms
40,120 KB
testcase_11 AC 246 ms
39,968 KB
testcase_12 AC 204 ms
40,024 KB
testcase_13 AC 153 ms
40,120 KB
testcase_14 AC 273 ms
39,692 KB
testcase_15 AC 149 ms
40,076 KB
testcase_16 AC 183 ms
39,928 KB
testcase_17 AC 127 ms
40,120 KB
testcase_18 AC 276 ms
39,676 KB
testcase_19 AC 155 ms
40,080 KB
testcase_20 AC 273 ms
39,660 KB
testcase_21 AC 155 ms
40,120 KB
testcase_22 AC 141 ms
40,016 KB
testcase_23 AC 182 ms
39,860 KB
testcase_24 AC 154 ms
40,108 KB
testcase_25 AC 222 ms
40,564 KB
testcase_26 AC 161 ms
40,556 KB
testcase_27 AC 195 ms
40,500 KB
testcase_28 AC 216 ms
40,432 KB
testcase_29 AC 270 ms
39,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
 
using namespace std;
 
typedef long long ll;
 
#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define per(i,a,b) for(ll i=b-1LL;i>=(a);--i)
#define clr(a, b) memset((a), (b) ,sizeof(a))
#define ctos(c) string(1,c)
#define print(x) cout<<#x<<" = "<<x<<endl;
 
#define MOD 1000000007

template <class T>
struct time_array{
 private:
  int t;
  vector<int> ts;
  vector<T> v;
 public:
  time_array() {}
  time_array(int n) : t(0), ts(n, 0), v(n) { }
  T& operator[](int i) {
    if (ts[i] < t) {
      ts[i] = t;
      v[i] = T();
    }
    return v[i];
  }
  int size() const {
    return v.size();
  }
  void clear() {
    ++t;
  }
};

struct segsum{
  long long nn;
  time_array<ll> dat1;
  time_array<ll> dat2;

  segsum(int n): dat1(n), dat2(n){}

  void init(long long n1){
    nn = 1;
    while(nn < n1)nn *= 2;
    dat1.clear();
    dat2.clear();
  }
  void add(long long a, long long b, long long x, long long k, long long l, long long r) {
    if (a <= l && r <= b) {
      dat1[k] += x;
    }
    else if (l < b && a < r) {
      dat2[k] += (min(b, r) - max(a, l)) * x;
      add(a, b, x, k * 2 + 1, l, (l + r) / 2);
      add(a, b, x, k * 2 + 2, (l + r) / 2, r);
    }
  }
  long long sum(long long a, long long b, long long k, long long l, long long r){
    if (b <= l || r <= a)return 0LL;
    else if (a <= l && r <= b) {
      return (dat1[k] * (r - l) + dat2[k]);
    }
    else {
      long long ret;
      ret = (min(b, r) - max(a, l)) * dat1[k];
      ret += sum(a, b, k * 2 + 1, l, (l + r) / 2);
      ret += sum(a, b, k * 2 + 2, (l + r) / 2, r);
      return ret;
    }
  }
  void add(long long a, long long b, long long x){
    return add(a,b,x,0,0,nn);
  }
  long long sum(long long a, long long b){
    return sum(a,b,0,0,nn);
  }
};

int main(){
  ll n;
  cin>>n;
  vector<vector<ll> > vv(100100,vector<ll>());
  rep(i,0,n){
    ll a;
    cin>>a;
    a--;
    vv[a].pb(i);
  }
  ll ans = 0;
  time_array<ll> ta(1<<19);
  segsum segsum1(1<<20);
  rep(i,0,vv.sz){
    vv[i].pb(n);
    ll p = 1<<17;
    ta.clear();
    segsum1.init(1<<19);
    ll now = -1;
    rep(j,0,vv[i].sz){
      ll nex = vv[i][j];
      ll a = nex-now-1;
      ans += ta[p-2]-ta[p-a-2];
      segsum1.add(p-a+1,p+1,1);
      p -= a;
      if(j!=vv[i].sz-1){
        segsum1.add(p,p+1,1);
        ans += segsum1.sum(0,p+1);
        ta[p] = ta[p-1]+segsum1.sum(0,p+1);
        p++;
      }
      now = nex;
    } 
  }
  cout << ans << endl;
  return 0;
}
0