CodeForces 635D Factory Repairs(树状数组)

题意:有 n天,每天普通情况下可以产生b个东西,维修完机器的情况下可以产生a个东西, 由普通的情况到好的情况需要k天的休息,就这k天啥也干不了,现在有 q次询问 现在你有两个操作

          1 x y,表示订单第x天需要y个东西

          2 x 在第x天进行休息,然后问你最多能够满足多少个东西的需求

思路:直接树状数组维护一下,k天以前每个点的最大值为b,k天以后每个点的最大值为a,记录区间和


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 200000+100
#define LL long long
int cas=1,T;
int n,k,a,b,q;
int c[maxn][2];
int A[maxn];
int lowbit(int x)
{
	return x&(-x);
}
void update(int x,int y,int z)
{
	for (int i = x;i<maxn;i+=lowbit(i))
		c[i][z]+=y;
}
int getsum(int x,int z)
{
	int ans = 0;
	for (int i = x;i;i-=lowbit(i))
		ans+=c[i][z];
	return ans;
}
int main()
{
	while (scanf("%d%d%d%d%d",&n,&k,&a,&b,&q)!=EOF)
	{
		for (int i = 1;i<=q;i++)
		{
			int op;
			scanf("%d",&op);
			if (op==1)
			{
				int x,y;
				scanf("%d%d",&x,&y);
				int temp = A[x];
				A[x]+=y;
				update(x,min(b,A[x])-min(b,temp),0);
				update(x,min(a,A[x])-min(a,temp),1);
			}
			else
			{
				int x;
				scanf("%d",&x);
				printf("%d\n",getsum(x-1,0)+getsum(n,1)-getsum(x+k-1,1));
			}
		}
	}
	//freopen("in","r",stdin);
	//scanf("%d",&T);
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}

Description

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 diai, indicating that ai new orders have been placed for thedi-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.

Input

The first line contains five integers nkab, and q (1 ≤ k ≤ n ≤ 200 0001 ≤ b < a ≤ 10 0001 ≤ 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:

  • diai (1 ≤ di ≤ n1 ≤ ai ≤ 10 000), representing an update of ai orders on day di, or
  • 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.

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.

Sample Input

Input
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
Output
3
6
4
Input
5 4 10 1 6
1 1 5
1 5 5
1 3 2
1 5 2
2 1
2 2
Output
7
1



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值