B - ロボットの宝集め / Robot's Treasure Collection 解説 by admin
Claude 4.6 Opus (Thinking)Overview
A simulation problem where a robot moves through \(N\) rooms arranged in a line and collects treasures according to a command sequence. You must handle partial modifications to the command sequence and answer queries about the score obtained under the current command sequence.
Analysis
First, note that for each operation 2 (score query), the robot completely restarts the simulation from the initial state. In other words, each query is independent.
If we naively simulate the command sequence of length \(Q\) character by character from the beginning each time, it costs \(O(Q)\) per query. If there are \(K\) queries, the total cost is \(O(KQ)\).
At first glance, since \(Q \leq 10^5\) and \(M \leq 10^5\), it seems like \(KQ\) could reach up to \(10^{10}\), which is concerning. However, the constraints explicitly state that \(KQ \leq 2 \times 10^7\). This guarantees that “when there are many queries, the command sequence is short, and when the command sequence is long, there are few queries,” meaning that a straightforward simulation is fast enough.
Algorithm
Process each operation in order.
- Operation
1 p c: Simply replace the \(p\)-th character (0-indexed: \(p-1\)) of the command sequence \(S\) with character \(c\). \(O(1)\). - Operation
2: Simulate the command sequence \(S\) character by character from the beginning, starting from the initial state: robot in room \(1\), score \(0\), all treasures uncollected.L: Move left if the room number is greater than \(1\)R: Move right if the room number is less than \(N\)P: If the treasure in the current room has not been collected, collect it and add to the scoreB: Return to room \(1\)
A bool collected[] array is used to track whether a treasure has been collected. After the simulation ends, only the rooms where treasures were actually collected are recorded, and only those flags are reset. This avoids the \(O(N)\) cost of reinitializing the entire array each time.
Concrete Example
If the rooms are \([10, 20, 30]\) (\(N=3\)) and the command sequence is RRPP:
1. R: Room \(1 \to 2\)
2. R: Room \(2 \to 3\)
3. P: Collect the treasure in room \(3\) (value \(30\)). Score \(= 30\)
4. P: Room \(3\) already collected. Do nothing.
The final score is \(30\).
Complexity
- Time complexity: \(O(M + KQ)\) (where \(K\) is the number of operation
2queries). Since \(KQ \leq 2 \times 10^7\) by the constraints, this is sufficiently fast. - Space complexity: \(O(N + Q)\) (for storing the treasure values array and the command sequence)
Implementation Notes
Resetting collection flags: Instead of initializing the entire
collectedarray in \(O(N)\) for each simulation, record the room numbers where treasures were actually collected in avisitedlist, and reset only those entries after the simulation. This keeps the cost of each simulation at \(O(Q)\).static bool collected[200001]: By allocating this as a global or static array, the initial values are guaranteed to be \(0\) (false), eliminating the need for explicit initialization each time.Fast I/O in C++: Use
ios_base::sync_with_stdio(false)andcin.tie(NULL)to speed up input/output and fit within the time limit.Source Code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, Q, M;
cin >> N >> Q >> M;
vector<long long> A(N + 1);
for (int i = 1; i <= N; i++) cin >> A[i];
string S;
cin >> S;
static bool collected[200001];
for (int i = 0; i < M; i++) {
int type;
cin >> type;
if (type == 1) {
int p;
char c;
cin >> p >> c;
S[p - 1] = c;
} else {
long long score = 0;
int pos = 1;
vector<int> visited;
for (int j = 0; j < Q; j++) {
char cmd = S[j];
if (cmd == 'L') {
if (pos > 1) pos--;
} else if (cmd == 'R') {
if (pos < N) pos++;
} else if (cmd == 'P') {
if (!collected[pos]) {
collected[pos] = true;
score += A[pos];
visited.push_back(pos);
}
} else { // B
pos = 1;
}
}
for (int v : visited) collected[v] = false;
cout << score << '\n';
}
}
return 0;
}
This editorial was generated by claude4.6opus-thinking.