VandanRogheliya

Untitled

Jul 15th, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.90 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define FOR(i, j, k, in) for (int i=j ; i<k ; i+=in)
  5. #define RFOR(i, j, k, in) for (int i=j ; i>=k ; i-=in)
  6. #define FO(i, j) FOR(i, 0, j, 1)
  7. #define RFO(i, j) RFOR(i, j, 0, 1)
  8. #define all(cont) cont.begin(), cont.end()
  9. #define rall(cont) cont.end(), cont.begin()
  10. #define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
  11. #define mp make_pair
  12. #define pb push_back
  13. #define setbits(x) __builtin_popcountll(x)
  14. #define zrobits(x) __builtin_ctzll(x)
  15. #define ps(x,y) fixed<<setprecision(y)<<x //cout << ps(ans, decimal places);
  16. #define INF (int)1e9
  17. #define EPS 1e-9
  18. #define PI 3.1415926535897932384626433832795
  19. typedef pair<int, int> pi;
  20. typedef pair<long long, long long> pll;
  21. typedef vector<int> vi;
  22. typedef vector<long long> vl;
  23. typedef vector<string> vs;
  24. typedef vector<pi> vpii;
  25. typedef vector<vi> vvi;
  26. typedef vector<vl> vvl;
  27. typedef map<int,int> mi;
  28. typedef map<long long,long long> ml;
  29. typedef set<int> si;
  30. typedef set<long long> sl;
  31. typedef long long int ll;
  32. typedef unsigned long long int  ull;
  33. typedef priority_queue<ll> mxhpl;
  34. typedef priority_queue<int> mxhpi;
  35. typedef priority_queue<ll, vector<ll>, greater<ll>> mnhpl;
  36. typedef priority_queue<int, vector<int>, greater<int>> mnhpi;
  37.  
  38. void IO() {
  39.   ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  40. #ifndef ONLINE_JUDGE
  41.   freopen("input.txt", "r", stdin);
  42.   // freopen("output.txt", "w", stdout);
  43. #endif
  44. }
  45.  
  46. template<typename... T>
  47. void read(T&... args) {
  48.   ((cin >> args), ...);
  49. }
  50.  
  51. template<typename... T>
  52. void write(T&&... args) {
  53.   ((cout << args << " "), ...);
  54. }
  55.  
  56. #define MOD 1000000007
  57. int mpow(int, int);
  58. /*******************************************************/
  59.  
  60. const int I = 0, Q = 1;
  61.  
  62. class Info {
  63. public:
  64.   int type, l, r, i, val, end;
  65.   Info() {
  66.     type = l = r = i = val = end = -1;
  67.   }
  68. };
  69.  
  70. int n, m,sgt[70000] = {0};
  71. //  a[30001],
  72. vector<Info> qs;
  73. unordered_map<int,int> occ;
  74.  
  75. int comp_ends(const Info& a, const Info& b) {
  76.   if (a.end < b.end) return true;
  77.   else if (a.end == b.end) return a.type == I;
  78.   else return false;
  79. }
  80.  
  81. void insert(int i, int val, int l, int r, int pos) {
  82.   if (l == r && l == i) {
  83.     sgt[pos] = val;
  84.     return;
  85.   }
  86.   else if (l == r || l > i || i > r) return;
  87.   int mid = (l+r)/2;
  88.   if (mid < i) insert(i,val,mid+1,r,pos*2+2);
  89.   else insert(i,val,l,mid,pos*2+1);
  90.  
  91.   sgt[pos] = sgt[pos*2+2]+sgt[pos*2+1];
  92. }
  93.  
  94. int query(int low, int high, int l, int r, int pos) {
  95.   if (low <= l && high >= r) return sgt[pos];
  96.   else if (low > r || high < l) return 0;
  97.  
  98.   int mid = (r+l)/2;
  99.   int ans = query(low,high,l,mid,pos*2+1);
  100.   ans += query(low,high,mid+1,r,pos*2+2);
  101.  
  102.   return ans;
  103. }
  104.  
  105. void solve() {
  106.   read(n);
  107.   FO(i,n) {
  108.     Info tmp;
  109.     tmp.type = I;
  110.     read(tmp.val);
  111.     tmp.i = i;
  112.     tmp.end = i;
  113.     qs.pb(tmp);
  114.   }
  115.  
  116.   read(m);
  117.   FO(i, m) {
  118.     Info tmp;
  119.     tmp.type = Q;
  120.     read(tmp.l, tmp.r);
  121.     tmp.l--;
  122.     tmp.r--;
  123.     tmp.i = i;
  124.     tmp.end = tmp.r;
  125.     qs.pb(tmp);
  126.   }
  127.  
  128.   sort((qs).begin(), (qs).end(), comp_ends);
  129.  
  130.   int ans[m];
  131.  
  132.   for(auto x: qs) {
  133.     // write(x.type, x.r, x.i, "\n");
  134.     if (x.type) {
  135.       ans[x.i] = query(x.l,x.r,0,n-1,0);
  136.  
  137.     } else {
  138.       if (occ.find(x.val) != occ.end()){
  139.         insert(x.i,1,0,n-1,0);
  140.         insert(occ[x.val],0,0,n-1,0);
  141.         occ[x.val] = x.i;
  142.  
  143.       } else {
  144.         insert(x.i,1,0,n-1,0);
  145.         occ[x.val] = x.i;
  146.  
  147.       }
  148.     }
  149.   }
  150.  
  151.   FO(i,m) cout << ans[i] << "\n";
  152.   // FO(i,m+n) write(qs[i].type?qs[i].r:qs[i].i);
  153. }
  154.  
  155. int main() {
  156.   IO();
  157.  
  158.   int t = 1;
  159.   // cin >> t;
  160.   while(t--) {
  161.     solve();
  162.   }
  163.  
  164.   return 0;
  165. }
  166.  
  167. /*******************************************************/
  168. int mpow(int base, int exp) {
  169.   base %= MOD;
  170.   int result = 1;
  171.   while (exp > 0) {
  172.     if (exp & 1) result = ((ll)result * base) % MOD;
  173.     base = ((ll)base * base) % MOD;
  174.     exp >>= 1;
  175.   }
  176.   return result;
  177. }
Add Comment
Please, Sign In to add comment