/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 33ms 37.051 MiB
#2 Accepted 32ms 37.137 MiB
#3 Accepted 32ms 37.074 MiB
#4 Accepted 485ms 43.711 MiB
#5 Accepted 589ms 90.816 MiB
#6 Accepted 1019ms 63.945 MiB
#7 Accepted 246ms 37.508 MiB
#8 Accepted 716ms 63.57 MiB
#9 Accepted 562ms 63.344 MiB
#10 Accepted 509ms 64.348 MiB
#11 Accepted 94ms 37.277 MiB
#12 Accepted 150ms 37.348 MiB
#13 Accepted 33ms 37.02 MiB

Code

#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define dbg(a,b,c,d) cerr<<a<<"  "<<b<<"  "<<c<<"  "<<d<<endl;
#define kill(a) {cout<<a<<endl;continue;}
#define KILL(a) {cout<<a<<endl;return 0;}
#define debug cerr<<"Error Found"<<endl;
#define mem(a,b) memset(a,b,sizeof(a))
#define lcm(a, b) (a/__gcd(a,b))*b
#define w(t) cin>>t;while(t--)
#define pi  2 * acos(0.0)
#define endl "\n"
int t, cs = 0;
const int mxn = 2e5 + 3, mod = 1e9 + 7;
vector<int> tre[mxn * 4], rtre[mxn * 4];
int ar[mxn];
void build(int st, int ed, int node)
{
    for(int i = st; i <= ed; i++)tre[node].push_back(ar[i]), rtre[node].push_back(-ar[i]);
    sort(tre[node].begin(), tre[node].end());
    sort(rtre[node].begin(), rtre[node].end());
    if(st == ed)return;
    int mid = st + ed >> 1;
    build(st, mid, node * 2);
    build(mid + 1, ed, node * 2 + 1);
}

int query(int st, int ed, int node, int l, int r, int x)
{
    if(st > r or ed < l)return 0;
    if(st >= l and ed <= r)
    {
        auto it = lower_bound(tre[node].begin(), tre[node].end(), x) - tre[node].begin();
        return it;
    }
    int mid = st + ed >> 1;
    return query(st, mid, node * 2, l, r, x) + query(mid + 1, ed, node * 2 + 1, l, r, x);
}
int rquery(int st, int ed, int node, int l, int r, int x)
{
    if(st > r or ed < l)return 0;
    if(st >= l and ed <= r)
    {
       auto it = lower_bound(rtre[node].begin(), rtre[node].end(), -x) - rtre[node].begin();
       return it;
    }
    int mid = st + ed >> 1;
    return rquery(st, mid, node * 2, l, r, x) + rquery(mid + 1, ed, node * 2 + 1, l, r, x);
}
void refresh(int n)
{
    for(int i = 1; i <= n; i++)tre[i].clear(), rtre[i].clear();
}
int32_t main()
{
   //fast;
   w(t)
   {
       int n;
       cin >> n;
       refresh(n * 4);
       for(int i = 1; i <= n; i++)cin >> ar[i];
       build(1, n, 1);
       int q;
       cin >> q;
       while(q--)
       {
           int l, x, r;
           //cin >> l >> x >> r;
           cin >> x;
           int a = query(1, n, 1, 1, x, ar[x]), b = rquery(1, n, 1, x, n, ar[x]);
           int64_t ans = 1LL * a * b;
           a = rquery(1, n, 1, 1, x, ar[x]), b = query(1, n, 1, x, n, ar[x]);
           ans += 1LL * a * b;
           cout << ans << endl;

       }
   }
}

Information

Submit By
Type
Submission
Problem
P1079 Roy and Query (Easy Version)
Language
C++17 (G++ 13.2.0)
Submit At
2024-11-07 12:48:49
Judged At
2024-11-11 02:23:21
Judged By
Score
100
Total Time
1019ms
Peak Memory
90.816 MiB