結果

問題 No.924 紲星
ユーザー polylogKpolylogK
提出日時 2019-09-05 23:49:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 796 ms / 4,000 ms
コード長 3,174 bytes
コンパイル時間 1,855 ms
コンパイル使用メモリ 180,936 KB
実行使用メモリ 17,180 KB
最終ジャッジ日時 2023-10-13 08:15:24
合計ジャッジ時間 9,952 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 779 ms
16,904 KB
testcase_09 AC 776 ms
17,180 KB
testcase_10 AC 796 ms
16,900 KB
testcase_11 AC 785 ms
16,984 KB
testcase_12 AC 792 ms
16,868 KB
testcase_13 AC 385 ms
9,856 KB
testcase_14 AC 375 ms
10,552 KB
testcase_15 AC 336 ms
9,156 KB
testcase_16 AC 279 ms
9,928 KB
testcase_17 AC 579 ms
13,308 KB
testcase_18 AC 2 ms
4,348 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...));
}

template< class T >
struct BinaryIndexedTree
{
  std::vector< T > data;

  BinaryIndexedTree(int sz)
  {
    data.assign(++sz, 0);
  }

  T sum(int k)
  {
	  if(k < 0) return 0;
    T ret = 0;
    for(++k; k > 0; k -= k & -k) ret += data[k];
    return (ret);
  }

  T get(int l, int r) {
	  return sum(r - 1) - sum(l - 1);
  }

  void add(int k, T x)
  {
    for(++k; k < data.size(); k += k & -k) data[k] += x;
  }
};



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;
			BinaryIndexedTree<int> bit(n);
			for(int i = 0; i < n; i++) {
				bit.add(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 = bit.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++) {
			BinaryIndexedTree<i64> bit(n);
			BinaryIndexedTree<i64> 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++) {
				bit.add(vec[i].second, vec[i].first);
				cnt.add(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]) - bit.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