[关闭]
@994495jj 2017-05-31T11:48:14.000000Z 字数 2287 阅读 1340

Pacific Northwest Region Programming Contest Division 1

(ACM)数据结构----线段树



Problem J Shopping

题意

题解

代码

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <vector>
  5. #include <cstdio>
  6. #include <string>
  7. #include <cmath>
  8. #include <queue>
  9. #include <set>
  10. #include <map>
  11. using namespace std;
  12. typedef long long ll;
  13. typedef unsigned long long ull;
  14. typedef pair<int,int> pii;
  15. typedef pair<ll,int> pli;
  16. typedef pair<ll,ll> pll;
  17. #define mp make_pair
  18. #define pb push_back
  19. #define fi first
  20. #define se second
  21. #define lson l,m,rt<<1
  22. #define rson m+1,r,rt<<1|1
  23. #define de(x) cout << #x << "=" << x << endl
  24. const int N=200005;
  25. ll Min[N<<2];
  26. int id[N<<2];
  27. void UP(int rt) {
  28. Min[rt]=Min[rt<<1];
  29. id[rt]=id[rt<<1];
  30. if( Min[rt<<1|1] < Min[rt] ) {
  31. Min[rt]=Min[rt<<1|1];
  32. id[rt]=id[rt<<1|1];
  33. }
  34. }
  35. //存区间内最小的数和它的id
  36. void build(int l,int r,int rt) {
  37. if(l==r) {
  38. scanf("%I64d",Min+rt);
  39. id[rt]=l;
  40. return ;
  41. }
  42. int m=(l+r)>>1;
  43. build(lson);
  44. build(rson);
  45. UP(rt);
  46. }
  47. //返回区间小于v的最前面的,区间的数没有小于v返回-1
  48. pli qry(int L,int R,ll v,int l,int r,int rt) {
  49. pli tt=mp(-1,-1);
  50. if( R<l || r<L || l>r || L>R || Min[rt]>v ) return tt;
  51. if(l==r) return mp(Min[rt],id[rt]);
  52. int m=(l+r)>>1;
  53. pli t1=qry(L,R,v,lson);
  54. if(t1!=tt) return t1;
  55. pli t2=qry(L,R,v,rson);
  56. return t2;
  57. }
  58. int main() {
  59. ///
  60. int n,q;scanf("%d%d",&n,&q);
  61. ///build
  62. build(1,n,1);
  63. while(q--) {
  64. ll v;int l,r;
  65. scanf("%I64d%d%d",&v,&l,&r);
  66. while(1) {
  67. pli t=qry(l,r,v,1,n,1);
  68. if(t.fi==-1) break;
  69. v=v%t.fi;
  70. l=t.se+1;
  71. if(l>r) break;
  72. }
  73. printf("%I64d\n",v);
  74. }
  75. return 0;
  76. }

The sale bin of Big Box Bargains contains n products in a row. The ith item has price ai per unit.There is no limit to the quantity of any item.
There are q customers who will enter the store to buy items. The ith customer has vi dollars, starts at item li and walks to the right to item ri (inclusive), one item at a time.
Each time they encounter an item, they will buy as many units of the item as they can afford.
You are now wondering, for each customer, how much money they will have left after buying items.

Input
The first line of input contains two space-separated integers n and q (1 ≤ n, q ≤ 200,000).
The next line of input contains n space-separated integers ai (1 ≤ ai ≤ 1018).
Each of the next q lines contains three space-separated integers vi (1 ≤ vi ≤ 1018), li , and ri (1 ≤ li ≤ ri ≤ n).

Output
For each of the q customers, print, on a single line, a single integer indicating the remaining amount of money after shopping.

Sample Input
5 3
5 3 2 4 6
8 5 5
107 1 4
7 3 5

Sample Output
2
0
1

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注