Submission #570728

# Submission time Handle Problem Language Result Execution time Memory
570728 2022-05-31T07:35:32 Z YouAreMyGalaxy Poklon (COCI17_poklon) C++17
140 / 140
597 ms 37376 KB
//Make CSP great again
//You're as beautiful as the day I lost you
#include <bits/stdc++.h>
#define TASK "TESTCODE"
#define Log2(x) 31 - __builtin_clz(x)
using namespace std;
const int N = 5e5;
int n, q, a[N + 1], res[N + 1];
struct FenwickTree
{
    int Tree[N + 1];
    void add(int pos, int val)
    {
        if (pos == 0)
        {
            return ;
        }
        for (; pos <= n; pos += pos & (-pos))
        {
            Tree[pos] += val;
        }
    }
    int sum(int pos)
    {
        int ret = 0;
        for (; pos > 0; pos -= pos & (-pos))
        {
            ret += Tree[pos];
        }
        return ret;
    }
    int sum(int l, int r)
    {
        return sum(r) - sum(l - 1);
    }
} Tree;
vector<pair<int, int> > Query[N + 1];
map<int, int> Map[3];
void read()
{
    cin >> n >> q;
    for (int i = 1; i <= n; ++ i)
    {
        cin >> a[i];
    }
    for (int i = 1; i <= q; ++ i)
    {
        int l, r;
        cin >> l >> r;
        Query[r].push_back(make_pair(i, l));
    }
}
void solve()
{
    for (int i = 1; i <= n; ++ i)
    {
        Tree.add(Map[2][a[i]], 1);
        Map[2][a[i]] = Map[1][a[i]];
        Tree.add(Map[2][a[i]], -2);
        Map[1][a[i]] = Map[0][a[i]];
        Tree.add(Map[1][a[i]], 1);
        Map[0][a[i]] = i;
        for (auto x : Query[i])
        {
            res[x.first] = Tree.sum(x.second, i);
        }
    }
    for (int i = 1; i <= q; ++ i)
    {
        cout << res[i] << '\n';
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        //freopen(TASK".OUT", "w", stdout);
    }
    int t = 1;
    bool typetest = false;
    if (typetest)
    {
        cin >> t;
    }
    for (int __ = 1; __ <= t; ++ __)
    {
        //cout << "Case " << __ << ": ";
        read();
        solve();
    }
}

Compilation message

poklon.cpp: In function 'int main()':
poklon.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Correct 7 ms 12116 KB Output is correct
3 Correct 8 ms 12044 KB Output is correct
4 Correct 12 ms 12244 KB Output is correct
5 Correct 72 ms 16820 KB Output is correct
6 Correct 95 ms 16816 KB Output is correct
7 Correct 220 ms 22000 KB Output is correct
8 Correct 339 ms 27344 KB Output is correct
9 Correct 473 ms 32308 KB Output is correct
10 Correct 597 ms 37376 KB Output is correct