結果

問題 No.404 部分門松列
コンテスト
ユーザー hipokaba
提出日時 2016-09-12 09:28:12
言語 C++11(old_compat)
(gcc 12.4.0 + boost 1.89.0)
コンパイル:
g++-12 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -include bits/stdc++.h -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 536 ms / 2,000 ms
コード長 1,514 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,694 ms
コンパイル使用メモリ 173,396 KB
実行使用メモリ 11,668 KB
最終ジャッジ日時 2026-03-08 16:07:29
合計ジャッジ時間 13,539 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <algorithm>

#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

typedef long long ll;

int n;
int a[200000];
int q;
int l[200000];
int h[200000];
ll bf[200001];
ll bb[200001];
ll bs[200001];
ll bq[200001];

void badd(ll* b, int i, ll x){
	while(i <= 200000){
		b[i] += x;
		i += i & -i;
	}
}
ll bsum(ll* b, int i){
	ll s = 0;
	while(i > 0){
		s += b[i];
		i -= i & -i;
	}
	return s;
}

void compress(){
	vector<int> v(a, a + n);
	sort(v.begin(), v.end());
	v.erase(unique(v.begin(), v.end()), v.end());
	rep(i, n){
		a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
	}
	rep(i, q){
		l[i] = lower_bound(v.begin(), v.end(), l[i]) - v.begin();
		h[i] = upper_bound(v.begin(), v.end(), h[i]) - v.begin();
	}
}

void add_count(){
	rep(i, n){
		badd(bb, a[i] + 1, 1);
	}
	rep(i, n){
		badd(bf, a[i] + 1, 1);
		badd(bb, a[i] + 1, -1);
		ll c = (bsum(bf, a[i] + 1) - bsum(bf, a[i])) * (bsum(bb, a[i] + 1) - bsum(bb, a[i])) - (bsum(bs, a[i] + 1) - bsum(bs, a[i]));
		badd(bs, a[i] + 1, c);
		ll p = bsum(bf, a[i]) * bsum(bb, a[i]);
		ll q = (bsum(bf, n) - bsum(bf, a[i] + 1)) * (bsum(bb, n) - bsum(bb, a[i] + 1));
		ll r = (bsum(bs, n) - bsum(bs, a[i] + 1)) + bsum(bs, a[i]);
		badd(bq, a[i] + 1, p + q - r);
	}
}
void remove_count(){
}

int main(){
	cin >> n;
	rep(i, n){
		cin >> a[i];
	}
	cin >> q;
	rep(i, q){
		cin >> l[i] >> h[i];
	}

	compress();

	add_count();
	remove_count();
	
	rep(i, q){
		cout << bsum(bq, h[i]) - bsum(bq, l[i]) << endl;
	}
	return 0;
}
0