# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1134972 | orzdraiduwu | Sum Zero (RMI20_sumzero) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
#define int long long
// using pr = array<int, 4>;
using pr = array<int, 2>;
// we do binary lifting on the intervals, it's heavy implementation
// so i'll wa this to submit later :sob:
signed main() {
int n; cin >> n;
vector<int> v(n);
for(int i = 0 ; i < n ; i++) {
int x; cin >> x;
// c += x;
v[i] = x;
}
unordered_map<int, int> gg;
vector<pr> uwu;
int c = 0;
for(int i = 0 ; i < n ; i++) {
// if(v[i] == 0) uwu.push_back({i, i});
// else if(gg.contains(v[i])) uwu.push_back({gg[v[i]], i});
// gg[v[i]] = i;
c += v[i];
if(c == 0) uwu.push_back({0, i});
else if(gg.contains(c)) uwu.push_back({gg[c]+1, i});
gg[c] = i;
}
// dbg
// int k = uwu.size();
// for(int i = 0 ; i < k ; i++) {
// cout << uwu[i][0] << " " << uwu[i][1] << endl;
// }
// building the graph.
vector<int> gr(k);
vector<bool> vis(k);
unoredered_set<int> gg;
for(int i = 0 ; i < k ; i++) gr[i] = i;
sort(uwu.begin(), uwu.end(), [](pr a, pr b) {return a[1] < b[1];});
for(int i = 0 ; i < k ; i++) {
if(vis[i]) continue;
vector<int> del;
int m = uwu[i][1], ind = i;
for(auto it = gg.begin() ; it != gg.end() ; it++) {
int qq = (*it)[0], pp = (*it)[1];
if(m <= qq) {
m = pp;
del.push_back(ind);
gr[ind] = *it;
vis[ind] = 1;
ind = *it;
}
}
for(int c: del) gg.erase(c);
}
for(int i = 0 ; i < n ; i++) cout << gr[i] << ' ';
}