CF 248B 前缀和

操作1 l r 是原序列l-r的和
操作2 l r 是从小到大排序后的l-r的和


input
6
6 4 2 7 2 7
3
2 3 6
1 3 4
1 1 6
output
24
9
28

 

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <string>
 6 # include <cmath>
 7 # include <queue>
 8 # include <list>
 9 # define LL long long
10 using namespace std ;
11 
12 LL a[100010] ;
13 LL b[100010] ;
14 
15 LL a_sum[100010] ;
16 LL b_sum[100010] ;
17 
18 int main()
19 {
20     //freopen("in.txt","r",stdin) ;
21     int n , m ;
22     while(scanf("%d" , &n) != EOF)
23     {
24         int i ;
25         for (i = 0 ; i < n ; i++)
26         {
27             scanf("%I64d" , &a[i]) ;
28             b[i] = a[i] ;
29         }
30         sort(b , b+n) ;
31         a_sum[0] = b_sum[0] ;
32         for (i = 1 ; i <= n ; i++)
33         {
34             a_sum[i] = a_sum[i-1] + a[i-1] ;
35             b_sum[i] = b_sum[i-1] + b[i-1] ;
36         }
37         scanf("%d" , &m) ;
38         while(m--)
39         {
40             int op , l , r ;
41             scanf("%d%d%d" , &op , &l , &r) ;
42             if (op == 1)
43                 printf("%I64d\n" , a_sum[r]-a_sum[l-1]) ;
44             else
45                 printf("%I64d\n" , b_sum[r]-b_sum[l-1]) ;
46         }
47     }
48     return 0;
49 }
View Code

 

posted @ 2015-10-04 23:19  __Meng  阅读(176)  评论(0编辑  收藏  举报