結果

問題 No.404 部分門松列
ユーザー 👑 kmjpkmjp
提出日時 2021-04-14 23:29:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,945 bytes
コンパイル時間 2,434 ms
コンパイル使用メモリ 211,508 KB
実行使用メモリ 22,312 KB
最終ジャッジ日時 2023-09-13 13:27:01
合計ジャッジ時間 18,421 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In instantiation of ‘V BIT<V, ME>::add(int, V) [with V = long long int; int ME = 21]’:
main.cpp:55:9:   required from here
main.cpp:28:77: 警告: 非 void を戻す関数内に return 文がありません [-Wreturn-type]
   28 |         V add(int e,V v) { val[e++]+=v; while(e<=1<<ME) bit[e-1]+=v,e+=e&-e;}
      |                                                                             ^

ソースコード

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

ll LU[202020],RU[202020];
ll LD[202020],RD[202020];


template<class V, int ME> class BIT {
public:
	V bit[1<<ME],val[1<<ME];
	V total(int e) {V s=0;e++;while(e) s+=bit[e-1],e-=e&-e; return s;}
	V add(int e,V v) { val[e++]+=v; while(e<=1<<ME) bit[e-1]+=v,e+=e&-e;}
};

BIT<ll,21> LL,RR,ret;

vector<int> V;
map<ll,ll> sameL,sameR;

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(i,N) cin>>A[i];
	cin>>Q;
	FOR(i,Q) cin>>L[i]>>R[i];
	
	V.push_back(0);
	V.push_back(1<<30);
	FOR(i,N) V.push_back(A[i]);
	FOR(i,Q) V.push_back(L[i]),V.push_back(R[i]);
	sort(ALL(V));
	V.erase(unique(ALL(V)),V.end());
	
	FOR(i,N) {
		A[i]=lower_bound(ALL(V),A[i])-V.begin();
		LU[i]=LL.total(1<<20)-LL.total(A[i]);
		LD[i]=LL.total(A[i]-1);
		LL.add(A[i],1);
	}
	for(i=N-1;i>=0;i--) {
		RU[i]=RR.total(1<<20)-RR.total(A[i]);
		RD[i]=RR.total(A[i]-1);
		RR.add(A[i],1);
		ret.add(A[i],LU[i]*RU[i]+LD[i]*RD[i]);
	}
	
	FOR(i,N) sameR[A[i]]++;
	ll same=0;
	FOR(i,N) {
		same -= sameL[A[i]]*sameR[A[i]];
		ret.add(A[i],-same);
		sameR[A[i]]--;
		sameL[A[i]]++;
		same += sameL[A[i]]*sameR[A[i]];
	}
	
	FOR(i,Q) {
		x=lower_bound(ALL(V),L[i])-V.begin();
		y=lower_bound(ALL(V),R[i])-V.begin();
		cout<<ret.total(y)-ret.total(x-1)<<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);
	solve(); return 0;
}
0