結果

問題 No.924 紲星
ユーザー 👑 kmjpkmjp
提出日時 2019-11-09 00:33:39
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,904 bytes
コンパイル時間 1,811 ms
コンパイル使用メモリ 178,872 KB
実行使用メモリ 22,956 KB
最終ジャッジ日時 2023-10-13 06:03:30
合計ジャッジ時間 8,181 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,952 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 18 ms
4,348 KB
testcase_04 AC 6 ms
4,348 KB
testcase_05 AC 21 ms
4,352 KB
testcase_06 AC 15 ms
4,348 KB
testcase_07 AC 6 ms
4,348 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 signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,q;
int A[202020];
int L[202020],R[202020];
const int DI=500;
vector<pair<int,int>> ev[500];
ll ret[202020];

multiset<int> P,Q;
ll AP=0,AQ=0;

void add(int v) {
	P.insert(v);
	AP+=v;
	v=*P.rbegin();
	AP-=v;
	P.erase(P.find(v));
	Q.insert(v);
	AQ+=v;
	if(P.size()<Q.size()) {
		v=*Q.begin();
		AQ-=v;
		AP+=v;
		P.insert(v);
		Q.erase(Q.begin());
	}
}
void del(int v) {
	if(P.count(v)) {
		P.erase(P.find(v));
		AP-=v;
		if(P.size()<Q.size()) {
			v=*Q.begin();
			AQ-=v;
			AP+=v;
			P.insert(v);
			Q.erase(Q.begin());
		}
	}
	else if(Q.count(v)) {
		Q.erase(Q.find(v));
		AQ-=v;
		if(Q.size()+2<=P.size()) {
			v=*P.rbegin();
			AP-=v;
			P.erase(P.find(v));
			Q.insert(v);
			AQ+=v;
		}
		
	}
}


void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>q;
	FOR(i,N) cin>>A[i];
	FOR(i,q) {
		cin>>L[i]>>R[i];
		L[i]--;
		ev[L[i]/DI].push_back({R[i],i});
	}
	FOR(i,500) {
		sort(ALL(ev[i]));
		P.clear();
		Q.clear();
		AP=AQ=0;
		int SL=i*DI,SR=i*DI;
		FORR(e,ev[i]) {
			x=e.second;
			while(L[x]<SL) add(A[--SL]);
			while(SR<R[x]) add(A[SR++]);
			while(SL<L[x]) del(A[SL++]);
			while(R[x]<SR) add(A[--SR]);
			y=*P.rbegin();
			ret[x]=(AQ-1LL*y*Q.size())+(1LL*y*P.size()-AP);
		}
		
	}
	FOR(i,q) cout<<ret[i]<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0