結果

問題 No.924 紲星
ユーザー polylogKpolylogK
提出日時 2019-09-06 01:10:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,607 ms / 4,000 ms
コード長 3,387 bytes
コンパイル時間 2,986 ms
コンパイル使用メモリ 182,716 KB
実行使用メモリ 22,172 KB
最終ジャッジ日時 2023-10-13 08:22:50
合計ジャッジ時間 22,732 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 4 ms
4,352 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 4 ms
4,352 KB
testcase_06 AC 4 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2,540 ms
22,024 KB
testcase_09 AC 2,607 ms
22,172 KB
testcase_10 AC 2,606 ms
21,840 KB
testcase_11 AC 2,551 ms
21,860 KB
testcase_12 AC 2,596 ms
21,832 KB
testcase_13 AC 1,132 ms
12,804 KB
testcase_14 AC 1,139 ms
12,032 KB
testcase_15 AC 1,011 ms
10,460 KB
testcase_16 AC 749 ms
15,224 KB
testcase_17 AC 1,821 ms
16,184 KB
testcase_18 AC 2 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cout;
using std::endl;
using std::cin;

template<typename T>
std::vector<T> make_v(size_t a){return std::vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

struct SegmentTree {
private:
    int n;
    std::vector<i64> node;
 
public:
    SegmentTree(int sz) {
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1, 0);
    }
 
    void update(int x, i64 val) {
        x += (n - 1);
        node[x] += val;
        while(x > 0) {
            x = (x - 1) / 2;
            node[x] = node[2*x+1] + node[2*x+2];
        }
    }
 
    i64 get(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        if(r <= a || b <= l) return 0;
        if(a <= l && r <= b) return node[k];
 
        i64 vl = get(a, b, 2*k+1, l, (l+r)/2);
        i64 vr = get(a, b, 2*k+2, (l+r)/2, r);
        return vl + vr;
    }
};

int main() {
	// input
	int n, q; scanf("%d%d", &n, &q);
	assert(1 <= n and n <= 2e5);
	assert(1 <= q and q <= 2e5);

	std::vector<int> a(n);
	for(int i = 0; i < n; i++) {
		scanf("%d", &a[i]);

		assert(-1e9 <= a[i] and a[i] <= 1e9);
	}

	std::vector<int> L(q), R(q);
	for(int i = 0; i < q; i++) {
		scanf("%d%d", &L[i], &R[i]);

		assert(1 <= L[i] and L[i] <= n);
		assert(1 <= R[i] and R[i] <= n);
		assert(L[i] <= R[i]);

		L[i]--;
	}
	
	// solve
	std::vector<std::pair<int, int>> vec;
	for(int i = 0; i < n; i++) vec.push_back({a[i], i});
	sort(begin(vec), end(vec));
	
	std::vector<i64> center = [&]{
		using P = std::pair<int, int>;
		
		std::vector<int> Left(q, -1), Right(q, n);
		std::vector<std::pair<int, int>> query, nxt;
		for(int i = 0; i < q; i++) query.push_back({(Left[i] + Right[i]) >> 1, i});

		while(!query.empty()) {
			sort(begin(query), end(query));

			int cur = 0;
			SegmentTree seg(n);
			for(int i = 0; i < n; i++) {
				seg.update(vec[i].second, 1);

				while(cur < query.size() and query[cur].first <= i) {
					auto & p = query[cur++];
					int id = p.second, mid = p.first;
		
					int len = (R[id] - L[id] + 1) >> 1;
					int val = seg.get(L[id], R[id]);
					if(len <= val) Right[id] = mid;
					else Left[id] = mid;

					if(Left[id] + 1 != Right[id])
						nxt.push_back({(Left[id] + Right[id]) >> 1, id});
				}
			}
			
			query.swap(nxt);
			nxt.clear();
		}

		std::vector<i64> center(q);
		for(int i = 0; i < q; i++) center[i] = vec[Left[i] + 1].first;
		return center;
	}();
	
	std::vector<i64> ans = [&]{
		std::vector<i64> ans(q);
		for(int loop = 0; loop < 2; loop++) {
			SegmentTree seg(n), cnt(n);
			
			std::vector<std::pair<int, int>> query;
			for(int i = 0; i < q; i++) query.push_back({center[i], i});
			sort(begin(query), end(query));
			if(loop) reverse(begin(query), end(query));

			int cur = 0;
			for(int i = 0; i < n; i++) {
				seg.update(vec[i].second, vec[i].first);
				cnt.update(vec[i].second, 1);

				while(cur < query.size() and query[cur].first == vec[i].first) {
					auto & p = query[cur++];
					i64 id = p.second, val = p.first;
					
					i64 tmp = val * cnt.get(L[id], R[id]) - seg.get(L[id], R[id]);
					ans[id] += tmp * (loop ? -1 : 1);
				}
			}

			reverse(begin(vec), end(vec));
		}
		return ans;
	}();

	for(auto v: ans) printf("%lld\n", v);
	return 0;
}
0