답안 #1069890

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1069890 2024-08-22T09:41:37 Z Cadoc Index (COCI21_index) C++14
110 / 110
216 ms 23264 KB
/*
    Author: Cadocx
    Codeforces: https://codeforces.com/profile/Kadoc
    VNOJ: oj.vnoi.info/user/Cadoc
*/

#include <bits/stdc++.h>
using namespace std;

// input/output
#define fastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define el cout << '\n'
#define debug(x) cout << #x << " = " << x << '\n'
#define execute cerr << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << "s"
// #pragma GCC optimize("O2", "unroll-loops", "Ofast")
// #pragma GCC target("avx,avx2,fma")
//data type
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define piv pair<int, vector<int>>
#define vi vector<int>
#define vl vector<ll>
#define vc vector<char>
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return 1; }; return 0; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return 1; }; return 0; }
//STL
#define sz(x) (int)(x).size()
#define FOR(i,l,r) for(auto i = l; i <= r; i++)
#define FORD(i,r,l) for(auto i = r; i >= l; i--)
#define forin(i,a) for(auto i : a)
#define pb push_back
#define eb emplace_back
#define pf push_front
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
//bitmask
#define bitcnt(n) __builtin_popcount(n)
#define mask(i) (1 << (i))
#define bit(n, i) (((n) >> (i)) & 1)
#define set_on(n, i) ((n) | mask(i))
#define set_off(n, i) ((n) & ~mask(i))
//constant
#define N 200005
#define MOD 1000230007
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define base 31
#define Kadoc 0

int n, q, M;
int a[N];
int ans[N];
vector<int> pos[N];
int t[N];

void upd(int x, int v){
    for(;x<=n; x+=x&-x) t[x] += v;
}

int get(int x){
    int Ans = 0;
    for(;x; x-=x&-x) Ans += t[x];
    return Ans;
}

struct Node{
    int l, r;
} qr[N];

void calc(int t, int l, int r, vector<int> &cand){
    if(l > r || !sz(cand)) return;

    int m = (l+r)>>1;
    vector<int> candL, candR;

    if(t) FOR(x, m, r) for(int i:pos[x]) upd(i, 1);
    else FOR(x, l-1, m-1) for(int i:pos[x]) upd(i, -1);

    for(int id:cand){
        if(get(qr[id].r) - get(qr[id].l - 1) >= m) candR.pb(id), ans[id] = m;
        else candL.pb(id);
    }

    calc(1, l, m-1, candL);
    calc(0, m+1, r, candR);

    if(t) FOR(x, m, r) for(int i:pos[x]) upd(i, -1);
    else FOR(x, l-1, m-1) for(int i:pos[x]) upd(i, +1);

    while(candL.size()) candL.pop_back();
    while(candR.size()) candR.pop_back();
}

void solve(){
    cin >> n >> q;
    M = 0;
    FOR(i, 1, n){
        cin >> a[i];
        M = max(M, a[i]);
        pos[a[i]].pb(i);
    }

    vector<int> v;
    FOR(i, 1, q){
        int l, r; cin >> l >> r;
        qr[i] = {l, r};
        v.pb(i);
    }

    calc(1, 1, M, v);

    FOR(i, 1, q) cout << ans[i], el;
}

int main(){
    #define NAME "TASK"
    if(fopen(NAME".inp", "r")){
        freopen(NAME".inp", "r", stdin);
        freopen(NAME".out", "w", stdout);
    }

    fastIO;
    
    if(Kadoc){
        int tc; cin >> tc;
        while(tc--){
            solve();
        }
    } else solve();
}

Compilation message

index.cpp: In function 'int main()':
index.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |         freopen(NAME".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
index.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |         freopen(NAME".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 49 ms 11352 KB Output is correct
12 Correct 48 ms 12240 KB Output is correct
13 Correct 46 ms 12156 KB Output is correct
14 Correct 42 ms 12136 KB Output is correct
15 Correct 42 ms 12256 KB Output is correct
16 Correct 44 ms 12236 KB Output is correct
17 Correct 45 ms 12168 KB Output is correct
18 Correct 40 ms 12260 KB Output is correct
19 Correct 43 ms 12200 KB Output is correct
20 Correct 43 ms 12172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 49 ms 11352 KB Output is correct
12 Correct 48 ms 12240 KB Output is correct
13 Correct 46 ms 12156 KB Output is correct
14 Correct 42 ms 12136 KB Output is correct
15 Correct 42 ms 12256 KB Output is correct
16 Correct 44 ms 12236 KB Output is correct
17 Correct 45 ms 12168 KB Output is correct
18 Correct 40 ms 12260 KB Output is correct
19 Correct 43 ms 12200 KB Output is correct
20 Correct 43 ms 12172 KB Output is correct
21 Correct 192 ms 23084 KB Output is correct
22 Correct 193 ms 23040 KB Output is correct
23 Correct 204 ms 23248 KB Output is correct
24 Correct 192 ms 23240 KB Output is correct
25 Correct 213 ms 23240 KB Output is correct
26 Correct 203 ms 23264 KB Output is correct
27 Correct 216 ms 22916 KB Output is correct
28 Correct 193 ms 23248 KB Output is correct
29 Correct 192 ms 23240 KB Output is correct
30 Correct 201 ms 22980 KB Output is correct