結果

問題 No.694 square1001 and Permutation 3
ユーザー kotamanegi
提出日時 2018-06-08 22:37:47
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2,196 ms / 3,000 ms
コード長 1,868 bytes
コンパイル時間 949 ms
コンパイル使用メモリ 106,824 KB
実行使用メモリ 36,600 KB
最終ジャッジ日時 2024-06-30 10:51:26
合計ジャッジ時間 13,237 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <fstream>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <assert.h>
using namespace std;
#define eps 0.000000001
#define LONG_INF 10000000000000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define seg_size 65536*8
#define REP(i,n) for(long long i = 0;i < n;++i)
long long seg_tree[seg_size * 2] = {};
long long seg_find(int now, int n_l, int n_r, int w_l, int w_r) {
	if (w_l <= n_l && n_r <= w_r) return seg_tree[now];
	if (n_r <= w_l || w_r <= n_l) return 0;
	return seg_find(now * 2, n_l, (n_l + n_r) / 2, w_l, w_r) + seg_find(now * 2 + 1, (n_l + n_r) / 2, n_r, w_l, w_r);
}
long long seg_set(int now) {
	seg_tree[now] = seg_tree[now * 2] + seg_tree[now * 2 + 1];
	if (now != 1) seg_set(now / 2);
	return 0;
}
int main() {
	int n;
	cin >> n;
	vector<int> hoa;
	map<int,int> geko;
	REP(i, n) {
		int c;
		cin >> c;
		hoa.push_back(c);
		geko[c] = 1;
	}
	int cnt = 0;
	for (auto i = geko.begin(); i != geko.end(); ++i) {
		i->second = cnt;
		cnt++;
	}
	for (int i = 0; i < hoa.size(); ++i) {
		hoa[i] = geko[hoa[i]];
	}
	long long ans = 0;
	for (int i = hoa.size() - 1; i >= 0; --i) {
		seg_tree[(seg_size + hoa[i])]++;
		seg_set((seg_size + hoa[i]) / 2);
		ans += seg_find(1, 0, seg_size,0,hoa[i]);
	}
	cout << ans << endl;
	for (int i = 0; i < n-1; ++i) {
		ans -= seg_find(1, 0, seg_size, 0, hoa[i]);
		ans += seg_find(1, 0, seg_size, hoa[i] + 1, seg_size);
		cout << ans << endl;
	}
	return 0;
}
0