Codeforces 627B Factory Repairs 【线段树】

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

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:

  • di ai (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 ndays.

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 11 order on day 2, and 2 orders on day 5, for a total of 4 orders.



题意:有n个机器,机器非正常时每天生产b个物品,正常时每天生产a个物品,维修机器需要花费连续的k天(这k天什么都不能做)。有q次操作

1 a d,表示在d天有a个要求(每个要求需要一个物品)

2 p,表示从p天开始维修。(每一次维修是独立的)

机器初始是非正常的。

对每一个2操作,输出最多可以满足的要求数。


思路:就是一个线段树,维护三个和就可以了。


AC代码:


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <string>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
const int MAXN = 2*1e5+10;
const int INF = 0x3f3f3f3f;
struct Tree{
    int l, r, len, sum[3];
};
Tree tree[MAXN<<2];
void PushUp(int o) {
    for(int i = 0; i < 3; i++) {
        tree[o].sum[i] = tree[ll].sum[i] + tree[rr].sum[i];
    }
}
void Build(int o, int l, int r)
{
    tree[o].l = l; tree[o].r = r;
    tree[o].len = r - l + 1;
    for(int i = 0; i < 3; i++) {
        tree[o].sum[i] = 0;
    }
    if(l == r) return ;
    int mid = (l + r) >> 1;
    Build(ll, l, mid); Build(rr, mid+1, r);
}
int a, b;
void Update(int o, int pos, int v)
{
    if(tree[o].l == tree[o].r)
    {
        int add = tree[o].sum[2] + v;
        tree[o].sum[0] = min(b, add);
        tree[o].sum[1] = min(a, add);
        tree[o].sum[2] = add;
        return ;
    }
    int mid = (tree[o].l + tree[o].r) >> 1;
    if(pos <= mid) Update(ll, pos, v);
    else Update(rr, pos, v);
    PushUp(o);
}
int Query(int o, int L, int R, int op)
{
    if(L > R) return 0;
    if(L == tree[o].l && R == tree[o].r)
        return tree[o].sum[op];
    int mid = (tree[o].l + tree[o].r) >> 1;
    if(R <= mid) return Query(ll, L, R, op);
    else if(L > mid) return Query(rr, L, R, op);
    else return Query(ll, L, mid, op) + Query(rr, mid+1, R, op);
}
int main()
{
    int n, k, q;
    cin >> n >> k >> a >> b >> q; Build(1, 1, n);
    while(q--)
    {
        int op; scanf("%d", &op);
        if(op == 1)
        {
            int d, a;
            scanf("%d%d", &d, &a);
            Update(1, d, a);
        }
        else
        {
            int p; scanf("%d", &p);
            cout << Query(1, 1, p-1, 0) + Query(1, p+k, n, 1) << endl;
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值