結果

問題 No.404 部分門松列
ユーザー sugim48sugim48
提出日時 2016-07-22 22:47:36
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 280 ms / 2,000 ms
コード長 2,324 bytes
コンパイル時間 1,272 ms
コンパイル使用メモリ 109,000 KB
実行使用メモリ 11,456 KB
最終ジャッジ日時 2023-09-11 02:23:25
合計ジャッジ時間 7,847 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 123 ms
6,108 KB
testcase_14 AC 105 ms
4,376 KB
testcase_15 AC 105 ms
4,376 KB
testcase_16 AC 139 ms
8,044 KB
testcase_17 AC 187 ms
8,744 KB
testcase_18 AC 85 ms
4,376 KB
testcase_19 AC 67 ms
5,336 KB
testcase_20 AC 140 ms
4,596 KB
testcase_21 AC 172 ms
7,788 KB
testcase_22 AC 103 ms
4,380 KB
testcase_23 AC 157 ms
4,520 KB
testcase_24 AC 204 ms
4,828 KB
testcase_25 AC 280 ms
11,456 KB
testcase_26 AC 259 ms
8,580 KB
testcase_27 AC 54 ms
4,376 KB
testcase_28 AC 145 ms
5,880 KB
testcase_29 AC 206 ms
8,316 KB
testcase_30 AC 129 ms
4,376 KB
testcase_31 AC 27 ms
4,376 KB
testcase_32 AC 174 ms
9,292 KB
testcase_33 AC 175 ms
7,504 KB
testcase_34 AC 160 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v, w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;
int INF = INT_MAX / 2;

struct bit {
	vector<ll> v;
	bit(int n) : v(n + 1) {}
	ll sum(int i) {
		ll res = 0;
		for (; i > 0; i -= i & -i) res += v[i];
		return res;
	}
	void add(int i, ll x) {
		for (i++; i < v.size(); i += i & -i) v[i] += x;
	}
	int lower_bound(ll x) {
		if (x <= 0) return 0;
		int res = 0;
		for (int i = 1 << 24; i > 0; i >>= 1)
			if (res + i < v.size() && v[res + i] < x) {
				res += i; x -= v[res];
			}
		return res + 1;
	}
};

int main() {
	int N; cin >> N;
	vector<int> A(N), _A;
	for (int i = 0; i < N; i++)
		cin >> A[i];
	_A = A;
	sort(_A.begin(), _A.end());
	_A.erase(unique(_A.begin(), _A.end()), _A.end());
	for (int i = 0; i < N; i++)
		A[i] = lower_bound(_A.begin(), _A.end(), A[i]) - _A.begin();
	int n = _A.size();
	bit b1(n), b2(n);
	int cnt1 = 0, cnt2 = N;
	vector<ll> l(n), r(n);
	for (int i = 0; i < N; i++) {
		b2.add(A[i], 1);
		r[A[i]]++;
	}
	r[A[0]]--;
	ll hoge = 0;
	ll ans = 0;
	vector<ll> c(n);
	for (int i = 0; i < N; i++) {
		ans = 0;
		b2.add(A[i], -1); cnt2--;
		ans += b1.sum(A[i]) * b2.sum(A[i]);
		ans += (cnt1 - b1.sum(A[i] + 1)) * (cnt2 - b2.sum(A[i] + 1));
		b1.add(A[i], 1); cnt1++;
		ans -= hoge - l[A[i]] * r[A[i]];
		if (i + 1 == N) break;
		r[A[i + 1]]--;
		hoge -= l[A[i + 1]];
		hoge += r[A[i]];
		l[A[i]]++; 
		//cout << hoge << endl;
		//cout << ans << endl;
		c[A[i]] += ans;
	}
	vector<ll> sum(n + 1);
	for (int j = 0; j < n; j++)
		sum[j + 1] = sum[j] + c[j];
	int Q; cin >> Q;
	while (Q--) {
		int lb, ub; scanf("%d%d", &lb, &ub); lb--;
		lb = upper_bound(_A.begin(), _A.end(), lb) - _A.begin();
		ub = upper_bound(_A.begin(), _A.end(), ub) - _A.begin();
		printf("%lld\n", sum[ub] - sum[lb]);
	}
}
0