[关闭]
@xunuo 2017-04-29T11:46:53.000000Z 字数 2548 阅读 972

Power oj 2459 Submissions of online judge---(树状数组)


Time Limit: 3000 MS Memory Limit: 65536 KB

树状数组 STL


Description

An online judge is a system to test programs in programming contests. The system can compile and execute codes, and test them with pre-constructed data. Submitted code may be run with restrictions, including time limit, memory limit, security restriction and so on. The output of the code will be captured by the system, and compared with the standard output. The system will then return the result. 
Always, Online Judge systems also have a status page where a list of these results is shown. For example, http://acm.swust.edu.cn/ is an Online Judge of SWUST. A following list can be found in the system. As it shows, every submission has a Run ID and User ID. The Run ID is an incrementing sequence from 1 to N, with which a User ID is associated.


Mr. Yang wants to know, from Run ID L to R, how many different Users there are?

Input

The first line is an integer T (1 ≤ T ≤ 10), which stands for the number of test cases you need to solve. For each case, the input format will be like this:
Line 1: N (1 ≤ N ≤ 100,000) indicating the number of submissions.
Line 2: N integers U1, U2…UN (0 ≤ Ui ≤ 1000,000,000). Ui indicates the User ID of the i-th (Run ID) submission.( For the sake of simplicity, User ID is digital)
Line 3: Q (1 ≤ Q ≤ 100,000), the number of Queries.
Next Q lines: each line contains 2 integers L, R representing a Query (1 ≤ L ≤ R ≤ N). Interval contains the endpoint

Output

For each Query, print the number of different Users from L to R in one line.

Sample Input

2
3
1 1 4
2
1 2
2 3
5
1 1 2 1 3
3
1 5
2 4
3 5

Sample Output

1
2
3
2
3

题意:

给你一串数字,问你在一个区间内有多少个不同的数字

解题思路

解题思路是个好东西~ 希望我也能有
     开n个vector来存询问区间的最左端和对应询问的次数(存询问次数是因为方便在最后的时候输出ans[i]);
     利用树状数组求和;
     将所求a[i]的前一个位置用map来存,(为什么不用数组呢?:是因为开不了那么大!!!哈哈哈哈哈哈哈!!!!zz)

完整代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N=100010;
  4. int n;
  5. int a[N];
  6. int c[N];///树状数组
  7. int ans[N];
  8. vector<pair<int,int> >v[N];///开n个vector来存区间起点li,因为是离线的,所以还要存对应的位置
  9. ///树状数组的一系列东西:
  10. int lowerbit(int x)
  11. {
  12. return x&-x;
  13. }
  14. void add(int i,int x)
  15. {
  16. while(i<=n)
  17. {
  18. c[i]+=x;
  19. i+=lowerbit(i);
  20. }
  21. }
  22. int SUM(int i)
  23. {
  24. int sum=0;
  25. while(i>0)
  26. {
  27. sum+=c[i];
  28. i-=lowerbit(i);
  29. }
  30. return sum;
  31. }
  32. int main()
  33. {
  34. int t;
  35. scanf("%d",&t);
  36. while(t--)
  37. {
  38. scanf("%d",&n);
  39. memset(c,0,sizeof(c));
  40. for(int i=0;i<=n;i++)
  41. v[i].clear();
  42. for(int i=1;i<=n;i++)
  43. scanf("%d",&a[i]);
  44. int q,l,r;
  45. scanf("%d",&q);
  46. for(int i=0;i<q;i++)
  47. {
  48. scanf("%d%d",&l,&r);
  49. v[r].push_back(make_pair(l,i));///v[r]
  50. }
  51. map<int,int>lastpose;///lastpose:指的是它的前一位置
  52. ///为什么要用map:因为数组装不下...太大了1e9!
  53. //unordered_map<int,int>lastpose;///其实还可以用unordered_map的,而且还要快一点,但是我的CB用不了!!!啊啊啊
  54. for(int i=1;i<=n;i++)
  55. {
  56. if(lastpose[a[i]]!=0)///map初始化的时候是为0的,只要它不为0,就减1;
  57. add(lastpose[a[i]],-1);
  58. lastpose[a[i]]=i;
  59. add(lastpose[a[i]],1);///+1;
  60. for(int j=0;j<v[i].size();j++)
  61. {
  62. int rr=i;
  63. int ll=v[i][j].first;
  64. int id=v[i][j].second;
  65. ans[id]=SUM(rr)-SUM(ll-1);///ans
  66. }
  67. }
  68. for(int i=0;i<q;i++)
  69. printf("%d\n",ans[i]);
  70. }
  71. return 0;
  72. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注