#include<iostream>
#include<set>
#include<vector>
#include<unordered_set>
using namespace std;
typedef long long ll;
const int C=64;
// time travelling multiset
class TTMS{
vector<int>days;
vector<pair<int,int>>changes;
vector<multiset<int>>checkpoints;
multiset<int>curr_set;
int curr_day=0;
void add_day(){
days.push_back(curr_day);
while(checkpoints.size()*C<curr_day)
checkpoints.push_back(curr_set);
}
public:
TTMS(){
checkpoints={{}};
}
void add_new_day(int day){
curr_day=day;
}
void insert(int el){
add_day();
curr_set.insert(el);
changes.push_back({el,1});
}
void remove(int el){
add_day();
curr_set.erase(curr_set.find(el));
changes.push_back({el,-1});
}
vector<int> get(int day){
while(checkpoints.size()*C<=day)
checkpoints.push_back(curr_set);
multiset<int>s=checkpoints[day/C];
for(int i=0;i<(int)changes.size();i++){
if(days[i]>day)
break;
if(days[i]>day/C*C){
if(changes[i].second==1)
s.insert(changes[i].first);
else if(changes[i].second==-1)
s.erase(s.find(changes[i].first));
}
}
return{s.begin(),s.end()};
}
};
int get_closest(vector<int>a,vector<int>b){
int res=1e9;
int i=0,j=0;
while(i<(int)a.size()&&j<(int)b.size()){
res=min(res,abs(a[i]-b[j]));
if(i<(int)a.size()&&a[i]<=b[j])
i++;
else if(j<(int)b.size()&&b[j]<=a[i])
j++;
}
return res;
}
int n;
int heights[100000];
TTMS ttms[100000];
void init(int N, int D, int H[]) {
n=N;
for(int i=0;i<n;i++)
heights[i]=H[i];
}
ll to(int a,int b){
if(a<b)
swap(a,b);
return a+((ll)b<<32);
}
void curseChanges(int U, int A[], int B[]) {
unordered_set<ll>conns;
for(int day=0;day<U;day++){
int a=A[day],b=B[day];
ll conn=to(a,b);
ttms[a].add_new_day(day+1);
ttms[b].add_new_day(day+1);
if(conns.find(conn)==conns.end()){
conns.insert(conn);
ttms[a].insert(heights[b]);
ttms[b].insert(heights[a]);
}else{
conns.erase(conn);
ttms[a].remove(heights[b]);
ttms[b].remove(heights[a]);
}
}
}
int question(int x, int y, int v) {
vector<int>a=ttms[x].get(v);
vector<int>b=ttms[y].get(v);
return get_closest(a,b);
}
Compilation message
potion.cpp: In member function 'void TTMS::add_day()':
potion.cpp:19:35: warning: comparison of integer expressions of different signedness: 'std::vector<std::multiset<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
19 | while(checkpoints.size()*C<curr_day)
| ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
potion.cpp: In member function 'std::vector<int> TTMS::get(int)':
potion.cpp:44:35: warning: comparison of integer expressions of different signedness: 'std::vector<std::multiset<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
44 | while(checkpoints.size()*C<=day)
| ~~~~~~~~~~~~~~~~~~~~^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
19100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
20380 KB |
Output is correct |
2 |
Correct |
13 ms |
20304 KB |
Output is correct |
3 |
Correct |
19 ms |
20324 KB |
Output is correct |
4 |
Correct |
35 ms |
21700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
183 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
186 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
253 ms |
187972 KB |
Output is correct |
2 |
Correct |
110 ms |
30600 KB |
Output is correct |
3 |
Correct |
198 ms |
26036 KB |
Output is correct |
4 |
Correct |
826 ms |
101612 KB |
Output is correct |
5 |
Correct |
972 ms |
148504 KB |
Output is correct |
6 |
Correct |
265 ms |
102828 KB |
Output is correct |
7 |
Correct |
685 ms |
41316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
19100 KB |
Output is correct |
2 |
Correct |
14 ms |
20380 KB |
Output is correct |
3 |
Correct |
13 ms |
20304 KB |
Output is correct |
4 |
Correct |
19 ms |
20324 KB |
Output is correct |
5 |
Correct |
35 ms |
21700 KB |
Output is correct |
6 |
Runtime error |
183 ms |
262144 KB |
Execution killed with signal 9 |
7 |
Halted |
0 ms |
0 KB |
- |