結果
| 問題 |
No.404 部分門松列
|
| コンテスト | |
| ユーザー |
chocorusk
|
| 提出日時 | 2020-01-01 14:21:00 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 822 ms / 2,000 ms |
| コード長 | 2,643 bytes |
| コンパイル時間 | 1,757 ms |
| コンパイル使用メモリ | 124,336 KB |
| 実行使用メモリ | 18,432 KB |
| 最終ジャッジ日時 | 2024-11-22 03:21:18 |
| 合計ジャッジ時間 | 18,507 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 31 |
ソースコード
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, ll> P;
template<typename T>
struct BIT{ //1-indexed
vector<T> bit;
int size;
BIT(int n):size(n), bit(n+1, 0){}
void init(){
fill(bit.begin(), bit.end(), 0);
}
T sum(int i){
T s=0;
while(i>0){
s+=bit[i];
i-=(i&(-i));
}
return s;
}
void add(int i, T x){
while(i<=size){
bit[i]+=x;
i+=(i&(-i));
}
}
};
int main()
{
int n;
cin>>n;
int a[200020];
vector<int> v(n);
for(int i=0; i<n; i++){
cin>>a[i];
v[i]=a[i];
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for(int i=0; i<n; i++) a[i]=lower_bound(v.begin(), v.end(), a[i])-v.begin();
int m=v.size();
BIT<ll> bit(m);
ll c1[200020];
for(int i=0; i<n; i++){
c1[i]=bit.sum(a[i]);
bit.add(a[i]+1, 1);
}
bit.init();
ll c2[200020];
for(int i=n-1; i>=0; i--){
c2[i]=bit.sum(a[i]);
bit.add(a[i]+1, 1);
}
ll c[200020]={};
for(int i=0; i<n; i++){
c[i]+=c1[i]*c2[i];
}
ll x[200020]={}, x1[200020]={};
for(int i=0; i<n; i++) x[a[i]]++;
bit.init();
for(int i=0; i<n; i++){
c[i]-=bit.sum(a[i]);
bit.add(a[i]+1, -x1[a[i]]*(x[a[i]]-x1[a[i]]));
x1[a[i]]++;
bit.add(a[i]+1, x1[a[i]]*(x[a[i]]-x1[a[i]]));
}
bit.init();
for(int i=0; i<n; i++){
c1[i]=i-bit.sum(a[i]+1);
bit.add(a[i]+1, 1);
}
bit.init();
for(int i=n-1; i>=0; i--){
c2[i]=n-1-i-bit.sum(a[i]+1);
bit.add(a[i]+1, 1);
}
for(int i=0; i<n; i++){
c[i]+=c1[i]*c2[i];
}
fill(x1, x1+m+1, 0);
bit.init();
for(int i=0; i<n; i++){
c[i]-=(bit.sum(m)-bit.sum(a[i]+1));
bit.add(a[i]+1, -x1[a[i]]*(x[a[i]]-x1[a[i]]));
x1[a[i]]++;
bit.add(a[i]+1, x1[a[i]]*(x[a[i]]-x1[a[i]]));
}
P p[200020];
for(int i=0; i<n; i++) p[i]=P(a[i], c[i]);
sort(p, p+n);
ll t[200020]; t[0]=0;
for(int i=0; i<n; i++) t[i+1]=t[i]+p[i].second;
int q; cin>>q;
for(int i=0; i<q; i++){
int l, h; cin>>l>>h; h++;
l=lower_bound(v.begin(), v.end(), l)-v.begin();
h=lower_bound(v.begin(), v.end(), h)-v.begin();
l=lower_bound(p, p+n, P(l, 0))-p;
h=lower_bound(p, p+n, P(h, 0))-p;
cout<<t[h]-t[l]<<endl;
}
return 0;
}
chocorusk