一个利用pbds的玄学做法
查看原帖
一个利用pbds的玄学做法
52136
Yobi_For_516楼主2022/7/16 00:44
#include <bits/stdc++.h>
#include <bits/extc++.h>
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    int n, q;
    std::cin >> n;
    std::vector<int> a(n);
    std::vector<int> lastfind(1000001, -1);
    for (auto &i : a)
        std::cin >> i;
    std::cin >> q;
    int j = 0;
    std::vector<std::pair<std::pair<int, int>, std::pair<int, int>>> que(q);
    for (auto &i : que)
    {
        std::cin >> i.first.second >> i.first.first;
        i.second.first = j++;
    }
    std::sort(que.begin(), que.end());
    __gnu_pbds::tree<int, __gnu_pbds::null_type, std::greater<int>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> tr;
    j = 0;
    for (auto &i : que)
    {
        while (j < i.first.first)
        {
            if (lastfind[a[j]] != -1)
                tr.erase(lastfind[a[j]]);
            lastfind[a[j]] = j;
            tr.insert(j);
            j++;
        }

        i.second.second = tr.order_of_key(i.first.second - 2);
    }
    sort(que.begin(), que.end(), [](auto a, auto b)
         { return a.second.first < b.second.first; });
    for (auto &i : que)
        std::cout << i.second.second << "\n";
}

我们可以离线化查询,利用动态的集合的rank问题去解决这道题

2022/7/16 00:44
加载中...