Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Don't overcomplicate it: Add and Remove generate no traffic at all. So the final answer is just the sum of contributions from Send commands.
If there are currently people in the chat and someone sends a message of length , then the server sends that message to every participant, so this command contributes bytes.
Because the input is guaranteed to be valid, you don't actually need to store the whole set of usernames. Tracking only the current number of users in the chat is enough. Yeah, a set would work too, but it'd be overkill for this tiny bastard.
Read input line by line using getline, because message texts may contain spaces. For a line of the form sender:message, the message length is simply the number of characters after the first :.
Maintain online and ans. For each line: if it starts with '+', do online++; if it starts with '-', do online--; otherwise find the colon position p and add
to the answer. Then print ans.
This problem is pure simulation. No DP, no graph, no black magic—just don't screw up the parsing.
Only the Send command creates outgoing traffic. Add and Remove contribute exactly .
So the whole answer is:
over all Send commands.
At first glance you may think you need a set of usernames. You can do that, but honestly it's unnecessary.
Because the statement guarantees the input is correct:
Add,Remove,we only need the current number of people in the chat.
Let that be .
Then:
+name means ,-name means ,sender:message means add
to the answer.Use getline, not cin >> ....
Why? Because messages may contain spaces, and can even be empty. A send command looks like:
So for a line s, find the first colon at position .
Then the message length is:
That's it.
Initialize:
For each input line s:
s[0] == '+', increment .s[0] == '-', decrement .:Finally print .
We maintain the invariant that equals the number of users currently in the chat after processing each command.
Add, exactly one user joins, so increasing by keeps the invariant true.Remove, exactly one user leaves, so decreasing by keeps the invariant true.Send, the server sends the message to all current participants, including the sender. If there are participants and the message length is , then the produced traffic is exactly . Adding this to is therefore correct.Since every command is processed correctly, the final is exactly the total outgoing traffic.
Let be the total number of characters in the input.
Which is hilariously more than enough for the limits.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
string s;
ll online = 0, ans = 0;
while (getline(cin, s)) {
if (s.empty()) continue;
if (s[0] == '+') {
++online;
} else if (s[0] == '-') {
--online;
} else {
size_t pos = s.find(':');
ll len = (ll)s.size() - (ll)pos - 1;
ans += online * len;
}
}
cout << ans << '\n';
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
string s;
ll online = 0, ans = 0;
while (getline(cin, s)) {
if (s.empty()) continue;
if (s[0] == '+') {
++online;
} else if (s[0] == '-') {
--online;
} else {
size_t pos = s.find(':');
ll len = (ll)s.size() - (ll)pos - 1;
ans += online * len;
}
}
cout << ans << '\n';
}