Submission #479649

# Submission time Handle Problem Language Result Execution time Memory
479649 2021-10-12T16:31:31 Z aris12345678 Poklon (COCI17_poklon) C++14
84 / 140
5000 ms 18500 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define X first
#define Y second

const int mxN = 500005;
int block, cnt = 0;
int a[mxN];
map<int, int> mp;

struct Q {
    int X, Y, pos;
};

bool comp(Q a, Q b) {
    if(a.X/block != b.X/block)
        return a.X/block < b.X/block;
    return a.Y < b.Y;
}

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

void remove(int p) {
    if(mp[a[p]] == 2)
        cnt--;
    mp[a[p]]--;
    if(mp[a[p]] == 2)
        cnt++;
}

int main() {
    int n, q;
    scanf("%d %d", &n, &q);
    block = int(sqrt(n));
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    vector<Q> queries;
    for(int i = 0; i < q; i++) {
        int l, r;
        scanf("%d %d", &l, &r);
        queries.push_back({l, r, i});
    }
    sort(queries.begin(), queries.end(), comp);
    vector<int> ans(q);
    int ml = 1, mr = 0;
    for(int i = 0; i < q; i++) {
        int l = queries[i].X, r = queries[i].Y;
        while(ml > l) {
            ml--;
            add(ml);
        }
        while(mr < r) {
            mr++;
            add(mr);
        }
        while(ml < l) {
            remove(ml);
            ml++;
        }
        while(mr > r) {
            remove(mr);
            mr--;
        }
        ans[queries[i].pos] = cnt;
    }
    for(int i = 0; i < q; i++)
        printf("%d\n", ans[i]);
    return 0;
}

Compilation message

poklon.cpp: In function 'int main()':
poklon.cpp:42:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |     scanf("%d %d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~
poklon.cpp:45:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 |         scanf("%d", &a[i]);
      |         ~~~~~^~~~~~~~~~~~~
poklon.cpp:49:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |         scanf("%d %d", &l, &r);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 2 ms 204 KB Output is correct
3 Correct 5 ms 332 KB Output is correct
4 Correct 35 ms 460 KB Output is correct
5 Correct 2822 ms 3804 KB Output is correct
6 Correct 3771 ms 3820 KB Output is correct
7 Execution timed out 5095 ms 7468 KB Time limit exceeded
8 Execution timed out 5078 ms 12464 KB Time limit exceeded
9 Execution timed out 5093 ms 14896 KB Time limit exceeded
10 Execution timed out 5089 ms 18500 KB Time limit exceeded