Submission #44779

# Submission time Handle Problem Language Result Execution time Memory
44779 2018-04-06T09:10:06 Z evpipis Poklon (COCI17_poklon) C++11
140 / 140
2074 ms 36092 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
typedef pair<int, int> ii;

const int len = 5e5+5;
int arr[len], ans, k, cnt[len];
pair<int, ii> ask[len];
map<int, int> mymap;

bool comp(pair<int, ii> a, pair<int, ii> b){
    if (a.se.fi/k < b.se.fi/k) return true;
    if (a.se.fi/k > b.se.fi/k) return false;
    return (a.se.se < b.se.se);
}

void add(int i, int x){
    if (cnt[arr[i]] == 2) ans--;
    cnt[arr[i]] += x;
    if (cnt[arr[i]] == 2) ans++;
}

int main(){
    int n, q, cur = 0, temp;
    scanf("%d %d", &n, &q);
    k = sqrt(n);
    for (int i = 0; i < n; i++){
        scanf("%d", &temp);
        if (!mymap.count(temp))
            arr[i] = mymap[temp] = cur++;
        else
            arr[i] = mymap[temp];
    }

    for (int i = 0; i < q; i++){
        scanf("%d %d", &ask[i].se.fi, &ask[i].se.se);
        ask[i].se.fi--, ask[i].se.se--;
        ask[i].fi = i;
    }
    sort(ask, ask+q, comp);

    int l = 0, r = -1;
    for (int i = 0; i < q; i++){
        while (r < ask[i].se.se)
            r++, add(r, 1);
        while (r > ask[i].se.se)
            add(r, -1), r--;
        while (l < ask[i].se.fi)
            add(l, -1), l++;
        while (l > ask[i].se.fi)
            l--, add(l, 1);

        ask[i].se.fi = ans;
    }

    sort(ask, ask+q);

    for (int i = 0; i < q; i++)
        printf("%d\n", ask[i].se.fi);
    return 0;
}

Compilation message

poklon.cpp: In function 'int main()':
poklon.cpp:27:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &q);
     ~~~~~^~~~~~~~~~~~~~~~~
poklon.cpp:30:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &temp);
         ~~~~~^~~~~~~~~~~~~
poklon.cpp:38:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &ask[i].se.fi, &ask[i].se.se);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 3 ms 488 KB Output is correct
4 Correct 8 ms 700 KB Output is correct
5 Correct 230 ms 3708 KB Output is correct
6 Correct 245 ms 5340 KB Output is correct
7 Correct 593 ms 10500 KB Output is correct
8 Correct 1069 ms 17200 KB Output is correct
9 Correct 1508 ms 26016 KB Output is correct
10 Correct 2074 ms 36092 KB Output is correct