/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 536.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 2ms 352.0 KiB
#4 Accepted 132ms 2.836 MiB
#5 Accepted 330ms 15.402 MiB
#6 Accepted 172ms 8.977 MiB
#7 Accepted 56ms 832.0 KiB
#8 Accepted 99ms 8.301 MiB
#9 Accepted 91ms 8.039 MiB
#10 Accepted 122ms 9.039 MiB
#11 Accepted 18ms 696.0 KiB
#12 Accepted 34ms 748.0 KiB
#13 Accepted 1ms 764.0 KiB

Code

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define ff first
#define ss second
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define f(i,x,y) for(int i=x;i<y;i++)
#define f2(i,x,y) for(int i=x;i>=y;i--)
#define pii pair<int,int>
#define Fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const int MOD =1000000007;
const int INF = 1e18;
const int N = 2e5+10;
int v[N],tree[4*N];
int merge(int a,int b){
    
    return (a+b);
}
void build(int node,int b,int e)// 1 based
{
    if(b==e)
    {
        tree[node]=v[b];
        return;
    }
    int left=node*2;
    int right=node*2+1;
    int mid=(b+e)/2;
    build(left,b,mid);
    build(right,mid+1,e);
    tree[node]=merge(tree[left],tree[right]);
}
int query(int node,int b,int e,int i,int j)// 1 based 
{
    if(i>e || j<b)
        return 0;
    if(b>=i && e<=j)
        return tree[node];
    int left=node*2;
    int right=node*2+1;
    int mid=(b+e)/2;
    int p1=query(left,b,mid,i,j);
    int p2=query(right,mid+1,e,i,j);
    return merge(p1,p2);
}
void update(int node,int b,int e,int i,int value) // 1 based
{
    if(i>e || i<b)
        return;
    if(b>=i && e<=i)
    {
        tree[node]+=value;
        return;
    }
    int left=node*2;
    int right=node*2+1;
    int mid=(b+e)/2;
    update(left,b,mid,i,value);
    update(right,mid+1,e,i,value);
    tree[node]=merge(tree[left],tree[right]);
}
vector<int>indices;
int getCompressedIndex(int a) {
    return lower_bound(indices.begin(), indices.end(), a) - indices.begin() + 1;
}
//========= COORDINATE COMPRESSION =======
void solve(int tc){
    int n; cin >> n;
    vector<int>a(n),pre(n);
    vector<pii>after(n),before(n);
    for(int i=0;i<n;i++){
        cin >> a[i];
        indices.pb(a[i]);
    }
    sort(indices.begin(), indices.end());
    indices.erase(unique(indices.begin(), indices.end()), indices.end());
    build(1,1,n);
    //cout << before[i].ff << " " << before[i].ss << endl;
    for(int i=0;i<n;i++){
        a[i] = getCompressedIndex(a[i]);
        before[i].ff = query(1,1,n,1,a[i]-1);
        before[i].ss = query(1,1,n,a[i]+1,n);
        update(1,1,n,a[i],1);
    }
    build(1,1,n);
    for(int i=n-1;i>=0;i--){
        after[i].ff = query(1,1,n,1,a[i]-1);
        after[i].ss = query(1,1,n,a[i]+1,n);
        update(1,1,n,a[i],1);
    }
    for(int i=0;i<n;i++){
        pre[i] = before[i].ff * after[i].ss;
        pre[i] += before[i].ss * after[i].ff;
    }
    int q; cin >> q;
    while(q--){
        int x; cin >> x;
        cout << pre[x-1] << endl;
    }
    indices.clear();
}
int32_t main(){

    Fast

    int t=1;

    cin >> t;

    for(int tc=1;tc<=t;tc++){

        solve(tc);
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1079 Roy and Query (Easy Version)
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-03 22:43:00
Judged At
2024-11-11 02:43:50
Judged By
Score
100
Total Time
330ms
Peak Memory
15.402 MiB