#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, m, z;
cin >> n >> m >> z;
vector<int> a(n); // Tree visits (the trees visited on each day)
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
// This will track how many leaves are eaten on each day
vector<int> leaves(n, 0); // Initialize with 0 leaves eaten each day
// Apply modifications
for (int i = 0; i < m; ++i) {
int p, w;
cin >> p >> w;
// Apply the modification to the first p days
for (int j = 0; j < p; ++j) {
leaves[j] += w;
}
}
// Build the prefix sum for the leaves eaten
vector<vector<long long>> prefix_sum(1000006, vector<long long>(n + 1, 0)); // Max tree number is 10^6
for (int i = 0; i < n; ++i) {
// We accumulate leaves in prefix sum for each tree
prefix_sum[a[i]][i+1] = leaves[i];
}
// Now compute the prefix sum for each tree
for (int i = 0; i < 1000006; ++i) {
for (int j = 1; j <= n; ++j) {
prefix_sum[i][j] += prefix_sum[i][j-1];
}
}
// Handle queries
for (int i = 0; i < z; ++i) {
int p, d;
cin >> p >> d;
// Output the answer for this query: total leaves eaten from tree d during the first p days
cout << prefix_sum[d][p] << endl;
}
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #include <iostream> #include <vector> using namespace std; int main() { int n, m, z; cin >> n >> m >> z; vector<int> a(n); // Tree visits (the trees visited on each day) for (int i = 0; i < n; ++i) { cin >> a[i]; } // This will track how many leaves are eaten on each day vector<int> leaves(n, 0); // Initialize with 0 leaves eaten each day // Apply modifications for (int i = 0; i < m; ++i) { int p, w; cin >> p >> w; // Apply the modification to the first p days for (int j = 0; j < p; ++j) { leaves[j] += w; } } // Build the prefix sum for the leaves eaten vector<vector<long long>> prefix_sum(1000006, vector<long long>(n + 1, 0)); // Max tree number is 10^6 for (int i = 0; i < n; ++i) { // We accumulate leaves in prefix sum for each tree prefix_sum[a[i]][i+1] = leaves[i]; } // Now compute the prefix sum for each tree for (int i = 0; i < 1000006; ++i) { for (int j = 1; j <= n; ++j) { prefix_sum[i][j] += prefix_sum[i][j-1]; } } // Handle queries for (int i = 0; i < z; ++i) { int p, d; cin >> p >> d; // Output the answer for this query: total leaves eaten from tree d during the first p days cout << prefix_sum[d][p] << endl; } return 0; } |
English