結果

問題 No.924 紲星
ユーザー FF256grhyFF256grhy
提出日時 2019-11-09 00:15:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,629 bytes
コンパイル時間 4,080 ms
コンパイル使用メモリ 180,940 KB
実行使用メモリ 14,356 KB
最終ジャッジ日時 2023-10-13 05:49:38
合計ジャッジ時間 8,257 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,356 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 11 ms
4,348 KB
testcase_04 AC 4 ms
4,352 KB
testcase_05 AC 21 ms
4,352 KB
testcase_06 AC 9 ms
4,356 KB
testcase_07 AC 4 ms
4,352 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long   signed int LL;
typedef long long unsigned int LU;
#define incID(i, l, r) for(LL i = (l)    ; i <  (r); ++i)
#define incII(i, l, r) for(LL i = (l)    ; i <= (r); ++i)
#define decID(i, l, r) for(LL i = (r) - 1; i >= (l); --i)
#define decII(i, l, r) for(LL i = (r)    ; i >= (l); --i)
#define inc(i, n)  incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec(i, n)  decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
template<typename T> bool setmin  (T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmax  (T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }
template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }
LL mo(LL a, LL b) { assert(b > 0); a %= b; if(a < 0) { a += b; } return a; }
LL fl(LL a, LL b) { assert(b > 0); return (a > 0 ? a / b : (a - b + 1) / b); }
LL ce(LL a, LL b) { assert(b > 0); return (a < 0 ? a / b : (a + b - 1) / b); }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define bit(b, i) (((b) >> (i)) & 1)
#define BC __builtin_popcountll
#define SC static_cast
#define SI(v) SC<int>(v.size())
#define SL(v) SC<LL >(v.size())
#define RF(e, v) for(auto & e: v)
#define ef else if
#define UR assert(false)

// ---- ----

const int B = 30000;

int main() {
	int n, q;
	cin >> n >> q;
	vector<LL> a(n);
	inc(i, n) { cin >> a[i]; }
	vector<vector<array<int, 3>>> v(n / B + 1);
	inc(i, q) {
		int l, r;
		cin >> l >> r;
		l--;
		v[l / B].PB({ r, l, SC<int>(i) });
	}
	
	RF(e, v) { sort(ALL(e)); }
	
	RF(e, v) {
	RF(f, e) {
		swap(f[0], f[1]); // { l, r, i }
	}
	}
	
	vector<LL> ans(q);
	inc(i, SI(v)) {
		LL l = i * B, r = l, Lsum = 0, Rsum = 0;
		multiset<LL> Lset, Rset;
		
		auto balance = [&]() {
			while(! (SI(Lset) + 1 >= SI(Rset))) {
				auto it = Rset.begin();
				LL w = * it;
				Lset.insert(w); Lsum += w;
				Rset.erase(it); Rsum -= w;
			}
			while(! (SI(Lset) + 0 <= SI(Rset))) {
				auto it = prev(Lset.end());
				LL w = * it;
				Lset.erase(it); Lsum -= w;
				Rset.insert(w); Rsum += w;
			}
		};
		auto add = [&](LL w) {
			if(SI(Rset) == 0) { Rset.insert(w); Rsum += w; return; }
			LL m = (* Rset.begin());
			if(w < m) { Lset.insert(w); Lsum += w; } else { Rset.insert(w); Rsum += w; }
			balance();
		};
		auto del = [&](LL w) {
			LL m = (* Rset.begin());
			if(w < m) { Lset.erase(Lset.find(w)); Lsum -= w; } else { Rset.erase(Rset.find(w)); Rsum -= w; }
			balance();
		};
		auto sum = [&]() -> LL {
			LL m = (* Rset.begin());
			return (SI(Lset) * m - Lsum) + (Rsum - SI(Rset) * m);
		};
		
		RF(e, v[i]) {
			int L = e[0], R = e[1];
			int Rc = R - r;
			int Lc = L - l;
			inc(k,  Rc) { add(a[r++]); }
			inc(k, +Lc) { del(a[l++]); }
			inc(k, -Lc) { add(a[--l]); }
			ans[e[2]] = sum();
			
			/*
			incID(k, L, R) { cout << a[k] << " "; } cout << " ans : " << sum() << endl;
			RF(e, Lset) { cout << e << " "; } cout << " sum: " << Lsum << endl;
			RF(e, Rset) { cout << e << " "; } cout << " sum: " << Rsum << endl;
			cout << endl;
			*/
		}
	}
	
	RF(e, ans) { cout << e << "\n"; }
	
	return 0;
}
0