結果

問題 No.404 部分門松列
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2016-07-22 23:44:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,958 bytes
コンパイル時間 2,189 ms
コンパイル使用メモリ 183,548 KB
実行使用メモリ 47,628 KB
最終ジャッジ日時 2024-04-24 02:44:46
合計ジャッジ時間 18,114 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
19,712 KB
testcase_01 AC 14 ms
19,840 KB
testcase_02 WA -
testcase_03 AC 14 ms
19,840 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 508 ms
21,200 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)

typedef long long ll;
template<class V, int NV> class SegTree {
public:
	static V const def = 0;
	V comp(V l, V r) { return (l + r); };

	vector<V> val;
	SegTree() { val = vector<V>(NV * 2, def); }

	V getval(int l, int r) { //[l,r]
		l += NV; r += NV + 1;
		V ret = def;
		while (l < r) {
			if (l & 1) ret = comp(ret, val[l++]);
			if (r & 1) ret = comp(ret, val[--r]);
			l /= 2; r /= 2;
		}
		return ret;
	}
	void update(int i, V v) {
		i += NV;
		val[i] = v;
		while (i>1) i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]);
	}
};
//-----------------------------------------------------------------
int N, Q;
map<int, vector<int> > A;
map<int, ll> cnt;
SegTree<ll, 1 << 20> seg;
#define INF INT_MAX/2

int main()
{
	cin >> N;
	rep(i, 0, N) {
		int a;
		scanf("%d", &a);
		A[a].push_back(i);
	}

	rep(i, 0, N) seg.update(i, 1);
	for (auto p : A) {
		int a = p.first;
		auto v = p.second;
		for (int i : v) seg.update(i, 0);
		for (int i : v) cnt[a] += seg.getval(0, i) * seg.getval(i, N - 1);
	}

	rep(i, 0, N) seg.update(i, 1);
	auto ite = A.rbegin();
	while (ite != A.rend()) {
		int a = ite->first;
		auto v = ite->second;
		for (int i : v) seg.update(i, 0);
		for (int i : v) cnt[a] += seg.getval(0, i) * seg.getval(i, N - 1);
		ite++;
	}

	/*for (auto p : cnt) {
		printf("[%d : %lld]\n", p.first, p.second);
	}*/

	ll sum = 0;
	for (auto p : cnt) {
		cnt[p.first] += sum;
		sum = cnt[p.first];
	}

	vector<int> idx;
	vector<ll> imos;
	idx.push_back(0), imos.push_back(0);
	for (auto p : cnt) idx.push_back(p.first), imos.push_back(p.second);
	idx.push_back(INF), imos.push_back(sum);

	int Q; cin >> Q;
	rep(i, 0, Q) {
		int L, H; cin >> L >> H;

		int LI = lower_bound(idx.begin(), idx.end(), L) - idx.begin();
		int HI = upper_bound(idx.begin(), idx.end(), H) - idx.begin();

		//printf("<%d %d>", LI, HI);
		printf("%lld\n", imos[HI - 1] - imos[LI - 1]);
	}
}
0