結果

問題 No.694 square1001 and Permutation 3
ユーザー ゆにぽけゆにぽけ
提出日時 2023-11-01 13:57:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 608 ms / 3,000 ms
コード長 2,344 bytes
コンパイル時間 1,632 ms
コンパイル使用メモリ 135,720 KB
実行使用メモリ 13,408 KB
最終ジャッジ日時 2023-11-01 13:57:58
合計ジャッジ時間 7,534 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,368 KB
testcase_01 AC 2 ms
4,368 KB
testcase_02 AC 2 ms
4,368 KB
testcase_03 AC 2 ms
4,368 KB
testcase_04 AC 2 ms
4,368 KB
testcase_05 AC 2 ms
4,368 KB
testcase_06 AC 2 ms
4,368 KB
testcase_07 AC 121 ms
7,356 KB
testcase_08 AC 454 ms
8,460 KB
testcase_09 AC 590 ms
13,408 KB
testcase_10 AC 97 ms
7,356 KB
testcase_11 AC 576 ms
13,408 KB
testcase_12 AC 608 ms
13,408 KB
testcase_13 AC 2 ms
4,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cassert>
#include<cmath>
#include<ctime>
#include<iomanip>
#include<numeric>
#include<stack>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<bitset>
#include<random>
#include<functional>
#include<utility>
using namespace std;
template<class T,T (*op)(T,T),T (*e)()> struct segtree
{
	private:
	int size,N;
	vector<T> node;

	public:
	segtree(int N = 0) : segtree(vector<T>(N,e())) {}
	segtree(const vector<T> &V)
	{
		N = (int)V.size();
		size = 1;
		while(size < N) size <<= 1;
		node.resize(2*size);
		for(int i = 0;i < N;i++) node[i+size] = V[i];
		for(int i = size-1;i >= 1;i--) node[i] = op(node[i*2],node[i*2+1]);
	}
	void replace(int pos,T x) {func_update(pos,x,[](T u,T v){return u = v;});}
	void add(int pos,T x) {func_update(pos,x,[](T u,T v){return u + v;});}
	void func_update(int pos,T x) {func_update(pos,x,op);}
	void func_update(int pos,T x,T (*func)(T,T))
	{
		assert(0 <= pos && pos < N);
		pos += size;
		node[pos] = func(node[pos],x);
		pos >>= 1;
		while(pos)
		{
			node[pos] = op(node[pos*2],node[pos*2+1]);
			pos >>= 1;
		}
	}
	T operator[](int pos)
	{
		assert(0 <= pos && pos < N);
		return node[pos+size];
	}
	T prod(int l,int r)
	{
		assert(0 <= l && l <= r && r <= N);
		T L = e(),R = e();
		l += size,r += size;
		while(l < r)
		{
			if(l & 1) L = op(L,node[l++]);
			if(r & 1) R = op(node[--r],R);
			l >>= 1,r >>= 1;
		}
		return op(L,R);
	}
	T prod() {return node[1];}
};
int op(int a,int b) {return a+b;}
int e() {return 0;}
int N,A[5 << 17];
void solve()
{
	cin >> N;
	vector<int> comp;
	for(int i = 0;i < N;i++) cin >> A[i],comp.push_back(A[i]);
	sort(comp.begin(),comp.end());
	comp.erase(unique(comp.begin(),comp.end()),comp.end());
	long long inv = 0;
	segtree<int,op,e> seg(comp.size());
	for(int i = 0;i < N;i++)
	{
		int id = lower_bound(comp.begin(),comp.end(),A[i])-comp.begin();
		inv += seg.prod(id+1,comp.size());
		seg.add(id,1);
	}
	cout << inv << "\n";
	for(int i = 0;i+1 < N;i++)
	{
		int id = lower_bound(comp.begin(),comp.end(),A[i])-comp.begin();
		inv -= seg.prod(0,id);
		inv += seg.prod(id+1,comp.size());
		cout << inv << "\n";
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	/* cin >> tt; */
	while(tt--) solve();
}
0