#include<iostream>
#include<vector>
#include<unordered_set>
#include<unordered_map>
using namespace std;
typedef long long ll;
const int TREE_SIZE=1<<30;
const int NUM_NODES=5e7;
int val[NUM_NODES];
int ch1[NUM_NODES];
int ch2[NUM_NODES];
vector<int>*elements[NUM_NODES];
int pcount[NUM_NODES];
int collected[NUM_NODES];
int curr_node=1;
void collect_fn(int node,int l,int r){
if(collected[node]!=0)
return;
collected[node]++;
if(l==r-1){
if(val[node]!=0){
elements[node]=new vector<int>;
elements[node]->push_back(l);
}
collected[node]=true;
return;
}
int m=(l+r)/2;
if(ch1[node]!=0)
collect_fn(ch1[node],l,m);
if(ch2[node]!=0)
collect_fn(ch2[node],m,r);
if(elements[ch1[node]]==nullptr)
elements[node]=elements[ch2[node]];
else if(elements[ch2[node]]==nullptr)
elements[node]=elements[ch1[node]];
else{
vector<int>*a=elements[ch1[node]],*b=elements[ch2[node]];
elements[node]=new vector<int>(a->begin(),a->end());
elements[node]->insert(elements[node]->end(),b->begin(),b->end());
if(collected[ch1[node]]==pcount[ch1[node]])
delete elements[ch1[node]];
if(collected[ch2[node]]==pcount[ch2[node]])
delete elements[ch2[node]];
}
}
void collect(int node){
collect_fn(node,0,TREE_SIZE);
}
int insert_fn(int old_node,int x,int q,int l,int r){
if(x<l||r<=x)
return old_node;
int node=curr_node++;
if(old_node){
ch1[node]=ch1[old_node];
pcount[ch1[node]]++;
ch2[node]=ch2[old_node];
pcount[ch2[node]]++;
val[node]=val[old_node];
}
if(l==r-1){
val[node]+=q;
}else{
int m=(l+r)/2;
pcount[ch1[node]]--;
ch1[node]=insert_fn(ch1[node],x,q,l,m);
pcount[ch1[node]]++;
pcount[ch2[node]]--;
ch2[node]=insert_fn(ch2[node],x,q,m,r);
pcount[ch2[node]]++;
}
if(ch1[node]==0&&ch2[node]==0&&val[node]==0)
return 0;
return node;
}
int insert(int old_node,int x,int q){
return insert_fn(old_node,x,q,0,TREE_SIZE);
}
// time travelling multiset
class TTMS{
vector<int>days;
vector<int>sets;
public:
TTMS(){
days={-1};
sets={curr_node++};
}
void add_new_day(int day){
days.push_back(day);
sets.push_back(sets.back());
}
void insert(int el){
sets.back()=::insert(sets.back(),el,1);
}
void remove(int el){
sets.back()=::insert(sets.back(),el,-1);
}
vector<int>* get(int day){
int l=0,r=(int)sets.size();
while(l!=r-1){
int m=(l+r)/2;
if(day<days[m])
r=m;
else
l=m;
}
collect(sets[l]);
return elements[sets[l]];
}
};
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);
if(a==nullptr||b==nullptr)
return 1e9;
int res=get_closest(*a,*b);
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
11344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
13008 KB |
Incorrect |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
636 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
532 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
43 ms |
31332 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
11344 KB |
Output is correct |
2 |
Incorrect |
11 ms |
13008 KB |
Incorrect |
3 |
Halted |
0 ms |
0 KB |
- |