Submission #370258

# Submission time Handle Problem Language Result Execution time Memory
370258 2021-02-23T15:45:43 Z Atill83 Poklon (COCI17_poklon) C++14
140 / 140
1811 ms 21732 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 5e5+5;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n, q;
int a[MAXN];
unordered_map<int, int> mp;

const int BS = 710;
struct que{
    int l, r, idx;
    bool operator < (que ot) const {
        return  (l / BS == ot.l / BS ? r < ot.r : l < ot.l);
    };
} Q[MAXN];

int ans[MAXN];
int cnt[MAXN];

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

    #ifdef Local
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
    #endif

    cin>>n>>q;

    vector<int> vc;



    for(int i = 0; i < n; i++){
        cin>>a[i];
        vc.push_back(a[i]);
    }

    sort(vc.begin(), vc.end());
    vc.resize(unique(vc.begin(), vc.end()) - vc.begin());

    int c = 0;

    for(int i: vc)
        mp[i] = c++;

    for(int i = 0; i < n; i++)
        a[i] = mp[a[i]];

    for(int i = 0; i < q; i++){
        cin>>Q[i].l>>Q[i].r;
        Q[i].l--;
        Q[i].r--;
        Q[i].idx = i;
    }
    
    sort(Q, Q + q);
    
    int r = -1, l = 0, cur = 0;

    for(int i = 0; i < q; i++){
        while(r < Q[i].r){
            r++;
            cnt[a[r]]++;
            if(cnt[a[r]] == 2)
                cur++;
            else if(cnt[a[r]] == 3)
                cur--;
        }

        while(l > Q[i].l){
            l--;
            cnt[a[l]]++;
            if(cnt[a[l]] == 2)
                cur++;
            else if(cnt[a[l]] == 3)
                cur--;
        }

        while(r > Q[i].r){
            cnt[a[r]]--;
            if(cnt[a[r]] == 2)
                cur++;
            else if(cnt[a[r]] == 1)
                cur--;
            r--;
        }

        while(l < Q[i].l){
            cnt[a[l]]--;
            if(cnt[a[l]] == 2)
                cur++;
            else if(cnt[a[l]] == 1)
                cur--;
            l++;
        }

        ans[Q[i].idx] = cur;
    
    }

    for(int i = 0; i < q; i++)
        cout<<ans[i]<<endl;
    


    #ifdef Local
        cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
    #endif
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 2 ms 364 KB Output is correct
3 Correct 2 ms 364 KB Output is correct
4 Correct 7 ms 620 KB Output is correct
5 Correct 172 ms 4460 KB Output is correct
6 Correct 133 ms 4488 KB Output is correct
7 Correct 454 ms 8936 KB Output is correct
8 Correct 790 ms 13180 KB Output is correct
9 Correct 1208 ms 17508 KB Output is correct
10 Correct 1811 ms 21732 KB Output is correct