結果

問題 No.956 Number of Unbalanced
ユーザー chocoruskchocorusk
提出日時 2019-12-19 00:54:28
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 646 ms / 2,000 ms
コード長 3,343 bytes
コンパイル時間 4,337 ms
コンパイル使用メモリ 135,988 KB
実行使用メモリ 20,284 KB
最終ジャッジ日時 2023-09-21 04:36:14
合計ジャッジ時間 13,077 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,752 KB
testcase_01 AC 3 ms
5,696 KB
testcase_02 AC 4 ms
5,692 KB
testcase_03 AC 4 ms
5,684 KB
testcase_04 AC 4 ms
5,728 KB
testcase_05 AC 4 ms
5,732 KB
testcase_06 AC 427 ms
19,424 KB
testcase_07 AC 487 ms
19,400 KB
testcase_08 AC 404 ms
19,140 KB
testcase_09 AC 327 ms
18,864 KB
testcase_10 AC 421 ms
18,700 KB
testcase_11 AC 544 ms
19,648 KB
testcase_12 AC 426 ms
19,104 KB
testcase_13 AC 282 ms
18,952 KB
testcase_14 AC 643 ms
20,232 KB
testcase_15 AC 282 ms
18,812 KB
testcase_16 AC 459 ms
19,696 KB
testcase_17 AC 269 ms
18,672 KB
testcase_18 AC 646 ms
20,220 KB
testcase_19 AC 286 ms
18,768 KB
testcase_20 AC 644 ms
20,284 KB
testcase_21 AC 279 ms
18,624 KB
testcase_22 AC 271 ms
18,668 KB
testcase_23 AC 458 ms
19,620 KB
testcase_24 AC 287 ms
18,556 KB
testcase_25 AC 441 ms
18,732 KB
testcase_26 AC 290 ms
18,608 KB
testcase_27 AC 420 ms
18,844 KB
testcase_28 AC 442 ms
18,852 KB
testcase_29 AC 644 ms
20,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
template<typename Monoid, typename OperatorMonoid=Monoid>
struct LazySegmentTree{
	using F=function<Monoid(Monoid, Monoid)>;
	using G=function<Monoid(Monoid, OperatorMonoid, int, int)>;
	using H=function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
	int sz;
	vector<Monoid> data;
	vector<OperatorMonoid> lazy;
	const F f;
	const G g;
	const H h;
	const Monoid e1;
	const OperatorMonoid e0;

	LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &e1, const OperatorMonoid &e0): f(f), g(g), h(h), e1(e1), e0(e0){
		sz=1;
		while(sz<n) sz<<=1;
		data.resize(2*sz-1, e1);
		lazy.resize(2*sz-1, e0);
	}

	void build(vector<Monoid> v){
		for(int i=0; i<v.size(); i++) data[i+sz-1]=v[i];
		for(int i=sz-2; i>=0; i--) data[i]=f(data[2*i+1], data[2*i+2]);
	}

	void eval(int k, int l, int r){
		if(lazy[k]!=e0){
			data[k]=g(data[k], lazy[k], l, r);
			if(k<sz-1){
				lazy[2*k+1]=h(lazy[2*k+1], lazy[k]);
				lazy[2*k+2]=h(lazy[2*k+2], lazy[k]);
			}
		}
		lazy[k]=e0;
	}

	void update(int a, int b, const OperatorMonoid &x, int k, int l, int r){
		eval(k, l, r);
		if(r<=a || b<=l) return;
		if(a<=l && r<=b){
			lazy[k]=h(lazy[k], x);
			eval(k, l, r);
		}else{
			update(a, b, x, 2*k+1, l, (l+r)/2);
			update(a, b, x, 2*k+2, (l+r)/2, r);
			data[k]=f(data[2*k+1], data[2*k+2]);
		}
	}
	void update(int a, int b, const OperatorMonoid &x){
		return update(a, b, x, 0, 0, sz);
	}

	Monoid find(int a, int b, int k, int l, int r){
		eval(k, l, r);
		if(b<=l || r<=a) return e1;
		if(a<=l && r<=b) return data[k];
		else return f(find(a, b, 2*k+1, l, (l+r)/2), find(a, b, 2*k+2, (l+r)/2, r));
	}
	Monoid find(int a, int b){
		return find(a, b, 0, 0, sz);
	}
	Monoid operator[](const int &k){
		return find(k, k+1);
	}
};
int main()
{
    int n;
	cin>>n;
	int a[100010];
	for(int i=0; i<n; i++){
		cin>>a[i];
	}
	vector<int> v[100010];
	for(int i=0; i<n; i++){
		v[a[i]].push_back(i+1);
	}
	ll ans=0;
	auto f=[](P p, P q){
		return P(p.first+q.first, p.second+q.second);
	};
	auto g=[](P p, ll x, int l, int r){
		return P(p.first+x*((ll)r*(r-1)/2-(ll)l*(l-1)/2), p.second+x*(r-l));
	};
	auto h=[](ll x, ll y){ return x+y;};
	LazySegmentTree<P, ll> seg(2*n+1, f, g, h, P(0, 0), 0);
	for(int i=1; i<=100000; i++){
		if(v[i].empty()) continue;
		v[i].push_back(n+1);
		int s=n, pr=0;
		seg.update(n, n+1, 1);
		for(auto j:v[i]){
			P p1=seg.find(s-(j-pr-1)-1, s-1);
			ans+=-p1.first+p1.second*(s-1);
			ans+=(j-pr-1)*seg.find(0, s-(j-pr)).second;
			if(j==n+1) break;
			seg.update(s-(j-pr-1), s, 1);
			s=s-(j-pr-1)+1;
			ans+=seg.find(0, s).second;
			seg.update(s, s+1, 1);
			pr=j;
		}
		s=n, pr=0;
		seg.update(n, n+1, -1);
		for(auto j:v[i]){
			if(j==n+1) break;
			seg.update(s-(j-pr-1), s, -1);
			s=s-(j-pr-1)+1;
			seg.update(s, s+1, -1);
			pr=j;
		}
	}
	cout<<ans<<endl;
	return 0;
}
0