CodeForces - 635D(线段树 点更新区间查询)

爱被打了一巴掌 2022-05-28 13:05 281阅读 0赞

D. Factory Repairs

time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A factory produces thimbles in bulk. Typically, it can produce up to a thimbles a day. However, some of the machinery is defective, so it can currently only produce b thimbles each day. The factory intends to choose a k-day period to do maintenance and construction; it cannot produce any thimbles during this time, but will be restored to its full production of a thimbles per day after the k days are complete.

Initially, no orders are pending. The factory receives updates of the form d**i, a**i, indicating that a**i new orders have been placed for the d**i-th day. Each order requires a single thimble to be produced on precisely the specified day. The factory may opt to fill as many or as few of the orders in a single batch as it likes.

As orders come in, the factory owner would like to know the maximum number of orders he will be able to fill if he starts repairs on a given day p**i. Help the owner answer his questions.

Input

The first line contains five integers n, k, a, b, and q (1 ≤ k ≤ n ≤ 200 000, 1 ≤ b < a ≤ 10 000, 1 ≤ q ≤ 200 000) — the number of days, the length of the repair time, the production rates of the factory, and the number of updates, respectively.

The next q lines contain the descriptions of the queries. Each query is of one of the following two forms:

  • 1 d**i a**i (1 ≤ d**i ≤ n, 1 ≤ a**i ≤ 10 000), representing an update of a**i orders on day d**i, or
  • 2 p**i (1 ≤ p**i ≤ n - k + 1), representing a question: at the moment, how many orders could be filled if the factory decided to commence repairs on day p**i?

It’s guaranteed that the input will contain at least one query of the second type.

Output

For each query of the second type, print a line containing a single integer — the maximum number of orders that the factory can fill over all n days.

Examples

input

Copy

  1. 5 2 2 1 8
  2. 1 1 2
  3. 1 5 3
  4. 1 2 1
  5. 2 2
  6. 1 4 2
  7. 1 3 2
  8. 2 1
  9. 2 3

output

  1. 3
  2. 6
  3. 4

input

Copy

  1. 5 4 10 1 6
  2. 1 1 5
  3. 1 5 5
  4. 1 3 2
  5. 1 5 2
  6. 2 1
  7. 2 2

output

  1. 7
  2. 1

Note

Consider the first sample.

We produce up to 1 thimble a day currently and will produce up to 2 thimbles a day after repairs. Repairs take 2 days.

For the first question, we are able to fill 1 order on day 1, no orders on days 2 and 3 since we are repairing, no orders on day 4 since no thimbles have been ordered for that day, and 2 orders for day 5 since we are limited to our production capacity, for a total of 3 orders filled.

For the third question, we are able to fill 1 order on day 1, 1 order on day 2, and 2 orders on day 5, for a total of 4 orders.

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int maxn=2e5+10;
  5. struct node{
  6. ll sa, sb;
  7. }tree[4*maxn];
  8. int n, k, a, b, q;
  9. ll _min(ll a, int b)
  10. {
  11. if(a>b) return b;
  12. else return a;
  13. }
  14. void build(int rt, int l, int r){
  15. if(l==r)
  16. {
  17. tree[rt].sa=0; tree[rt].sb=0;
  18. return;
  19. }
  20. int mid=(l+r)>>1;
  21. build(rt<<1, l, mid);
  22. build(rt<<1|1, mid+1, r);
  23. }
  24. void update(int rt, int l, int r, int day, int pr){
  25. if(day==l && r==day)
  26. {
  27. tree[rt].sa=_min(pr+tree[rt].sa, b);
  28. tree[rt].sb=_min(pr+tree[rt].sb, a);//取工作效率 和 工作量较小者
  29. return;
  30. }
  31. int mid=(l+r)>>1;
  32. if(mid>=day)
  33. update(rt<<1, l, mid, day, pr);
  34. else if(mid<day)
  35. update(rt<<1|1, mid+1, r, day, pr);
  36. tree[rt].sa=tree[rt<<1].sa+tree[rt<<1|1].sa;
  37. tree[rt].sb=tree[rt<<1].sb+tree[rt<<1|1].sb;
  38. return;
  39. }
  40. void query(int rt, int l, int r, int L, int R, ll &suma, ll &sumb){
  41. if(l==L && r==R)
  42. {
  43. suma+=tree[rt].sa;
  44. sumb+=tree[rt].sb;
  45. return;
  46. }
  47. int mid=(l+r)>>1;
  48. if(mid>=R)
  49. query(rt<<1, l, mid, L, R, suma, sumb);
  50. else if(mid<L)
  51. query(rt<<1|1, mid+1, r, L, R, suma, sumb);
  52. else
  53. {
  54. query(rt<<1, l, mid, L, mid, suma, sumb);
  55. query(rt<<1|1, mid+1, r, mid+1, R, suma, sumb);
  56. }
  57. }
  58. int main()
  59. {
  60. scanf("%d%d%d%d%d", &n, &k, &a, &b, &q);
  61. build(1, 1, n);
  62. while(q--){
  63. int op, w, e;
  64. scanf("%d", &op);
  65. if(op==1){
  66. scanf("%d%d", &w, &e);
  67. update(1, 1, n, w, e);
  68. }
  69. else{
  70. scanf("%d", &w);
  71. ll sum=0, s=0;
  72. int l1=1, r1, l2, r2=n;
  73. r1=w-1; l2=w+k;
  74. if(l1<=r1)
  75. query(1, 1, n, l1, r1, sum, s);
  76. if(l2<=r2)
  77. query(1, 1, n, l2, r2, s, sum);
  78. printf("%lld\n", sum);
  79. }
  80. }
  81. }

发表评论

表情:
评论列表 (有 0 条评论,281人围观)

还没有评论,来说两句吧...

相关阅读