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 di, ai, indicating that ai new orders have been placed for the di-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 pi. Help the owner answer his questions.
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 di ai (1 ≤ di ≤ n, 1 ≤ ai ≤ 10 000), representing an update of ai orders on day di, or
- 2 pi (1 ≤ pi ≤ n - k + 1), representing a question: at the moment, how many orders could be filled if the factory decided to commence repairs on day pi?
It's guaranteed that the input will contain at least one query of the second type.
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.
5 2 2 1 8 1 1 2 1 5 3 1 2 1 2 2 1 4 2 1 3 2 2 1 2 3
3 6 4
5 4 10 1 6 1 1 5 1 5 5 1 3 2 1 5 2 2 1 2 2
7 1
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.
题目大意:
一共有N天,如果要修理机器需要连续的K天,如果机器修好了可以在一天内完成a个订单,如果没修好可以完成b个订单,一共有q个讯息。
对应q个讯息:
①1 x y 表示第x天,有y个订单需要完成,而且这y个订单可以完成部分订单。而且这份订单只能在这一天进行(不能提前预知这一天要来订单,而且也不能在第二天继续这份订单)。
②2 x,表示我们在第x天开始修理机器,并且问之前的订单最多可以完成多少份。
样例的具体信息在Note中已经给出。
思路:
1、对应每个查询,其实就是分成两段的查询。一段是没有修理机器的时候能够完成的订单查询,一段是修理完机器之后能够完成的订单查询。
所以我们不妨建立两棵树,每一段查询我们都可以通过树状数组的区间查询来完成。
2、那么问题在于如何维护这棵树。
①对于没有修理完机器的时间段来讲,如果订单数多于b个,那么这一天只能完成b个订单,如果订单数小于b个,,那么这一天就能全部完成任务。
所以我们只要维护树状数组中每天的那一块的数据小于等于b即可。如果这一天的订单总数大于了b,那么我们一直维护这一天的订单总数为b个就行了,无需再向上添加订单,因为再添加我们也完成不了。
②同理对于修理完的机器时间段来讲,我们只要维护树状数组中每天的那一块的数据小于等于a即可
3、完成了建树维护和查询之后,控制好细节就没有别的神马了。
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
int tree[3000005];//树
int tree2[3000005];
int n,k,a,b,q;
int lowbit(int x)//lowbit
{
return x&(-x);
}
int sum2(int x)
{
int sum=0;
while(x>0)
{
sum+=tree2[x];
x-=lowbit(x);
}
return sum;
}
void add2(int x,int c)//加数据。
{
while(x<=n)
{
tree2[x]+=c;
x+=lowbit(x);
}
}
int sum(int x)
{
int sum=0;
while(x>0)
{
sum+=tree[x];
x-=lowbit(x);
}
return sum;
}
void add(int x,int c)//加数据。
{
while(x<=n)
{
tree[x]+=c;
x+=lowbit(x);
}
}
int main()
{
while(~scanf("%d%d%d%d%d",&n,&k,&a,&b,&q))
{
memset(tree,0,sizeof(tree));
memset(tree2,0,sizeof(tree2));
while(q--)
{
int op;
scanf("%d",&op);
if(op==1)
{
int pos,val,ad;
scanf("%d%d",&pos,&val);
int posval=sum(pos)-sum(pos-1);
if(posval+val>b)ad=b-posval;
else ad=val;
add(pos,ad);
posval=sum2(pos)-sum2(pos-1);
if(posval+val>a)ad=a-posval;
else ad=val;
add2(pos,ad);
}
else
{
int pos;
scanf("%d",&pos);
int output=0;
output+=sum(pos-1);
pos+=k-1;
output+=sum2(n)-sum2(pos);
printf("%d\n",output);
}
}
}
}