Efficient simulation of epidemic tracking systems requires careful container selection to manage temporal data and regional states. The core architecture relies on a custom structure for movement logs and associative arrays for trackign hazardous zones.
struct TravelRecord {
int day;
int user_id;
int location;
};
std::vector<TravelRecord> daily_records[1010];
std::map<int, std::pair<int, int>> risk_windows;
std::map serves as the primary lookup mechanism, providing O(log n) retrieval through a balanced tree structure. It automatically orders entries by key and enforces unique identifiers, making it ideal for mapping region IDs to their active hazard periods. std::pair bundles the start and end timestamps of a risk window, accessible via .first and .second, eliminating the need for verbose interval structs.
Hazard Zone Interval Management
When a location is flagged, it enters a mandatory 7-day quarantine window [start, start + 6]. Subsequent flags for the same location must merge with existing windows if they overlap or are contiguous. The update routine checks the current boundary and extends it only when the new flag falls within or immediately follows the active period.
void update_risk_zone(int loc, int current_day) {
auto it = risk_windows.find(loc);
if (it == risk_windows.end()) {
risk_windows[loc] = {current_day, current_day + 6};
} else {
if (current_day <= it->second.second + 1) {
it->second.second = current_day + 6;
} else {
it->second = {current_day, current_day + 6};
}
}
}
Exposure Validation Logic
Identifying at-risk individuals involves cross-referencing travel logs against active hazard intervals. A record triggers an alert only when multiple temporal constraints align:
- The travel date falls within a rolling 7-day observation window relative to the current processing day.
- Both the historical travel date and the current evaluation day reside inside the location's registered risk interval.
bool is_exposed(int record_day, int loc, int current_day) {
auto it = risk_windows.find(loc);
if (it == risk_windows.end()) return false;
int win_start = it->second.first;
int win_end = it->second.second;
bool recent = (record_day >= current_day - 6) && (record_day <= current_day);
bool valid_interval = (record_day >= win_start) && (current_day <= win_end);
return recent && valid_interval;
}
Iterating through the entire historical dataset for each day causes severe performance degradation. The algorithm restricts scans to the previous seven days, leveraging the fixed observation window to maintain optimal daily complexity relative to the total timeline.
Complete Implementation
The full pipeline processes daily inputs, updates regional states, filters relevant logs, and outputs deduplicated user identifiers using std::set for auotmatic sorting.
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
constexpr int MAX_DAYS = 1010;
struct TravelRecord {
int day;
int user_id;
int location;
};
std::vector<TravelRecord> daily_records[MAX_DAYS];
std::map<int, std::pair<int, int>> risk_windows;
void update_risk_zone(int loc, int current_day) {
auto it = risk_windows.find(loc);
if (it == risk_windows.end()) {
risk_windows[loc] = {current_day, current_day + 6};
} else {
if (current_day <= it->second.second + 1) {
it->second.second = current_day + 6;
} else {
it->second = {current_day, current_day + 6};
}
}
}
bool is_exposed(int record_day, int loc, int current_day) {
auto it = risk_windows.find(loc);
if (it == risk_windows.end()) return false;
int win_start = it->second.first;
int win_end = it->second.second;
bool recent = (record_day >= current_day - 6) && (record_day <= current_day);
bool valid_interval = (record_day >= win_start) && (current_day <= win_end);
return recent && valid_interval;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int total_days;
if (!(std::cin >> total_days)) return 0;
for (int d = 0; d < total_days; ++d) {
int zone_count, record_count;
std::cin >> zone_count >> record_count;
for (int i = 0; i < zone_count; ++i) {
int z;
std::cin >> z;
update_risk_zone(z, d);
}
for (int i = 0; i < record_count; ++i) {
int r_day, r_user, r_loc;
std::cin >> r_day >> r_user >> r_loc;
if (r_day <= d) {
daily_records[d].push_back({r_day, r_user, r_loc});
}
}
std::set<int> flagged_users;
int start_scan = std::max(0, d - 6);
for (int scan_d = start_scan; scan_d <= d; ++scan_d) {
for (const auto& rec : daily_records[scan_d]) {
if (is_exposed(rec.day, rec.location, d)) {
flagged_users.insert(rec.user_id);
}
}
}
std::cout << d;
for (int uid : flagged_users) {
std::cout << " " << uid;
}
std::cout << "\n";
}
return 0;
}