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.
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 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.
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.
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.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve a dynamic problem with updates (increment orders on a day) and queries: given a start day pi for repairs of length k (days pi, pi+1, ..., pi+k-1), during which production is zero; before repair, production capacity per day is b (the current limited capacity); after repair (i.e., from day pi+k onward), production capacity per day is a (full capacity). The factory can choose which orders to fill, but orders are for specific days. Each order requires a thimble to be produced on that exact day. The factory can fill at most the production capacity per day. So the maximum number of orders filled is the sum over days of min(orders_on_day, production_capacity_on_that_day) given the capacities determined by the repair period.
Specifically:
For days < pi: capacity = b (since before repair)
For days in [pi, pi+k-1]: capacity = 0 (repair period)
For days >= pi+k: capacity = a (after repair)
We need to answer queries after a series of updates.
n up to 200k, q up to 200k.
Each query asks for sum_{d=1..n} min(orders[d], cap[d]) for a given repair start pi.
We have two production capacities: b (low) and a (high), and zero during repair.
So for a given query pi, answer = sum_{d=1}^{pi-1} min(ord[d], b) + 0 for d in [pi, pi+k-1] + sum_{d=pi+k}^{n} min(ord[d], a).
Thus we need to maintain two prefix sums: sum_before(pi-1) of min(ord[d], b) and sum_after(pi+k) of min(ord[d], a). The total = prefix_b(pi-1) + suffix_a(pi+k). But careful: days after repair includes days from pi+k to n. So we need suffix sum from pi+k to n of min(ord[d], a).
We have point updates: each update adds ai to ord[di]. So after each update, the values min(ord[d], b) and min(ord[d], a) for that day change. We need to support updates to these two values and answer queries for arbitrary pi.
Thus we need two Fenwick trees (or segment trees) for range sum queries: one for f_b[d] = min(ord[d], b), another for f_a[d] = min(ord[d], a). Then for query pi: ans = query1(1, pi-1) + query2(pi+k, n). That's O(log n) per query and O(log n) per update (update two BITs for that day). Since n,q up to 200k, that is fine.
But careful: When we update ord[di] by adding ai, we need to recompute f_b[di] and f_a[di] based on new ord[di]. However, the min is capped at b and a respectively, so if ord[di] was already >= b, adding more doesn't change f_b; similarly for a. So we need to know the current ord[di] to compute new min. We'll maintain an array ord[1..n] (current total orders per day). Initially ord[d]=0. For each update, we add ai to ord[di]. Then we compute new_fb = min(ord[di], b), new_fa = min(ord[di], a). Then we need to update the BITs with the difference delta_fb = new_fb - old_fb and delta_fa = new_fa - old_fa. To know old_fb and old_fa, we need to have stored previous computed min values, or we can recompute from ord before update? We'll maintain an array fb[d], fa[d] (or just store ord and compute min each time). Since updates are point increments, we can just compute old_min = min(ord before, cap) and new_min = min(ord after, cap). So we need ord before update. So we keep ord array.
Complexities: ord values can go high (up to sum of ai per day, but ai each up to 10000 and q up to 200k, so ord can be up to 2e9, but we only care about caps b and a up to 10000, but that's fine using int.
So the solution is straightforward: two BITs: bit_b for sum of min(ord, b) and bit_a for sum of min(ord, a). For update: read di, ai; get cur = ord[di]; new_cur = cur + ai; ord[di] = new_cur; compute fb_old = min(cur, b); fb_new = min(new_cur, b); update bit_b at di with (fb_new - fb_old). Similarly for fa with a. For query: read pi; ans = bit_b.sum(pi-1) + (bit_a.sum(n) - bit_a.sum(pi+k-1)??? Actually suffix from pi+k to n inclusive. If we use BIT that gives prefix sum up to index inclusive, then suffix = prefix(n) - prefix(pi+k-1). So ans = query_b(pi-1) + (query_a(n) - query_a(pi+k-1)). Ensure indices within range: if pi-1 < 1 then first part is 0. If pi+k-1 >= n then second part is 0 because repair period extends to end? But note pi is at most n-k+1, so pi+k-1 <= n, so suffix from pi+k to n may be empty if pi+k > n? pi+k-1 = n => pi+k = n+1 > n, so suffix empty, but pi is at most n-k+1, so pi+k-1 = n, so pi+k = n+1 > n, so suffix is empty. So second part = query_a(n) - query_a(n) = 0. That's fine. So we compute accordingly.
query 2: pi=1. ans = sum_b days before pi: pi-1=0 => 0; suffix from pi+k=3 to 5: sum_a days 3,4,5: fa[3]=2, fa[4]=2, fa[5]=2 total 6. Output 6 matches.
query 2: pi=3. pi is at most n-k+1=4? Actually n=5,k=2 => max pi=4. pi=3 valid. ans = sum_b days 1..2: fb[1]=1, fb[2]=1 => total 2; suffix from pi+k=5 to 5: fa[5]=2 => total 4. Output 4 matches.
Works.
Second sample: n=5,k=4,a=10,b=1. Updates:
day1 +5: ord1=5 => fb=min(5,1)=1, fa=min(5,10)=5.
day5 +5: ord5=5 => fb=1, fa=5.
day3 +2: ord3=2 => fb=1, fa=2.
day5 +2: ord5=7 => old fa5=5, new fa5=min(7,10)=7, delta+2; fb5 old=1, new=min(7,1)=1, delta 0.
Now query pi=1: ans = sum_b before 1 = 0; suffix from pi+k=5 to 5: fa[5]=7 => ans=7. Output 7 matches.
Query pi=2: ans = sum_b days 1..1: fb[1]=1; suffix from pi+k=6 to 5: empty => 0; ans=1. Output 1 matches.
So solution is correct.
Now consider corner cases: b < a, but that's fine.
Complexity: O((n+q) log n). Memory: O(n) for BIT and ord.
Implementation details:
We'll implement BIT class with size n. Methods: add(idx, delta) and sum(idx) returns prefix sum 1..idx inclusive. We'll use 1-indexed arrays.
Pseudo:
int n, k, a, b, q;
vector ord(n+1,0);
BIT bit_b(n), bit_a(n);
For each query:
read type;
if type == 1:
int d, ai;
cin >> d >> ai;
int old = ord[d];
int new_val = old + ai;
ord[d] = new_val;
// update b
int old_b = min(old, b);
int new_b = min(new_val, b);
bit_b.add(d, new_b - old_b);
// update a
int old_a = min(old, a);
int new_a = min(new_val, a);
bit_a.add(d, new_a - old_a);
else:
int p;
cin >> p;
int left = (p-1 >= 1) ? bit_b.sum(p-1) : 0;
int right = (p+k <= n) ? (bit_a.sum(n) - bit_a.sum(p+k-1)) : 0; // careful: if p+k > n, then suffix empty
// Since p <= n-k+1, p+k-1 <= n, so p+k <= n+1. When p+k == n+1, suffix empty.
if (p+k <= n) {
right = bit_a.sum(n) - bit_a.sum(p+k-1);
} else {
right = 0;
}
cout << left + right << endl;
That's it.
Edge: If p+k-1 == n, then suffix empty; we handle via condition p+k <= n? For p+k = n+1, then p+k-1 = n, so suffix from n+1 to n empty; p+k <= n? false, so right=0. So condition correct.
One more nuance: There is an initial state where no orders. ord all zeros. BIT initially zeros.
Now test with some random cases.
But is there any tricky part? The problem statement says: "The factory may opt to fill as many or as few of the orders in a single batch as it likes." That essentially means we can choose which orders to fill, but since orders are for specific days, the maximum we can fill is the sum of min(capacity, orders) per day because we can always fill up to capacity on each day, independently. There's no cross-day constraints. So it's indeed separable.
Thus solution is simple.
Now we should double-check constraints: a and b up to 10000, ai up to 10000, q up to 200k, n up to 200k. BIT sums can be up to n * a (2e5 * 1e4 = 2e9) fits in 32-bit signed? Actually 2e9 just fits in int32 (max 2,147,483,647). Might exceed if n=200k, a=10000, then max sum = 2e9 exactly, which is borderline but safe if using 64-bit (long long) to be safe. But int might be okay, but to be safe we use long long. However BIT sum might accumulate many updates, but each day's contribution min(ord, a) <= a <= 10000, so total sum <= 200000 * 10000 = 2e9, fits in 32-bit signed. But using long long is safe. However, the output may be up to 2e9, still fits in 32-bit. But to be safe, we can use long long in BIT sums. But BIT internal array storing cumulative sums could be up to 2e9, but int might hold but many languages int is 32-bit, so it's fine, but to avoid overflow when adding we can use long long. In C++, I'd use long long.
Implementation: BIT with vector.
Complexities: O((n+q) log n). Acceptable.
Now we must confirm that reading input correctly and output.
One more nuance: The problem statement includes "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." It also says "Initially, no orders are pending." Updates add orders. So each query is answered with current orders.
So BIT approach works.
But wait: The query says: "at the moment, how many orders could be filled if the factory decided to commence repairs on day pi?" That is exactly what we answered.
Thus solution.
Let's implement.
Edge Cases: p may be 1, then left sum empty. p may be n-k+1, then suffix sum may be zero if p+k > n? Actually p = n-k+1, then p+k-1 = n, so suffix from p+k = n+1 to n empty, so right=0.
Now test with example.
All good.
But there's a nuance: In query, the production capacity for days before pi is b. But after repairs, it's a. However, what about days that are after repairs but have production limited to a; if there are orders > a, we can only produce a that day. That's min.
Therefore solution stands.
Thus answer.
Now implement.
But I recall this is a known Codeforces problem: "F - Factory Repairs" or something. Yes, this is CF 416B? Actually I recall problem "C. Factory Repairs" from Round? But anyway.
So we will produce final code.
Implementation details in C++:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
class BIT {
public:
int n;
vector bit;
BIT(int n) : n(n), bit(n+1, 0) {}
void add(int idx, ll delta) {
while (idx <= n) {
bit[idx] += delta;
idx += idx & -idx;
}
}
ll sum(int idx) {
ll res = 0;
while (idx > 0) {
res += bit[idx];
idx -= idx & -idx;
}
return res;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k, a, b, q;
cin >> n >> k >> a >> b >> q;
vector ord(n+1, 0);
BIT bit_b(n), bit_a(n);
while (q--) {
int type;
cin >> type;
if (type == 1) {
int d, ai;
cin >> d >> ai;
int old = ord[d];
int nw = old + ai;
ord[d] = nw;
// update b
int old_b = min(old, b);
int new_b = min(nw, b);
if (new_b != old_b) bit_b.add(d, new_b - old_b);
// update a
int old_a = min(old, a);
int new_a = min(nw, a);
if (new_a != old_a) bit_a.add(d, new_a - old_a);
} else {
int p;
cin >> p;
ll left = (p-1 >= 1) ? bit_b.sum(p-1) : 0;
ll right = 0;
if (p + k <= n) {
right = bit_a.sum(n) - bit_a.sum(p + k - 1);
}
cout << left + right << '\n';
}
}
return 0;
}
Check condition: if p + k <= n, suffix from p+k to n inclusive = total - prefix(p+k-1). Since p+k-1 is inclusive index of last day before suffix. Yes.
But what if p+k-1 >= n? p+k-1 is at most n (since p ≤ n-k+1), but if p+k-1 == n, then p+k = n+1, so condition p+k <= n fails, so right = 0. Good.
All good.
Now confirm with second sample: after updates as described, query p=2: left = bit_b.sum(1) = min(ord1,b)=min(5,1)=1; p+k=2+4=6 >5 so right=0; ans=1. Good.
Thus final answer.
But wait, there is one more nuance: The problem states "the factory may opt to fill as many or as few of the orders in a single batch as it likes." Actually this implies we can choose which orders to fill, but we don't have to fill all orders. The maximum number we can fill is as computed. However, note that the factory can produce up to capacity each day, but after repairs, the capacity is a. So the per-day min is correct.
Thus solution is correct.
Now I'll produce final answer with explanation.
But the user asked to "help the owner answer his questions." So the answer will be code plus explanation? The system expects me to output final answer with code? The user message is likely a problem statement; they might want solution explanation and code. But the conversation: "We need to solve this problem." Usually on Codeforces, they want code. Since it's a chat, I should provide the solution code and explanation. The instruction: "You are a world-class programming algorithmist with deep expertise in competitive programming." So I should present the solution with explanation, then code.
Thus final response: explanation and C++ code.
But to be thorough, we can also explain the BIT approach.
Now note: The constraints: n up to 200k, q up to 200k, BIT works O(log n) each.
We can also use segment trees, but BIT is simpler.
Thus final answer. We can process updates and queries efficiently using two Fenwick trees (Binary Indexed Trees). One tree maintains, for each day, the number of orders that can be produced with the limited capacity b, i.e., min(orders[day], b). The other tree maintains the number that can be produced with the full capacity a, i.e., min(orders[day], a).
When an update adds ai orders to day di, we recompute both capped values and update the trees with the differences.
For a query that asks for the maximum number of orders that can be filled if repairs start on day p, the answer is:
code
sum_{day < p} min(orders[day], b) + sum_{day >= p+k} min(orders[day], a)
The first part is a prefix query on the first tree up to day p-1, the second part is a suffix query on the second tree from day p+k to n. Both can be obtained in O(log n).
cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
class BIT {
int n;
vector<ll> bit;
public:
BIT(int n) : n(n), bit(n + 1, 0) {}
void add(int idx, ll delta) {
while (idx <= n) {
bit[idx] += delta;
idx += idx & -idx;
}
}
ll sum(int idx) {
ll res = 0;
while (idx > 0) {
res += bit[idx];
idx -= idx & -idx;
}
return res;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k, a, b, q;
cin >> n >> k >> a >> b >> q;
vector<int> orders(n + 1, 0);
BIT capB(n), capA(n);
while (q--) {
int type;
cin >> type;
if (type == 1) {
int d, ai;
cin >> d >> ai;
int old = orders[d];
int nw = old + ai;
orders[d] = nw;
// update for capacity b
int old_b = min(old, b);
int new_b = min(nw, b);
if (new_b != old_b) capB.add(d, new_b - old_b);
// update for capacity a
int old_a = min(old, a);
int new_a = min(nw, a);
if (new_a != old_a) capA.add(d, new_a - old_a);
} else {
int p;
cin >> p;
ll left = (p - 1 >= 1) ? capB.sum(p - 1) : 0;
ll right = 0;
if (p + k <= n) {
right = capA.sum(n) - capA.sum(p + k - 1);
}
cout << left + right << '\n';
}
}
return 0;
}