Describe
A sequence contains n (1<n<=100000) elements, which may have the same number of M (1<=m<=100000) queries on this sequence, each of which contains an ordinal x, and answer the sum of the elements in the series that are less than the X element.
Input
The first line contains a number t representing the total number of test samples each set of samples contains 4 rows The first row contains an integer n that represents the length of the sequence the second line contains n less than 1000 positive integers separated by a space the third line contains an integer m for the number of queries the fourth row contains m of 1 to N of the integer representing the query number
Output
Each set of outputs contains a m-separated number of integers to buy the results of M-Times inquiry
Example Input
24255 139 58 41251 1 2 3 35427 563 289 203 32643 1 4 4
Example Output
197 197 58 0 0203 818 0 0
Analysis: This problem is not difficult, mainly there is easy to consider where, I am through the structure to hold the number and number, and then the value of the size of the order, and then calculate the value of each number of the results stored in the RES array, error-prone is not considering the number of possible repetition of the casesuch as this set of data5 the51 2 3 4Output0 0 0If you consider this set of data it should be a big problem.
put your own AC code.
#include <stdio.h> #include <algorithm>using std::sort;//define global Variables const int M=100000+5;STRUCT data{int N;int Num;} A[m];int B[m];int res[m];//is prioritized according to the size of the numeric value, if the number is equal then the ascending bool CMP (data X,data y) {if (X.N<Y.N) {return true;} else if (X.N>Y.N) {return false;} Else{return X.num<y.num;}} int main () {int t;scanf ("%d", &t), while (t--) {int n,cnt=1;scanf ("%d", &n), for (int i=0; i<n; ++i) {scanf ("%d", &A[I].N); a[i].num=i+1;} int m;scanf ("%d", &m);//Enter the number of the query for (int i=0; i<m; ++i) {scanf ("%d", &b[i]);} Sort the data in array a sort (a,a+n,cmp);//statistics are less than the sum of the corresponding values for each ordinal res[a[0].num]=0;for (int i=1; i<n; ++i) {if (A[I].N!=A[I-1].N) {res[ A[i].num]=res[a[i-1].num]+a[i-1].n;int temp=i-1;//If the front is all the same element while (TEMP>0&&A[I-1].N==A[TEMP-1].N) {res [A[i].num]+=a[temp-1].n;--temp;}} else{//the same as the previous element Res[a[i].num]=res[a[i-1].num];}} The output is for (int i=0; i<m; ++i) {printf ("%d", Res[b[i]]),//Output space, and the last number outputs a newline if (i!=m-1) {printf ("");} else{printf ("\ n");}}}
Soj--4393:laob ' s problem1