답안 #518947

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
518947 2022-01-25T08:59:35 Z ak2006 Poklon (COCI17_poklon) C++14
140 / 140
3046 ms 41340 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<string>;
const ll mod = 1e9 + 7,inf = 1e18;
#define pb push_back
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
void setIO()
{
    fast;
    // #ifndef ONLINE_JUDGE
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    // #endif
}
const int rn = 700;
vi freq(5e5 + 1);
int ans1;
bool cmp(vi&a,vi&b)
{
    if ((a[0]/rn) != (b[0]/rn))
        return ((a[0]/rn) < (b[0]/rn));
    return a[1] < b[1];
}
void add(int i)
{
    if (freq[i] == 1)
        ans1++;
    if (freq[i] == 2)
        ans1--;
    freq[i]++;
}   
void sub(int i)
{
    if (freq[i] == 3)
        ans1++;
    if (freq[i] == 2)
        ans1--;
    freq[i]--;
}
int main()
{
    setIO();
    int n,q;
    cin>>n>>q;
    vi a(n + 1),ans(q);
    vvi queries(q,vi(3));
    for (int i = 1;i<=n;i++)
        cin>>a[i];
    vector<int> b = a;
    sort(b.begin() + 1, b.end());
    map<int, int> m;
    for (int i = 1; i <= n; i++) 
        m[b[i]] = i;
    for (int i = 1; i <= n; i++) 
        a[i] = m[a[i]];
    for (int i = 0;i<q;i++){
        int l,r;
        cin>>l>>r;
        queries[i][0] = l,queries[i][1] = r;
        queries[i][2] = i;
    }
    sort(queries.begin(),queries.end(),cmp);
    int curL = 1,curR = 0;
    for (int i = 0;i<q;i++){
        while (curL > queries[i][0]){
            curL--;
            add(a[curL]);
        }
        while (curR < queries[i][1]){
            curR++;
            add(a[curR]);
        }
        while (curL < queries[i][0]){
            sub(a[curL]);
            curL++;
        }
        while (curR > queries[i][1]){
            sub(a[curR]);
            curR--;
        }
        ans[queries[i][2]] = ans1;
    }
    for (int i = 0;i<q;i++)cout<<ans[i]<<"\n";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2252 KB Output is correct
2 Correct 2 ms 2252 KB Output is correct
3 Correct 3 ms 2296 KB Output is correct
4 Correct 11 ms 2636 KB Output is correct
5 Correct 253 ms 10448 KB Output is correct
6 Correct 263 ms 10440 KB Output is correct
7 Correct 722 ms 19244 KB Output is correct
8 Correct 1370 ms 27716 KB Output is correct
9 Correct 2107 ms 34628 KB Output is correct
10 Correct 3046 ms 41340 KB Output is correct