Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: 1. She will tell you two numbers, l and r (1?≤?l?≤?r?≤?n), and you should tell her . 2. Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and r (1?≤?l?≤?r?≤?n), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. Input The first line contains an integer n (1?≤?n?≤?105). The second line contains n integers: v1,?v2,?...,?vn (1?≤?vi?≤?109) — costs of the stones. The third line contains an integer m (1?≤?m?≤?105) — the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and r (1?≤?l?≤?r?≤?n; 1?≤?type?≤?2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. Output Print m lines. Each line must contain an integer — the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. Sample test(s) input 6 6 4 2 7 2 7 3 2 3 6 1 3 4 1 1 6 output 24 9 28 input 4 5 5 2 3 10 1 2 4 2 1 4 1 1 1 2 1 4 2 1 2 1 1 1 1 3 3 1 1 3 1 4 4 1 2 2 output 10 15 5 15 5 5 2 12 3 5 Note Please note that the answers to the questions may overflow 32-bit integer type. [cpp] view plaincopy 01.#include 02.#include 03.#include 04.using namespace std; 05.#define ll 100000 06.#define lson 2*i 07.#define rson 2*i+1 08.#define lc l,mid,2*i 09.#define rc mid+1,r,2*i+1 10.#define LL __int64 11. 12.LL sum,n,maxn; 13. 14.LL s[ll],s1[ll]; 15. 16.struct node 17.{ 18. LL l,r,n; 19. LL sum; 20.} a[2][ll<<2]; 21. 22.void init(LL l,LL r,LL i,LL tr) 23.{ 24. a[tr][i].l = l; 25. a[tr][i].r = r; 26. a[tr][i].n = 0; 27. a[tr][i].sum = 0; 28. if(l!=r) 29. { 30. LL mid = (l+r)/2; 31. init(lc,tr); 32. init(rc,tr); 33. } 34.} 35. 36.void insert(LL i,LL x,LL m,LL tr) 37.{ 38. if(x>=a[tr][i].l && x<=a[tr][i].r) 39. { 40. a[tr][i].n=m; 41. a[tr][i].sum=m; 42. } 43. if(a[tr][i].l == a[tr][i].r) 44. return ; 45. LL mid = (a[tr][i].l+a[tr][i].r)/2; 46. if(x>mid) 47. insert(2*i+1,x,m,tr); 48. else 49. insert(2*i,x,m,tr); 50. a[tr][i].sum = a[tr][2*i].sum+a[tr][2*i+1].sum; 51. a[tr][i].n = max(a[tr][2*i].n,a[tr][2*i+1].n); 52.} 53. 54. 55.LL find_sum(LL x,LL y,LL i,LL tr) 56.{ 57. if(a[tr][i].l == x && a[tr][i].r == y) 58. return a[tr][i].sum; 59. LL mid = (a[tr][i].l+a[tr][i].r)/2; 60. if(x>mid) 61. return find_sum(x,y,2*i+1,tr); 62. else if(y<=mid) 63. return find_sum(x,y,2*i,tr); 64. else 65. return find_sum(x,mid,2*i,tr)+find_sum(mid+1,y,2*i+1,tr); 66.} 67. 68.int main() 69.{ 70. LL n,m,i,j,b,x,y,cas; 71. scanf("%I64d",&n); 72. init(1,n,1,0); 73. init(1,n,1,1); 74. for(i = 1; i<=n; i++) 75. { 76. scanf("%I64d",&s[i]); 77. insert(1,i,s[i],0); 78. } 79. sort(s+1,s+n+1); 80. for(i = 1; i<=n; i++) 81. { 82. insert(1,i,s[i],1); 83. } 84. scanf("%I64d",&m); 85. while(m--) 86. { 87. scanf("%I64d%I64d%I64d",&cas,&x,&y); 88. printf("%I64d\n",find_sum(x,y,1,cas-1)); 89. 90. } 91. 92. return 0; 93.} AAA0010359
ekewcb CF#248DIV2:B. Kuriyama Mirai's Stones(线段树)
最新推荐文章于 2016-04-25 18:12:27 发布