답안 #440137

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
440137 2021-07-01T16:07:20 Z FatihSolak Index (COCI21_index) C++17
60 / 110
2500 ms 135564 KB
#include <bits/stdc++.h>
#define N 200005
using namespace std;
int arr[N];
int cnt[N];
struct Node{
    int sum=0;
    Node *lc=NULL,*rc=NULL;
    Node(int a){
        sum = a;
    }
    Node(Node *a, Node *b){
        if(a != NULL)sum += a->sum;
        if(b != NULL)sum += b->sum;
        lc = a;
        rc = b;
    }
};
Node* build(int l,int r){
    if(l == r){
        return new Node(cnt[l]);
    }
    int m = (l+r)/2;
    return new Node(build(l,m),build(m+1,r));
}
Node* upd(Node* v,int l,int r,int pos,int val){
    if(l == r){
        return new Node(v->sum + val);
    }
    int m = (l+r)/2;
    if(pos <=m){
        return new Node(upd(v->lc,l,m,pos,val),v->rc);
    }
    else return new Node(v->lc,upd(v->rc,m+1,r,pos,val));
}
int get(Node* v1,Node* v2,int tl,int tr, int l){
    if(tr <  l){
        return 0;
    }
    if(l <= tl){
        return v1->sum - v2 -> sum;
    }
    int tm  = (tl+tr)/2;
    return get(v1->lc,v2->lc,tl,tm,l) + get(v1->rc,v2->rc,tm+1,tr,l);
}
int get(Node* v,int tl,int tr, int l){
    if(tr <  l){
        return 0;
    }
    if(l <= tl){
        return v->sum;
    }
    int tm  = (tl+tr)/2;
    return get(v->lc,tl,tm,l) + get(v->rc,tm+1,tr,l);
}
int main(){
    int n,q;
    scanf("%d%d",&n,&q);
    for(int i=0;i<n;i++){
        scanf("%d",&arr[i]);
        cnt[arr[i]]++;
    }
    vector<Node *> roots;
    roots.push_back(build(1,N-1));
    for(int i=1;i<n;i++){
        roots.push_back(upd(roots[i-1],1,N-1,arr[i-1],-1));
    }
    while(q--){
        int ql,qr;
        scanf("%d%d",&ql,&qr);
        ql--,qr--;
        int l = 1,r = qr-ql+1;
        while(l < r){
            int m = (l+r+1)/2;
            int val;
            if(qr +1 != n){
                val = get(roots[ql],roots[qr+1],1,N-1,m);
            }
            else val = get(roots[ql],1,N-1,m);
            if(val >= m){
                l = m;
            }
            else r = m-1;
        }
        printf("%d\n",l);
    }
}

Compilation message

index.cpp: In function 'int main()':
index.cpp:58:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |     scanf("%d%d",&n,&q);
      |     ~~~~~^~~~~~~~~~~~~~
index.cpp:60:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |         scanf("%d",&arr[i]);
      |         ~~~~~^~~~~~~~~~~~~~
index.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |         scanf("%d%d",&ql,&qr);
      |         ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 13388 KB Output is correct
2 Correct 20 ms 13404 KB Output is correct
3 Correct 20 ms 13380 KB Output is correct
4 Correct 22 ms 13404 KB Output is correct
5 Correct 20 ms 13388 KB Output is correct
6 Correct 20 ms 13388 KB Output is correct
7 Correct 20 ms 13388 KB Output is correct
8 Correct 22 ms 13336 KB Output is correct
9 Correct 28 ms 13432 KB Output is correct
10 Correct 28 ms 13324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 13388 KB Output is correct
2 Correct 20 ms 13404 KB Output is correct
3 Correct 20 ms 13380 KB Output is correct
4 Correct 22 ms 13404 KB Output is correct
5 Correct 20 ms 13388 KB Output is correct
6 Correct 20 ms 13388 KB Output is correct
7 Correct 20 ms 13388 KB Output is correct
8 Correct 22 ms 13336 KB Output is correct
9 Correct 28 ms 13432 KB Output is correct
10 Correct 28 ms 13324 KB Output is correct
11 Correct 343 ms 43956 KB Output is correct
12 Correct 370 ms 43976 KB Output is correct
13 Correct 357 ms 43880 KB Output is correct
14 Correct 342 ms 43948 KB Output is correct
15 Correct 340 ms 43984 KB Output is correct
16 Correct 344 ms 43900 KB Output is correct
17 Correct 325 ms 43912 KB Output is correct
18 Correct 342 ms 43900 KB Output is correct
19 Correct 369 ms 43936 KB Output is correct
20 Correct 388 ms 43964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 13388 KB Output is correct
2 Correct 20 ms 13404 KB Output is correct
3 Correct 20 ms 13380 KB Output is correct
4 Correct 22 ms 13404 KB Output is correct
5 Correct 20 ms 13388 KB Output is correct
6 Correct 20 ms 13388 KB Output is correct
7 Correct 20 ms 13388 KB Output is correct
8 Correct 22 ms 13336 KB Output is correct
9 Correct 28 ms 13432 KB Output is correct
10 Correct 28 ms 13324 KB Output is correct
11 Correct 343 ms 43956 KB Output is correct
12 Correct 370 ms 43976 KB Output is correct
13 Correct 357 ms 43880 KB Output is correct
14 Correct 342 ms 43948 KB Output is correct
15 Correct 340 ms 43984 KB Output is correct
16 Correct 344 ms 43900 KB Output is correct
17 Correct 325 ms 43912 KB Output is correct
18 Correct 342 ms 43900 KB Output is correct
19 Correct 369 ms 43936 KB Output is correct
20 Correct 388 ms 43964 KB Output is correct
21 Correct 2401 ms 135564 KB Output is correct
22 Execution timed out 2536 ms 134104 KB Time limit exceeded
23 Halted 0 ms 0 KB -