답안 #818067

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
818067 2023-08-10T01:26:37 Z vjudge1 Poklon (COCI17_poklon) C++17
140 / 140
1020 ms 23000 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;

template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}

const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 5e5 + 7;
const ll oo = 1e18 + 69;
const int sz = 800;

int n, q, a[maxn], res[maxn], l, r, cnt, mp[maxn];

struct query {
    int l, r, id;
    friend bool operator < (query &a, query &b) {
        if(a.l / sz == b.l / sz) return ((a.l / sz) & 1) == 0 ? a.r < b.r:a.r > b.r;
        return a.l / sz < b.l / sz;
    }
};
vector<query> que;

void add(int i) {
    mp[a[i]]++;
    if(mp[a[i]] == 3) cnt--; 
    if(mp[a[i]] == 2) cnt++;
}

void sub(int i) {
    mp[a[i]]--;
    if(mp[a[i]] == 2) cnt++;
    if(mp[a[i]] == 1) cnt--;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    //freopen(TASK".inp", "r", stdin);
    //freopen(TASK".out", "w", stdout);

    cin>>n>>q;
    vector<int> val;
    for(int i = 1; i <= n; i++) {
        cin>>a[i];
        val.pb(a[i]);
    }

    sort(all(val));
    val.erase(unique(all(val)), val.end());
    for(int i = 1; i <= n; i++) {
        a[i] = lower_bound(all(val), a[i]) - val.begin();
    }
    for(int i = 0; i < q; i++) {
        cin>>l>>r;
        que.pb({l, r, i});
    }

    sort(all(que));
    l = 1, r = 0, cnt = 0;
    for(auto e:que) {
        while(r < e.r) add(++r);
        while(l > e.l) add(--l);
        while(r > e.r) sub(r--);
        while(l < e.l) sub(l++);
        res[e.id] = cnt;
    }

    for(int i = 0; i < q; i++) cout<<res[i]<<"\n";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 6 ms 596 KB Output is correct
5 Correct 124 ms 4636 KB Output is correct
6 Correct 125 ms 4684 KB Output is correct
7 Correct 306 ms 9516 KB Output is correct
8 Correct 520 ms 15008 KB Output is correct
9 Correct 775 ms 18780 KB Output is correct
10 Correct 1020 ms 23000 KB Output is correct