結果

問題 No.404 部分門松列
ユーザー hipokabahipokaba
提出日時 2016-09-12 09:28:12
言語 C++11
(gcc 11.4.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,514 bytes
コンパイル時間 584 ms
コンパイル使用メモリ 52,012 KB
最終ジャッジ日時 2023-09-11 02:42:30
合計ジャッジ時間 2,670 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp: In function ‘void compress()’:
main.cpp:36:2: error: ‘vector’ was not declared in this scope
  vector<int> v(a, a + n);
  ^~~~~~
main.cpp:36:2: note: ‘std::vector’ is defined in header ‘<vector>’; did you forget to ‘#include <vector>’?
main.cpp:3:1:
+#include <vector>
 
main.cpp:36:2:
  vector<int> v(a, a + n);
  ^~~~~~
main.cpp:36:9: error: expected primary-expression before ‘int’
  vector<int> v(a, a + n);
         ^~~
main.cpp:37:7: error: ‘v’ was not declared in this scope
  sort(v.begin(), v.end());
       ^

ソースコード

diff #

#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