B. 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 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 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:

  • di ai (1 ≤ di ≤ n, 1 ≤ 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.

Examples
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
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.

题意:一台机器,n天时间,维修得k天,正常状况下生产a件产品,非正常状况下生产b件产品,维修期间不生产产品,1下是在第d天生产ai件产品,2下是在第pi天开始维修,问在每个2下输出n天最多生产了多少件产品;

思路:线段树,维护两个和,sum[0]是这个区间内正常情况下最多生产的件数,sum[1]是在非正常情况下最多生产的件数,把(1,pi-1)的sum[0]+(pi+k,n)的sum[1]就是所要求的结果;

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int N=1e6+6;
int n,k,a,b,q;
struct nod
{
int l,r,len,sum[2];
};
nod tree[4*N];
void build(int node,int le,int ri)
{
tree[node].l=le;
tree[node].r=ri;
tree[node].len=ri-le+1;
for(int i=0;i<2;i++)
{
tree[node].sum[i]=0;
}
if(le==ri)return ;
else
{
int mid=(ri+le)>>1;
build(2*node,le,mid);
build(2*node+1,mid+1,ri);
}
}
void update(int node,int fx,int fy)
{
if(tree[node].l==tree[node].r&&tree[node].l==fx)
{
if(fy+tree[node].sum[0]>=a)
{
tree[node].sum[0]=a;
}
else tree[node].sum[0]+=fy;
if(fy+tree[node].sum[1]>=b)
{
tree[node].sum[1]=b;
}
else tree[node].sum[1]+=fy;
return ;
}
int mid=(tree[node].l+tree[node].r)>>1;
if(fx<=mid)update(2*node,fx,fy);
else update(2*node+1,fx,fy);
for(int i=0;i<2;i++)
{
tree[node].sum[i]=tree[2*node].sum[i]+tree[2*node+1].sum[i];
}
}
int query(int node,int L,int R,int le,int ri,int pos)
{
if(L>R)return 0;
if(L<=le&&R>=ri)
{
return tree[node].sum[pos];
}
int mid=(le+ri)>>1;
if(R<=mid) return query(2*node,L,R,le,mid,pos);
else if(L>mid)return query(2*node+1,L,R,mid+1,ri,pos);else{return query(2*node,L,R,le,mid,pos)+query(2*node+1,L,R,mid+1,ri,pos);}}int main(){int flag,x,y;
scanf("%d%d%d%d%d",&n,&k,&a,&b,&q);
build(1,1,n);for(int i=0;i<q;i++){
scanf("%d",&flag);if(flag==1){
scanf("%d%d",&x,&y);
update(1,x,y);}else{
scanf("%d",&x);
printf("%d\n",query(1,1,x-1,1,n,1)+query(1,x+k,n,1,n,0)); } }
return0;
}

codeforces 627B B. Factory Repairs(线段树)的更多相关文章

  1. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  2. B. Factory Repairs--cf627B(线段树)

    http://codeforces.com/problemset/problem/627/B 题目大意:  n代表天数 ,k代表每一次维修持续的天数,a代表维修过后每天能生产a件产品,b代表维修之前每 ...

  3. codeforces 22E XOR on Segment 线段树

    题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...

  4. Codeforces 588E. A Simple Task (线段树+计数排序思想)

    题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...

  5. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

  6. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  7. Codeforces 444C DZY Loves Colors(线段树)

    题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...

  8. Codeforces 85D Sum of Medians(线段树)

    题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...

  9. [Codeforces]817F. MEX Queries 离散化+线段树维护

    [Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...

随机推荐

  1. Linux2_小技巧

    0 鼠标不灵么: 左侧设置图标----显示----未知显示屏--关闭 1 左侧自动隐藏 右键---更改桌面背景---行为--隐藏 2 终端打开 搜索到终端添加到左侧 ctrl+alt+T快捷打开 ct ...

  2. java 中 集合类相关问题

    1,Java中Collection和Collections的差别 java.util.Collection 是一个集合接口.它提供了对集合对象进行基本操作的通用接口方法. Collection接口在J ...

  3. 【BZOJ4128】Matrix BSGS+hash

    [BZOJ4128]Matrix Description 给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p) Input 第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * ...

  4. Labview新建项目步骤

    打开Labview软件,点击工具栏中文件选项卡,如图所示. 2 点击新建一个空白项目. 3 此时为未命名项目,按下Ctrl+S保存项目到自己指定的目录并完成命名. 4 如图示在我的电脑上点击右键,新建 ...

  5. PLSQL使用技巧----加快你的编程效率

    使用PLSQL 编程效率明显有所提高了 1.登录后默认自动选中My Objects      默认情况下,PLSQL Developer登录后,Brower里会选择All objects,如果你登录的 ...

  6. sql 语句实现分页查询

    SELECT * FROM ( SELECT A.*, ROWNUM RN FROM (SELECT * FROM TABLE_NAME) A WHERE ROWNUM <= 40 ) WHER ...

  7. centos7 PXE自动安装环境搭建

    原理: 要进行自动安装的主机A,加电启动时以网卡为第一启动设备 1.启动时会向网络广播,找到dhcp服务器B请求分配IP地址信息,服务器B除了给其分配基本的IP信息(ip.netmask.getewa ...

  8. SQLServer将一个表内指定列的所有值插入另一个表

    insert into records_resolve_bak(resolve_save_addr,resolve_time,resolve_status) select  resolve_save_ ...

  9. 基于SQLAIchemy的Flask目录

    预先知识 flask的基本使用 快速搭建开发的目录,以后我们在用Flask开发项目的时候可以直接用这个目录,不需要再自己创建. flask-sqlalchemy flask-sqlalchemy相当于 ...

  10. table control里面各种属性和事件

    [转自]http://blog.csdn.net/hackai886/article/details/7935366 SAP中,Table Control是在Screen中用的最广泛的控件之一了,可以 ...