답안 #826707

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
826707 2023-08-15T22:11:09 Z Pichon5 Index (COCI21_index) C++17
110 / 110
2432 ms 137172 KB
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <string>
#include <sstream>
#include <fstream>
#include <cmath>
#include <queue>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <bits/stdc++.h>
#define lcm(a,b) (a/__gcd(a,b))*b
#define ke ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define F first
#define S second
#define vi vector<int>
#define ll long long
using namespace std;
const int tam=200005;
int a[tam];
struct st{
    st * izq;
    st * der;
    int val;
};
st * P[tam];
st * init(int b, int e){
    int mid=(b+e)/2;
    st * nuevo=new st;
    if(b==e){
        nuevo->val=0;
        return nuevo;
    }
    st * A=init(b,mid);
    st * B=init(mid+1,e);
    nuevo->val=A->val+B->val;
    nuevo->izq=A;
    nuevo->der=B;
    return nuevo;
}

st * update(st * nodo, int b, int e, int pos, int valor){
   int mid=(b+e)/2;
   st * nuevo = new st;
   nuevo->izq=nodo->izq;
   nuevo->der=nodo->der;
   nuevo->val=nodo->val;
   if(b==e){
      nuevo->val=nodo->val+valor;
      return nuevo;
   }
   if(pos<=mid){
      nuevo->izq=update(nodo->izq,b,mid,pos,valor);
   }else{
      nuevo->der=update(nodo->der,mid+1,e,pos,valor);
   }
   nuevo->val=nuevo->izq->val + nuevo->der->val;
   return nuevo;
}

int query(st * nodo, int b, int e, int izq, int der){
    int mid=(b+e)/2;
    if(b>=izq && e<=der){
        return nodo->val;
    }
    if(der<=mid){
        return query(nodo->izq,b,mid,izq,der);
    }else{
        if(izq>=mid+1){
            return query(nodo->der,mid+1,e,izq,der);
        }else{
            return query(nodo->izq,b,mid,izq,der) + query(nodo->der,mid+1,e,izq,der);
        }
    }
}

int main(){
    int n,q;
    cin>>n>>q;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }   
    P[0]=init(0,tam-1);
    for(int i=1;i<=n;i++){
        P[i]=update(P[i-1],0,tam-1,a[i],1);
    }
    int izq,der; 
    while(q--){
        cin>>izq>>der;
        int b=0,e=der-izq+1;
        int res=0;
        while(b<=e){
            int mid=(b+e)/2;
            //quiero probar si mi respuesta es mid
            int QR=query(P[der],0,tam-1,mid,tam-1);
            int QL=query(P[izq-1],0,tam-1,mid,tam-1);
            if(QR-QL>=mid){
                res=mid;
                b=mid+1;
            }else{
                e=mid-1;
            }
        }
        cout<<res<<endl;


    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 13396 KB Output is correct
2 Correct 15 ms 13396 KB Output is correct
3 Correct 20 ms 13396 KB Output is correct
4 Correct 14 ms 13396 KB Output is correct
5 Correct 15 ms 13396 KB Output is correct
6 Correct 14 ms 13396 KB Output is correct
7 Correct 18 ms 13312 KB Output is correct
8 Correct 18 ms 13380 KB Output is correct
9 Correct 15 ms 13368 KB Output is correct
10 Correct 18 ms 13356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 13396 KB Output is correct
2 Correct 15 ms 13396 KB Output is correct
3 Correct 20 ms 13396 KB Output is correct
4 Correct 14 ms 13396 KB Output is correct
5 Correct 15 ms 13396 KB Output is correct
6 Correct 14 ms 13396 KB Output is correct
7 Correct 18 ms 13312 KB Output is correct
8 Correct 18 ms 13380 KB Output is correct
9 Correct 15 ms 13368 KB Output is correct
10 Correct 18 ms 13356 KB Output is correct
11 Correct 328 ms 43736 KB Output is correct
12 Correct 376 ms 43732 KB Output is correct
13 Correct 340 ms 43692 KB Output is correct
14 Correct 366 ms 43784 KB Output is correct
15 Correct 353 ms 43684 KB Output is correct
16 Correct 338 ms 43780 KB Output is correct
17 Correct 335 ms 43684 KB Output is correct
18 Correct 366 ms 43828 KB Output is correct
19 Correct 348 ms 43700 KB Output is correct
20 Correct 350 ms 43708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 13396 KB Output is correct
2 Correct 15 ms 13396 KB Output is correct
3 Correct 20 ms 13396 KB Output is correct
4 Correct 14 ms 13396 KB Output is correct
5 Correct 15 ms 13396 KB Output is correct
6 Correct 14 ms 13396 KB Output is correct
7 Correct 18 ms 13312 KB Output is correct
8 Correct 18 ms 13380 KB Output is correct
9 Correct 15 ms 13368 KB Output is correct
10 Correct 18 ms 13356 KB Output is correct
11 Correct 328 ms 43736 KB Output is correct
12 Correct 376 ms 43732 KB Output is correct
13 Correct 340 ms 43692 KB Output is correct
14 Correct 366 ms 43784 KB Output is correct
15 Correct 353 ms 43684 KB Output is correct
16 Correct 338 ms 43780 KB Output is correct
17 Correct 335 ms 43684 KB Output is correct
18 Correct 366 ms 43828 KB Output is correct
19 Correct 348 ms 43700 KB Output is correct
20 Correct 350 ms 43708 KB Output is correct
21 Correct 2369 ms 137028 KB Output is correct
22 Correct 2180 ms 137076 KB Output is correct
23 Correct 2432 ms 137068 KB Output is correct
24 Correct 2265 ms 137108 KB Output is correct
25 Correct 2192 ms 137080 KB Output is correct
26 Correct 2206 ms 137172 KB Output is correct
27 Correct 2290 ms 137064 KB Output is correct
28 Correct 2265 ms 136992 KB Output is correct
29 Correct 2329 ms 137016 KB Output is correct
30 Correct 2331 ms 137104 KB Output is correct