Submission #989938

# Submission time Handle Problem Language Result Execution time Memory
989938 2024-05-29T06:24:07 Z Has2008 Poklon (COCI17_poklon) C++17
0 / 140
5000 ms 23956 KB
#include "bits/stdc++.h"

#define int long long
#define mod 1000000007

using namespace std;

const int maxn = 500005;
const int N = 1 << (int)ceil(log2(500005));

int n, q;
int arr[maxn], tree[N];

int query(int n, int ql, int qr, int l, int r)
{
    if(l >= ql && r <= qr) return tree[n];
    if(l > qr || r < ql) return 0;

    int md = (l + r) / 2;
    return query(n * 2, ql, qr, l, md) + query(n * 2 + 1, ql, qr, md + 1, r);
}

int update(int u, int v)
{
    u += N;
    tree[u] = v;

    u /= 2;
    while (u > 0)
    {
        tree[u] = tree[u * 2] + tree[u * 2 + 1];
        u /= 2;
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    cin >> n >> q;
    for(int i = 0; i < n; i++) cin >> arr[i];

    vector < pair < int, int >> v, o;
    while (q--)
    {
        int l, r;
        cin >> l >> r;

        v.push_back({r, l});
        o.push_back({l, r});
    }
    sort(v.begin(), v.end());

    map < int, vector < int >> mp;
    map < pair < int, int >, int > ans;

    int indx = 0;
    for(int i = 0; i < n; i++)
    {
        vector < int > vv = mp[arr[i]];

        if(vv.size() == 0) mp[arr[i]].push_back(i);
        else if(vv.size() == 1)
        {
            update(vv[0], 1);

            mp[arr[i]].push_back(i);
        }
        else if(vv.size() == 2)
        {
            update(vv[0], -1);
            update(vv[1], 1);

            mp[arr[i]].push_back(i);
        }
        else
        {
            update(vv[0], 0);
            update(vv[1], -1);
            update(vv[2], 1);

            mp[arr[i]].clear();
            mp[arr[i]].push_back(vv[1]);
            mp[arr[i]].push_back(vv[2]);
            mp[arr[i]].push_back(i);
        }
        while (indx != q && v[indx].first == i + 1)
        {
            ans[{v[indx].second, v[indx].first}] = query(1, v[indx].second - 1, v[indx].first - 1, 0, N - 1);
            indx++;
        }
    }
    for(int i = 0; i < o.size(); i++) cout << ans[{o[i].first, o[i].second}] << endl;
}

Compilation message

poklon.cpp: In function 'long long int update(long long int, long long int)':
poklon.cpp:34:1: warning: no return statement in function returning non-void [-Wreturn-type]
   34 | }
      | ^
poklon.cpp: In function 'int main()':
poklon.cpp:95:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |     for(int i = 0; i < o.size(); i++) cout << ans[{o[i].first, o[i].second}] << endl;
      |                    ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Execution timed out 5050 ms 4444 KB Time limit exceeded
2 Execution timed out 5061 ms 4444 KB Time limit exceeded
3 Execution timed out 5044 ms 4444 KB Time limit exceeded
4 Execution timed out 5057 ms 4700 KB Time limit exceeded
5 Execution timed out 5067 ms 9992 KB Time limit exceeded
6 Execution timed out 5039 ms 9908 KB Time limit exceeded
7 Execution timed out 5038 ms 12968 KB Time limit exceeded
8 Execution timed out 5019 ms 20908 KB Time limit exceeded
9 Execution timed out 5035 ms 22668 KB Time limit exceeded
10 Execution timed out 5074 ms 23956 KB Time limit exceeded