【树状数组】P1972 [SDOI2009]HH的项链

断了今生、忘了曾经 提交于 2019-12-01 04:30:17
  1 #include<iostream>
  2 #include<string>
  3 #include<queue>
  4 #include<stack>
  5 #include<vector>
  6 #include<map>
  7 #include<cstdio>
  8 #include<cstdlib>
  9 #include<algorithm>
 10 #include<set>
 11 #include<iomanip>
 12 #include<cstring>
 13 #include<cmath>
 14 #include<limits>
 15 using namespace std;
 16 
 17 #define au auto
 18 #define debug(i) cout<<"debug: "<<i<<endl
 19 #define mfor(i,a,b) for(register int i=(a);i<=(b);i++)
 20 #define mrep(i,a,b) for(register int i=(a);i>=(b);i--)
 21 #define LLL __int128
 22 #define Re register
 23 #define il inline
 24 #define mem(a,b) memset(a,(b),sizeof(a))
 25 typedef pair<int, int> intpair;
 26 typedef long long int LL;
 27 const int INF = 0x3f3f3f3f;
 28 const int INFLL = 0x3f3f3f3f3f3f3f3f;
 29 
 30 int n;
 31 const int maxn = 1000010;
 32 int a[maxn];
 33 int tr[maxn];
 34 
 35 inline int lowbit(int x)
 36 {
 37     return x & (-x);
 38 }
 39 
 40 inline void updata(int x, int k)
 41 {
 42     while (x <= n)
 43     {
 44         tr[x] += k;
 45         x += lowbit(x);
 46     }
 47 }
 48 
 49 inline int get_sum(int x)
 50 {
 51     int res = 0;
 52     while (x > 0)
 53     {
 54         res += tr[x];
 55         x -= lowbit(x);
 56     }
 57     return res;
 58 }
 59 
 60 struct Query
 61 {
 62     int l, r;
 63     int i;
 64     bool operator<(Query s)
 65     {
 66         return r < s.r;
 67     }
 68 }q[maxn];
 69 
 70 int m;
 71 int mark[maxn];
 72 int ans[maxn];
 73 
 74 int main()
 75 {
 76     ios::sync_with_stdio(0);
 77     cin.tie(0);
 78     cout.tie(0);
 79     cin >> n;
 80     mfor(i, 1, n) cin >> a[i];
 81     cin >> m;
 82     mfor(i, 1, m)
 83     {
 84         cin >> q[i].l >> q[i].r;
 85         q[i].i = i;
 86     }
 87     sort(q + 1, q + 1 + m);
 88     int nxt = 1;
 89     mfor(i, 1, m)
 90     {
 91         mfor(j, nxt, q[i].r)
 92         {
 93             if (mark[a[j]]) updata(mark[a[j]], -1);
 94             updata(j, 1);
 95             mark[a[j]] = j;
 96         }
 97         nxt = q[i].r + 1;
 98         ans[q[i].i] = get_sum(q[i].r) - get_sum(q[i].l - 1);
 99     }
100     mfor(i, 1, m) cout << ans[i] << endl;
101     return 0;
102 }
View Code

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!