Recently oaiei has encountered a problem. And he needs your help.
There are N numbers in an array and Q queries. For each query, there are two integers S and T. And the proplem is that there are how many different numbers in the index range [S,T] of the array (the array ‘s index is started from 1).
Input
There are multiple test cases. For each test case, the first line is an integer N (1<=N<=100000), indicating there are N numbers in the array. In the second line,there are N integers which are separated by a space. The third line is an integer Q(1<=Q<=1000), indicating there are Q queries. The following Q lines, for each line, there are two integers S and T(1<=S<=T<=N), indicating the index range of the array. The element value of the array will fit in a signed 32-bit integer.
Output
For each test, output an integer, indicating the number of different numbers in the index range [S,T] of the array.
Sample Input
10
1 2 3 3 3 3 4 -9 -9 10
2
1 5
5 10
Sample output
3
4
有关结构体和排序的问题,定义一个结构体,用来存储输入的值和它所对应的位置。 然后对里面的值从小到大或从大到小进行排序,排序以后原来的数和它们所对应的位置并没有改变, 那么排序了每个数又会有新的位置,这时候的位置除去了重复的值,也就是说值一样的数他们所对 应的位置是一样的,可以用一个数组下标来存储,c[a.num]=t;,a.num 是原来的位置,c[a.num]是现在 的位置,这样就总共有t+1个不相同的数,再定义一个数组b[t+1]={0},如果要求原来的数列中第j-至 j+i个数有多少个不重复的数,就可以用b[c[j]]来求,因为c[j]是排列以后的位置,相同的数他们的位置 都是相同的,所以当这个位置的数数访问时b[c[j]]就标记为1。 此外这里面还用到了调用头文件的排序方法,头文件#include<algorithm>里面的sort(a,a+n,down/up)函数, 其中down/up()是一个函数,这个函数用来决定是升还是降, bool down(int x,int y) {return a>b;}(降序) bool up(int x,int y) {return a<b;}(升序)
|