Walk Alone planted nnn magical carrot trees in a row Let aia_iai denote the number of carrots on the iii-th tree note that aia_iai is a real number Initially there are no carrots on any tree On each
Here is a possible implementation in C++:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<double> carrots(n+1, 0);
for (int i = 0; i < m; i++) {
int type, l, r, x;
cin >> type >> l >> r;
if (type == 1) {
cin >> x;
for (int j = l; j <= r; j++) {
carrots[j] += x/k;
}
} else {
for (int j = l; j <= r; j++) {
if (carrots[j] >= 1) {
carrots[j] -= 1;
}
}
}
}
int totalCarrots = 0;
for (int i = 1; i <= n; i++) {
totalCarrots += (int)carrots[i];
}
cout << totalCarrots << endl;
return 0;
}
Explanation:
- We start by reading the input values for n, m, and k.
- We create a vector
carrotsof size n+1 to store the number of carrots on each tree. We initialize all elements to 0. - We iterate m times to process each operation.
- For each operation, we read the type (1 or 2), l, and r. If the type is 1, we also read x.
- If the type is 1, we iterate from l to r and increase the number of carrots on each tree by x/k.
- If the type is 2, we iterate from l to r and decrease the number of carrots on each tree by 1 if the current number of carrots is at least 1.
- After processing all operations, we calculate the total number of carrots by summing up the integer part of each element in the
carrotsvector. - Finally, we output the total number of carrots
原文地址: https://www.cveoy.top/t/topic/h7PY 著作权归作者所有。请勿转载和采集!