Advertisement
aayyk

Untitled

Nov 5th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <queue>
  7. #include <stack>
  8. #include <climits>
  9. #include <string>
  10. #include <set>
  11. #include <cmath>
  12. #include <map>
  13. #include <unordered_map>
  14. #include <numeric>
  15. #include <random>
  16. #include <memory>
  17. #include <chrono>
  18. #include <iterator>
  19. #include <functional>
  20. #include <unordered_set>
  21. #include <cassert>
  22. #ifdef LOCAL
  23. #include "debug.h"
  24. #else
  25. #define debug(x...)
  26. #endif
  27. //#pragma GCC optimize("Ofast")
  28. //#define int ll
  29.  
  30.  
  31.  
  32. using namespace std;
  33. typedef long long ll;
  34. typedef long double ld;
  35. typedef pair <int, int> pii;
  36. #define mp make_pair
  37. #define pb push_back
  38. #define sz(x) int((x).size())
  39.  
  40. #ifndef LOCAL
  41. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  42. #else
  43. mt19937 rng(228);
  44. #endif
  45.  
  46. const int N = 1e5 + 7;
  47. const int inf = INT_MAX / 2;
  48. const ll INF = LLONG_MAX / 3;
  49. const int MOD = 1e9 + 7;
  50. const double eps = 1e-6;
  51. const string cars[] = {"🚗", "🚕", "🚙"};
  52.  
  53. struct Tree {
  54.     private: int t[4 * N];
  55.     private: int n;
  56.  
  57.     public: Tree(int n) : n(n) {}
  58.  
  59.     private: void update(int v, int tl, int tr, int pos, int val) {
  60.         if (tl == tr) {
  61.             t[v] = val;
  62.         }
  63.         else {
  64.             int tm = (tl + tr) / 2;
  65.  
  66.             if (pos <= tm) {
  67.                 update(2 * v, tl, tm, pos, val);
  68.             }
  69.             else {
  70.                 update(2 * v + 1, tm + 1, tr, pos, val);
  71.             }
  72.  
  73.             t[v] = t[2 * v] + t[2 * v + 1];
  74.         }
  75.     }
  76.  
  77.     public: void set(int pos, int val) {
  78.         update(1, 0, n - 1, pos, val);
  79.     }
  80.  
  81.     private: int ans(int v, int tl, int tr, int l, int r) {
  82.         if (l > r) {
  83.             return 0;
  84.         }
  85.         if (tl == l && tr == r) {
  86.             return t[v];
  87.         }
  88.  
  89.         int tm = (tl + tr) / 2;
  90.         return ans(2 * v, tl, tm, l, min(tm, r)) + ans(2 * v + 1, tm + 1, tr, max(tm + 1, l), r);
  91.     }
  92.  
  93.     public: int query(int l, int r) {
  94.         return ans(1, 0, n - 1, l, r);
  95.     }
  96. };
  97.  
  98. signed main() {
  99. #ifdef LOCAL
  100.     freopen("input.txt", "r", stdin);
  101.     freopen("output.txt", "w", stdout);
  102. #endif
  103.     cout << fixed << setprecision(9);
  104.     ios::sync_with_stdio(false);
  105.     cin.tie();
  106.     cout.tie();
  107.  
  108.     int n;
  109.     cin >> n;
  110.  
  111.     vector < int > a(n);
  112.  
  113.     for (int& x : a) {
  114.         cin >> x;
  115.     }
  116.  
  117.     int q;
  118.     cin >> q;
  119.  
  120.     vector < tuple < int, int, int > > b(q);
  121.  
  122.     for (int i = 0; i < q; i++) {
  123.         auto& [x, y, id] = b[i];
  124.         cin >> x >> y;
  125.         x--, y--;
  126.         id = i;
  127.     }
  128.  
  129.     sort(b.begin(), b.end());
  130.     map < int, int > last;
  131.     int j = q - 1;
  132.     Tree tree(n);
  133.     vector < int > ans(q);
  134.  
  135.     for (int i = n - 1; i >= 0; i--) {
  136.         int t = last[a[i]];
  137.         if (t) {
  138.             tree.set(t, 0);
  139.         }
  140.        
  141.         tree.set(i, 1);
  142.         last[a[i]] = i;
  143.  
  144.         while (j >= 0 && get < 0 > (b[j]) >= i) {
  145.             ans[get < 2 > (b[j])] = tree.query(get < 0 > (b[j]), get < 1 > (b[j]));
  146.             j--;
  147.         }
  148.     }
  149.  
  150.     for (int x : ans) {
  151.         cout << x << "\n";
  152.     }
  153.  
  154.     return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement