結果

問題 No.742 にゃんにゃんにゃん 猫の挨拶
ユーザー iqueue02iqueue02
提出日時 2020-01-08 16:33:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 2,500 ms
コード長 1,162 bytes
コンパイル時間 1,997 ms
コンパイル使用メモリ 167,936 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-15 00:37:33
合計ジャッジ時間 2,956 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 14 ms
4,380 KB
testcase_12 AC 13 ms
4,384 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;
const ll INF = (1LL<<60);
template <typename T> 
struct SegmentTree {
  
  int n;
  vector<T> node;
 
  T f(T a, T b) {return (a + b);}
 
  SegmentTree (int sz, T init = 0) {
    n = 1; while (n < sz) n <<= 1;
    node.assign(2*n, init);
  }
 
  void update(int k, T x) {
    k += (n - 1);
    node[k] = x;
    while (k > 0) {
      k = (k - 1) >> 1;
      node[k] = f(node[2*k+1], node[2*k+2]);
    } 
  }
 
  T query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    if (r <= a || b <= l) return 0;
    if (a <= l && r <= b) return node[k];
    T vl = query(a, b, 2*k+1, l, (l+r)/2);
    T vr = query(a, b, 2*k+2, (l+r)/2, r);
    return f(vl, vr);
  }
 
  T getvalue(int k) {
    return query(k,k+1);
  }
 
};
 
int main(){ 
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i,n) cin >> a[i];
  SegmentTree<int> seg(n+1);
  ll res = 0;
  for (int i = 0; i < n; i++) {
    res += i - seg.query(0, a[i]);
    seg.update(a[i]-1,1);
  }
  cout << res << endl;
  return 0;
} 
0