結果

問題 No.924 紲星
ユーザー 👑 kmjpkmjp
提出日時 2020-02-01 01:26:07
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,794 bytes
コンパイル時間 3,889 ms
コンパイル使用メモリ 176,212 KB
実行使用メモリ 36,380 KB
最終ジャッジ日時 2023-10-17 18:26:38
合計ジャッジ時間 12,659 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
16,556 KB
testcase_01 AC 5 ms
16,556 KB
testcase_02 AC 5 ms
16,560 KB
testcase_03 WA -
testcase_04 AC 6 ms
16,576 KB
testcase_05 AC 8 ms
16,608 KB
testcase_06 WA -
testcase_07 AC 7 ms
16,572 KB
testcase_08 AC 818 ms
36,380 KB
testcase_09 AC 814 ms
36,128 KB
testcase_10 AC 819 ms
36,108 KB
testcase_11 AC 795 ms
36,096 KB
testcase_12 AC 820 ms
36,380 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 351 ms
21,880 KB
testcase_17 WA -
testcase_18 AC 5 ms
16,492 KB
権限があれば一括ダウンロードができます

ソースコード

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;
ll A[202020];
int L[202020],R[202020];
ll ret[202020],M[202020];

template<class V, int ME> class BIT {
public:
	V bit[1<<ME];
	V operator()(int e) {if(e<0) return 0;V s=0;e++;while(e) s+=bit[e-1],e-=e&-e; return s;}
	void add(int e,V v) { e++; while(e<=1<<ME) bit[e-1]+=v,e+=e&-e;}
};
BIT<ll,18> bt;

pair<ll,int> P[202020];
vector<int> ev[202020];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>Q;
	FOR(i,N) {
		cin>>A[i];
		P[i]={A[i],i};
	}
	sort(P,P+N);
	
	FOR(i,Q) {
		cin>>L[i]>>R[i];
		L[i]--;
		M[i]=N-1;
	}
	for(j=17;j>=0;j--) {
		FOR(i,N) {
			if(M[i]-(1<<j)>=0) ev[M[i]-(1<<j)].push_back(i);
		}
		
		FOR(i,N) {
			bt.add(P[i].second,1);
			FORR(e,ev[i]) {
				if(bt(R[e]-1)-bt(L[e]-1)>=(R[e]-L[e]+1)/2) M[e]-=1<<j;
			}
			ev[i].clear();
		}
		FOR(i,N) bt.add(i,-1);
	}
	
	FOR(i,Q) {
		ev[M[i]].push_back(i);
		ll v=P[M[i]].first;
		ret[i]+=1LL*((R[i]-L[i]+1)/2)*v-1LL*(R[i]-L[i]-(R[i]-L[i]+1)/2)*v;
	}
	FOR(i,N) {
		bt.add(P[i].second,A[P[i].second]);
		FORR(e,ev[i]) {
			ret[e]-=2*(bt(R[e]-1)-bt(L[e]-1));
		}
	}
	
	FOR(i,Q) {
		ret[i]+=bt(R[i]-1)-bt(L[i]-1);
		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