結果

問題 No.924 紲星
ユーザー kwm_tkwm_t
提出日時 2023-05-21 16:45:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,486 bytes
コンパイル時間 4,422 ms
コンパイル使用メモリ 277,928 KB
実行使用メモリ 38,692 KB
最終ジャッジ日時 2023-08-23 15:11:50
合計ジャッジ時間 14,372 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 997 ms
37,892 KB
testcase_09 AC 1,014 ms
37,300 KB
testcase_10 AC 1,012 ms
38,264 KB
testcase_11 AC 992 ms
38,692 KB
testcase_12 AC 993 ms
37,916 KB
testcase_13 AC 442 ms
20,384 KB
testcase_14 AC 412 ms
16,660 KB
testcase_15 AC 379 ms
16,376 KB
testcase_16 AC 405 ms
28,552 KB
testcase_17 AC 652 ms
25,120 KB
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<long long,long long>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }

P op(P a, P b) { return { a.first + b.first,a.second + b.second }; }
P e() { return { 0,0 }; }
int op0(int a, int b) { return a + b; }
int e0() { return 0; }
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, q; cin >> n >> q;
	vector<int>a(n);
	vector<P>p;
	rep(i, n) {
		cin >> a[i];
		p.push_back({ a[i],i });
	}
	sort(all(p));
	vector<int>l(q), r(q);
	rep(i, q) cin >> l[i] >> r[i], l[i]--;

	vector<int>ok(q, n), ng(q, 0);
	while (true) {
		bool fin = true;
		vector<vector<int>>mid(n);
		rep(i, q) {
			if (abs(ok[i] - ng[i]) > 1) {
				fin = false;
				int m = (ok[i] + ng[i]) / 2;
				mid[m].push_back(i);
			}
		}
		if (fin)break;
		segtree<int, op0, e0>seg(n);
		rep(i, n) {
			//todo	データ構造の更新
			seg.set(p[i].second, 1);
			rep(j, mid[i].size()) {
				//todo	データ構造の取得
				int get = seg.prod(l[mid[i][j]], r[mid[i][j]]);
				int sz = r[mid[i][j]] - l[mid[i][j]] + 1;
				sz = (sz + 1) / 2;
				//todo	配列更新
				if (get >= sz)ok[mid[i][j]] = i;
				else ng[mid[i][j]] = i;
			}
		}
	}
	vector<pair<int, P>>eve;
	rep(i, n) eve.push_back({ a[i],{i, 0} });
	rep(i, q) eve.push_back({ (int)p[ok[i]].first,{i, 1} });
	vector<long long>ans(q);
	sort(all(eve));
	rep(_, 2) {
		segtree<P, op, e>seg(n);
		rep(i, eve.size()) {
			int t = eve[i].second.second;
			int id = eve[i].second.first;
			int val = eve[i].first;
			if (0 == t) {
				seg.set(id, { val,1 });
			}
			else {
				auto get = seg.prod(l[id], r[id]);
				long long val = get.second * p[ok[id]].first - get.first;
				ans[id] += abs(val);
			}
		}
		reverse(all(eve));
	}
	rep(i, q) cout << ans[i] << endl;
	return 0;
}
0