/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 11ms 588.0 KiB
#2 Accepted 11ms 580.0 KiB
#3 Accepted 161ms 1.109 MiB
#4 Accepted 269ms 5.152 MiB
#5 Accepted 632ms 20.617 MiB
#6 Accepted 628ms 20.574 MiB
#7 Accepted 636ms 20.613 MiB
#8 Accepted 616ms 20.645 MiB
#9 Accepted 1213ms 21.445 MiB
#10 Accepted 1250ms 41.438 MiB
#11 Accepted 248ms 1.367 MiB

Code

#include<bits/stdc++.h>
#define REP(i,n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define ll long long int
#define INF 1000000000
#define endl '\n'
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;
 
using namespace std;
#define maxN 200100
vi st[4*maxN];
int ar[maxN];
 
void build(int si , int ss , int se)
{
    if(ss == se)
    {
        st[si].pb(ar[ss]);
        return;
    }
 
    int mid = (ss + se) / 2;
    build(2*si , ss , mid);
    build(2*si + 1, mid+1 , se);
 
    int i=0;
    int j=0;
 
    while(i < st[2*si].size() && j < st[2*si+1].size())
    {
        if(st[2*si][i] <= st[2*si+1][j])
        st[si].pb(st[2*si][i]) , i++;
 
        else
        st[si].pb(st[2*si+1][j]) , j++;
    }
 
    while(i < st[2*si].size())
    st[si].pb(st[2*si][i]) , i++;
 
    while(j < st[2*si+1].size())
    st[si].pb(st[2*si+1][j]) , j++;
 
}
 
int query(int si , int ss ,int se , int qs , int qe , int k)
{
    if(ss > qe || se < qs) return 0;
 
    if(ss >= qs && se <= qe)
    {
        int res = upper_bound(st[si].begin() , st[si].end() , k-1) - st[si].begin();
        return res;
    }
 
    int mid = (ss + se) / 2;
    int l = query(2*si , ss , mid , qs , qe , k);
    int r = query(2*si + 1 , mid + 1 , se , qs , qe , k);
 
    return l + r;
}
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t=1;
    cin>>t;
    while(t--){
    int n , q , l , r , k;
    cin>>n;
    REP(i , n) cin>>ar[i];
    build(1 , 1 , n);
    cin>>q;
    while(q--)
    {
        int l,x,r;
        cin>>l>>x>>r;
        ll ans=0;
        ll leftside = query(1 , 1 , n , l, x-1 ,ar[x]);
        ll rightside=(r-x)-query(1 , 1 , n , x+1,r,ar[x]+1);
        ans+=(leftside * rightside);
        //cout<<leftside<<" "<<rightside<<endl;
        leftside = (x-l+1)-query(1 , 1 , n , l, x ,ar[x]+1);
        rightside=query(1 , 1 , n , x+1, r ,ar[x]);
        //cout<<leftside<<" "<<rightside<<endl;
        ans+=(leftside * rightside);
        cout<<ans<<"\n";
    }
    for(int i=0;i<=4*n;i++)st[i].clear();
}
return 0;
}
 
 

Information

Submit By
Type
Submission
Problem
P1082 Roy and Query (Hard Version)
Language
C++20 (G++ 13.2.0)
Submit At
2024-08-06 12:12:40
Judged At
2024-12-17 11:28:29
Judged By
Score
100
Total Time
1250ms
Peak Memory
41.438 MiB