#include<iostream>
#include<set>
#include<vector>
#include<unordered_set>
using namespace std;
typedef long long ll;
int u;
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);
if(checkpoints.size()*C<days.size())
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});
}
multiset<int>*get(int day){
multiset<int>*s=new multiset<int>;
if(day==u){
*s={curr_set.begin(),curr_set.end()};
return s;
}
int l=0,r=(int)days.size();
while(l<r-1){
int m=(l+r)/2;
if(days[m]<=day)
l=m;
else
r=m;
}
int idx=l;
if(idx/C>=(int)checkpoints.size())
*s=curr_set;
else
*s=checkpoints[idx/C];
for(int i=idx/C*C;i<(int)changes.size();i++){
if(days[i]>day)
break;
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;
}
};
int get_closest(multiset<int>*a,multiset<int>*b){
int res=1e9;
auto i=a->begin();
auto j=b->begin();
while(i!=a->end()&&j!=b->end()){
res=min(res,abs(*i-*j));
if(i!=a->end()&&*i<=*j)
i++;
else if(j!=b->end()&&*j<=*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[]){
u=U;
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) {
auto*a=ttms[x].get(v);
auto*b=ttms[y].get(v);
int res=get_closest(a,b);
delete a;
delete b;
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
13008 KB |
Output is correct |
2 |
Correct |
7 ms |
13008 KB |
Output is correct |
3 |
Correct |
7 ms |
13008 KB |
Output is correct |
4 |
Correct |
16 ms |
13920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
387 ms |
58380 KB |
Output is correct |
2 |
Correct |
390 ms |
58356 KB |
Output is correct |
3 |
Correct |
150 ms |
23320 KB |
Output is correct |
4 |
Correct |
2123 ms |
71644 KB |
Output is correct |
5 |
Correct |
840 ms |
50164 KB |
Output is correct |
6 |
Execution timed out |
3010 ms |
89620 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
373 ms |
58460 KB |
Output is correct |
2 |
Correct |
1174 ms |
83616 KB |
Output is correct |
3 |
Correct |
883 ms |
63320 KB |
Output is correct |
4 |
Correct |
1327 ms |
89252 KB |
Output is correct |
5 |
Correct |
475 ms |
60692 KB |
Output is correct |
6 |
Correct |
1774 ms |
89428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
15440 KB |
Output is correct |
2 |
Correct |
141 ms |
13448 KB |
Output is correct |
3 |
Correct |
239 ms |
13460 KB |
Output is correct |
4 |
Correct |
675 ms |
15580 KB |
Output is correct |
5 |
Correct |
631 ms |
15952 KB |
Output is correct |
6 |
Correct |
116 ms |
14544 KB |
Output is correct |
7 |
Correct |
598 ms |
14344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12752 KB |
Output is correct |
2 |
Correct |
7 ms |
13008 KB |
Output is correct |
3 |
Correct |
7 ms |
13008 KB |
Output is correct |
4 |
Correct |
7 ms |
13008 KB |
Output is correct |
5 |
Correct |
16 ms |
13920 KB |
Output is correct |
6 |
Correct |
387 ms |
58380 KB |
Output is correct |
7 |
Correct |
390 ms |
58356 KB |
Output is correct |
8 |
Correct |
150 ms |
23320 KB |
Output is correct |
9 |
Correct |
2123 ms |
71644 KB |
Output is correct |
10 |
Correct |
840 ms |
50164 KB |
Output is correct |
11 |
Execution timed out |
3010 ms |
89620 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |