結果

問題 No.956 Number of Unbalanced
ユーザー maroon_kurimaroon_kuri
提出日時 2019-12-19 00:58:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 650 ms / 2,000 ms
コード長 8,596 bytes
コンパイル時間 2,856 ms
コンパイル使用メモリ 223,068 KB
実行使用メモリ 34,104 KB
最終ジャッジ日時 2024-07-06 23:12:34
合計ジャッジ時間 11,867 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,632 KB
testcase_01 AC 4 ms
5,504 KB
testcase_02 AC 3 ms
5,632 KB
testcase_03 AC 4 ms
5,632 KB
testcase_04 AC 4 ms
5,632 KB
testcase_05 AC 3 ms
5,632 KB
testcase_06 AC 349 ms
24,960 KB
testcase_07 AC 406 ms
24,832 KB
testcase_08 AC 320 ms
25,176 KB
testcase_09 AC 173 ms
24,704 KB
testcase_10 AC 324 ms
24,576 KB
testcase_11 AC 511 ms
25,312 KB
testcase_12 AC 352 ms
24,832 KB
testcase_13 AC 113 ms
29,852 KB
testcase_14 AC 647 ms
25,456 KB
testcase_15 AC 197 ms
29,300 KB
testcase_16 AC 389 ms
24,960 KB
testcase_17 AC 266 ms
33,848 KB
testcase_18 AC 627 ms
25,472 KB
testcase_19 AC 235 ms
33,576 KB
testcase_20 AC 630 ms
25,600 KB
testcase_21 AC 117 ms
29,980 KB
testcase_22 AC 261 ms
34,104 KB
testcase_23 AC 386 ms
24,960 KB
testcase_24 AC 233 ms
33,576 KB
testcase_25 AC 353 ms
24,960 KB
testcase_26 AC 117 ms
24,832 KB
testcase_27 AC 354 ms
24,832 KB
testcase_28 AC 362 ms
24,908 KB
testcase_29 AC 650 ms
25,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
#define int ll

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif

template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

using pi=pair<int,int>;
using vi=vc<int>;

template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.a<<","<<p.b<<"}";
}

template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}

#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ";
	dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,"Line:",__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif

using uint=unsigned;
using ull=unsigned long long;

template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
	return os<<vc<t>(all(a));
}

template<int i,class T>
void print_tuple(ostream&,const T&){
}

template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
	if(i)os<<",";
	os<<get<i>(t);
	print_tuple<i+1,T,Args...>(os,t);
}

template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
	os<<"{";
	print_tuple<0,tuple<Args...>,Args...>(os,t);
	return os<<"}";
}

void print(ll x,int suc=1){
	cout<<x;
	if(suc==1)
		cout<<"\n";
	if(suc==2)
		cout<<" ";
}

ll read(){
	ll i;
	cin>>i;
	return i;
}

vi readvi(int n,int off=0){
	vi v(n);
	rep(i,n)v[i]=read()+off;
	return v;
}

template<class T>
void print(const vector<T>&v,int suc=1){
	rep(i,v.size())
		print(v[i],i==int(v.size())-1?suc:2);
}

string readString(){
	string s;
	cin>>s;
	return s;
}

template<class T>
T sq(const T& t){
	return t*t;
}

//#define CAPITAL
void yes(bool ex=true){
	#ifdef CAPITAL
	cout<<"YES"<<endl;
	#else
	cout<<"Yes"<<endl;
	#endif
	if(ex)exit(0);
}
void no(bool ex=true){
	#ifdef CAPITAL
	cout<<"NO"<<endl;
	#else
	cout<<"No"<<endl;
	#endif
	if(ex)exit(0);
}
void possible(bool ex=true){
	#ifdef CAPITAL
	cout<<"POSSIBLE"<<endl;
	#else
	cout<<"Possible"<<endl;
	#endif
	if(ex)exit(0);
}
void impossible(bool ex=true){
	#ifdef CAPITAL
	cout<<"IMPOSSIBLE"<<endl;
	#else
	cout<<"Impossible"<<endl;
	#endif
	if(ex)exit(0);
}

constexpr ll ten(int n){
	return n==0?1:ten(n-1)*10;
}

const ll infLL=LLONG_MAX/3;

#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif

int topbit(signed t){
	return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
	return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
	return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
	return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
	return __builtin_popcount(t);
}
int popcount(ll t){
	return __builtin_popcountll(t);
}
bool ispow2(int i){
	return i&&(i&-i)==i;
}
int mask(int i){
	return (int(1)<<i)-1;
}

bool inc(int a,int b,int c){
	return a<=b&&b<=c;
}

template<class t> void mkuni(vc<t>&v){
	sort(all(v));
	v.erase(unique(all(v)),v.ed);
}

ll rand_int(ll l, ll r) { //[l, r]
	#ifdef LOCAL
	static mt19937_64 gen;
	#else
    static random_device rd;
    static mt19937_64 gen(rd());
    #endif
    return uniform_int_distribution<ll>(l, r)(gen);
}

template<class t>
int lwb(const vc<t>&v,const t&a){
	return lower_bound(all(v),a)-v.bg;
}

//b==e のクエリを投げるかどうか気をつける
//b==e 投げても問題ない?
//なんかバグったら考えよう

//KUPC2017I
//HDU 5306 Gorgeous Sequence
template<class N>
struct segbeats{
	vc<N> x;
	int s;
	segbeats(){}
	template<class T>
	segbeats(const vc<T>& a){
		int n=a.size();
		s=1;
		while(s<n)s*=2;
		x.resize(s*2);
		rep(i,n)
			x[s+i]=N(a[i]);
		gnr(i,1,s)
			upd(i);
	}
	void push(int i){
		x[i].push(x[i*2],x[i*2+1]);
	}
	void upd(int i){
		x[i]=N::merge(x[i*2],x[i*2+1]);
	}
	template<class F,class... Args>
	void chr(int l,int r,int i,int b,int e,F f,Args&&... args){
		if(e<=l||r<=b)
			return;
		if(b<=l&&r<=e&&(x[i].*f)(forward<Args>(args)...))
			return;
		push(i);
		int m=(l+r)/2;
		chr(l,m,i*2,b,e,f,forward<Args>(args)...);
		chr(m,r,i*2+1,b,e,f,forward<Args>(args)...);
		upd(i);
	}
	template<class F,class... Args>
	void ch(int b,int e,F f,Args&&... args){
		assert(b<=e);
		chr(0,s,1,b,e,f,forward<Args>(args)...);
	}
	//use decltype((declval<N>().*F())()) for old-fashioned judges
	template<class F,class G,class H>
	auto getr(int l,int r,int i,int b,int e,F f,G g,H h){
		if(e<=l||r<=b)
			return h;
		if(b<=l&&r<=e)
			return (x[i].*f)();
		push(i);
		int m=(l+r)/2;
		return g(getr(l,m,i*2,b,e,f,g,h),getr(m,r,i*2+1,b,e,f,g,h));
	}
	template<class F,class G,class H>
	auto get(int b,int e,F f,G g,H h){
		assert(b<=e);
		return getr(0,s,1,b,e,f,g,h);
	}
	//return minimum index
	template<class F,class...Args>
	pair<int,N> findr(int i,int l,int r,int b,int e,F f,Args&&...args){
		if(e<=l||r<=b)return {e,N()};
		if(b<=l&&r<=e){
			if(!(x[i].*f)(forward<Args>(args)...))return {e,N()};
			if(r-l==1)return {l,x[i]};
		}
		push(i);
		int m=(l+r)/2;
		auto a=findr(i*2,l,m,b,e,f,forward<Args>(args)...);
		if(a.a<e)return a;
		return findr(i*2+1,m,r,b,e,f,forward<Args>(args)...);
	}
	//NOT VERIFIED
	template<class F,class...Args>
	pair<int,N> find(int b,int e,F f,Args&&...args){
		assert(b<=e);
		return findr(1,0,s,b,e,f,forward<Args>(args)...);
	}
	//NOT VERIFIED
	/*
	//return maximum index
	template<class F,class...Args>
	pair<int,N> findr(int i,int l,int r,int b,int e,F f,Args&&...args){
		if(e<=l||r<=b)return {b-1,N()};
		if(b<=l&&r<=e){
			if(!(x[i].*f)(args...))return {b-1,N()};
			if(r-l==1)return {l,x[i]};
		}
		push(i);
		int m=(l+r)/2;
		auto a=findr(i*2+1,m,r,b,e,f,forward<Args>(args)...);
		if(a.a>=b)return a;
		return findr(i*2,l,m,b,e,f,forward<Args>(args)...);
	}
	template<class F,class...Args>
	pair<int,N> find(int b,int e,F f,Args&&...args){
		assert(b<=e);
		return findr(1,0,s,b,e,f,forward<Args>(args)...);
	}
	*/
	void enumerater(int l,int r,int i,int b,int e,vc<N>&dst){
		if(e<=l||r<=b)
			return;
		if(l+1==r){
			dst.pb(x[i]);
			return;
		}
		push(i);
		int m=(l+r)/2;
		enumerater(l,m,i*2,b,e,dst);
		enumerater(m,r,i*2+1,b,e,dst);
	}
	void enumerate(int b,int e,vc<N>&dst){
		assert(b<=e);
		return enumerater(0,s,1,b,e,dst);
	}
};

//usage

//range add linear function to range
//don't forget to pass the second argument to addA
struct N{
	int s,len,la,lb;
	N(int v=0,int w=1):s(v),len(w),la(0),lb(0){}
	bool addA(int v,int&d){
		la+=v;
		s+=len*(len-1)/2*v;
		addB(d*v);
		d+=len;
		return true;
	}
	bool addB(int v){
		lb+=v;
		s+=len*v;
		return true;
	}
	void push(N&x,N&y){
		int tmp=0;
		x.addA(la,tmp);
		y.addA(la,tmp);
		x.addB(lb);
		y.addB(lb);
		la=0;
		lb=0;
	}
	static N merge(N x,N y){
		return N(x.s+y.s,x.len+y.len);
	}
	int gets(){return s;}
};

int slv(int n,vi p,segbeats<N>&seg){
	if(p.empty())return 0;
	int ans=0;
	int cur=n;
	vi qs;
	{
		int pre=0;
		for(auto i:p){
			if(pre<i){
				qs.pb(-(i-pre));
			}
			qs.pb(1);
			pre=i+1;
		}
		if(pre<n)
			qs.pb(-(n-pre));
	}
	vc<tuple<int,int,int>> his;
	auto addA=[&](int l,int r){
		dmp2(l,r);
		his.eb(0,l,r);
		int tmp=0;
		seg.ch(l,r,&N::addA,1,tmp);
	};
	auto addB=[&](int v,int l,int r){
		dmp2(v,l,r);
		assert(v>0);
		his.eb(v,l,r);
		seg.ch(l,r,&N::addB,v);
	};
	addB(1,cur+1,seg.s);
	auto getSum=[&](int l,int r){
		dmp2(l,r);
		return seg.get(l,r,&N::gets,[](int a,int b){return a+b;},int(0));
	};
	dmp(qs);
	for(auto q:qs){
		if(q<0){
			ans+=getSum(cur+q,cur);
			addA(cur+q,cur);
			addB(-q,cur,seg.s);
		}else{
			ans+=getSum(cur+1,cur+2);
			addB(1,cur+2,seg.s);
		}
		dmp(ans);
		cur+=q;
	}
	for(auto h:his){
		int v,l,r;tie(v,l,r)=h;
		if(v==0){
			int tmp=0;
			seg.ch(l,r,&N::addA,-1,tmp);
		}else{
			seg.ch(l,r,&N::addB,-v);
		}
	}
	dmp2(p,ans);
	return ans;
}

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	
	int n;cin>>n;
	const int V=ten(5);
	vvc<int> pos(V);
	rep(i,n){
		int x;cin>>x;
		x--;
		pos[x].pb(i);
	}
	segbeats<N> seg(vi(2*n+1));
	int ans=0;
	rep(i,V)ans+=slv(n,pos[i],seg);
	cout<<ans<<endl;
}
0