結果
| 問題 |
No.404 部分門松列
|
| コンテスト | |
| ユーザー |
kmjp
|
| 提出日時 | 2016-07-22 23:04:12 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 2,146 bytes |
| コンパイル時間 | 1,764 ms |
| コンパイル使用メモリ | 172,708 KB |
| 実行使用メモリ | 19,496 KB |
| 最終ジャッジ日時 | 2024-11-06 09:17:18 |
| 合計ジャッジ時間 | 14,276 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | RE * 4 |
| other | RE * 31 |
コンパイルメッセージ
main.cpp: In instantiation of ‘V BIT<V, ME>::add(int, V) [with V = long long int; int ME = 20]’:
main.cpp:60:9: required from here
main.cpp:28:77: warning: no return statement in function returning non-void [-Wreturn-type]
28 | V add(int e,V v) { val[e++]+=v; while(e<=1<<ME) bit[e-1]+=v,e+=e&-e;}
| ^
ソースコード
#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;}
V set(int e,V v) { add(e,v-val[e]);}
int lower_bound(V val) {
V tv=0; int i,ent=0;
for(i=ME-1;i>=0;i--) if(tv+bit[ent+(1<<i)-1]<val) tv+=bit[ent+(1<<i)-1],ent+=(1<<i);
return ent;
}
};
BIT<ll,20> LL,RR,ret;
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];
vector<int> V;
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<<19)-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<<19)-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]);
}
map<ll,ll> sameL,sameR;
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;
}
kmjp