/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 2.324 MiB
#2 Accepted 2ms 2.488 MiB
#3 Accepted 3ms 2.527 MiB
#4 Accepted 3ms 772.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;
#define SC               scanf
#define PF               printf
#define ull              unsigned long long
#define ld               long double
#define F                first
#define S                second
#define pb               push_back
#define sort_a(a)        sort(a.begin(),a.end());
#define sort_d(a)        sort(a.rbegin(),a.rend());
#define READ(f)          freopen(f, "r", stdin)
#define WRITE(f)         freopen(f, "w", stdout)
#define rev(s)           reverse(s.begin(),s.end())
#define P(ok)            cout << (ok ? "YES\n": "NO\n")
#define __Heart__              ios_base :: sync_with_stdio(false); cin.tie(NULL);
#define ll long long
typedef pair< ll, ll>                   PII;
const int mx = 1e5 + 5;
int K , Ans[mx], arr[mx],freq[mx] , F[mx] ;
struct Query
{
    int index, l, r ;
    bool operator < (const Query &other)const
    {
        int block_own = l/ K ;
        int block_other = other.l / K ;
        return(block_other == block_own) ? r < other.r : block_own < block_other ;
    }

} query[mx];
void add(int index)
{
    freq[arr[index]]++ ;
}
void remove (int index)
{
   freq[arr[index]]-- ;
}
void solve()
{
    int n , q ; cin >> n ;
    for(int i = 0 ; i <= n ; i++) freq[i] = 0 ;
    for(int i = 0 ; i < n ; i++) cin >> arr[i] ;
    K = sqrt(n) ;
    cin >> q ;
    for(int i = 0 ; i < q ; i++) cin >> query[i].l >> query[i].r , query[i].index = i  , query[i].l-- , query[i].r--;
    sort(query , query + q) ;
    int l = 0, r = -1 ;
    for(int i = 0 ; i < q ; i++)
    {
        while(r < query[i].r) add(++r ) ;
        while(l < query[i].l) remove(l++) ;
        while(r > query[i].r) remove(r-- );
        while(l > query[i].l) add(--l) ;
        int cnt = 0 ;
        int dis = query[i].r - query[i].l + 1 ;
        for(int j = 1 ; j <= dis ; j++){
            if(!freq[j]) cnt++ ;
        }
        Ans[query[i].index] = cnt;
    }
    for(int i = 0 ; i < q ; i++) cout<< Ans[i] << "\n" ;
}
int main()
{
     __Heart__
    // READ("input7.txt") ;
   // WRITE("output7.txt") ;
     int t ; cin >> t ; while(t--) solve() ;
}

Information

Submit By
Type
Submission
Problem
P1157 Roy and Tree Permutation
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-30 20:52:01
Judged At
2024-12-30 20:52:01
Judged By
Score
100
Total Time
3ms
Peak Memory
2.527 MiB