#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
int SSZ=20;
vector<pair<int,int>>C[100100];
map<int,set<int>> temps[100100], prooc[100100];
set<int>cur[100100];
int h[100100],ord[100100],rev[100100],CC;
void init(int N, int D, int H[]) {
vector<int>v;
for(int i=0;i<N;i++) h[i]=H[i],
v.push_back(i),temps[i][-1];
sort(v.begin(),v.end(),[](int a,int b){return h[a]<h[b];});
for(int i=0;i<N;i++)
rev[ord[v[i]]=i]=v[i];
}
void curseChanges(int U, int A[], int B[]) {
for(int i=0;i<U;i++){
A[i]=ord[A[i]];
B[i]=ord[B[i]];
if(cur[A[i]].count(B[i]))
cur[A[i]].erase(B[i]),
cur[B[i]].erase(A[i]);
else cur[A[i]].insert(B[i]),
cur[B[i]].insert(A[i]);
C[A[i]].push_back({i,B[i]});
C[B[i]].push_back({i,A[i]});
if(C[A[i]].size()%SSZ==0)
temps[A[i]][i]=cur[A[i]];
if(C[B[i]].size()%SSZ==0)
temps[B[i]][i]=cur[B[i]];
}
for(auto&i:cur)
i.clear();
}
int calc(vector<int>A,vector<int>B){
int l=0,r=0,ans=1e9;
while(l<A.size()&&r<B.size()){
ans=min(ans,abs(A[l]-B[r]));
if(A[l]<B[r])l++;
else r++;
}
return ans;
}
int question(int x, int y, int v) {
x=ord[x],y=ord[y];
vector<int>A,B;
auto it=--temps[x].lower_bound(v);
set<int>X;
swap(X,it->second);
auto it2=lower_bound(C[x].begin(),C[x].end(),make_pair(it->first+1,0));
int I=it2-C[x].begin(),begI=I;
for(;I<C[x].size()&&C[x][I].first<v;I++)
if(X.count(C[x][I].second))
X.erase(C[x][I].second);
else X.insert(C[x][I].second);
for(auto i:X)A.push_back(h[rev[i]]);
while(--I>=begI)if(X.count(C[x][I].second))
X.erase(C[x][I].second);
else X.insert(C[x][I].second);
swap(X,it->second);
it=--temps[y].lower_bound(v);
swap(X,it->second);
it2=lower_bound(C[y].begin(),C[y].end(),make_pair(it->first+1,0));
I=it2-C[y].begin(),begI=I;
for(;I<C[y].size()&&C[y][I].first<v;I++)
if(X.count(C[y][I].second))
X.erase(C[y][I].second);
else X.insert(C[y][I].second);
for(auto i:X)B.push_back(h[rev[i]]);
while(--I>=begI)if(X.count(C[y][I].second))
X.erase(C[y][I].second);
else X.insert(C[y][I].second);
swap(X,it->second);
return calc(A,B);
}
#ifdef ONLINE_JUDGE
int main() {
int N, D, U, Q;
std::ios::sync_with_stdio(false); std::cin.tie(NULL);
std::cin >> N >> D >> U >> Q;
int *F = new int[N];
for (int i=0; i<N; i++)
std::cin >> F[i];
init(N, D, F);
int *A = new int[U], *B = new int[U];
for (int i=0; i<U; i++) {
std::cin >> A[i] >> B[i];
}
curseChanges(U, A, B);
bool correct = true;
for (int i=0; i<Q; i++) {
int X,Y,V;
std::cin >> X >> Y >> V;
cout<<question(X,Y,V)<<endl;
}
return 0;
}
#endif
Compilation message
potion.cpp: In function 'int calc(std::vector<int>, std::vector<int>)':
potion.cpp:38:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | while(l<A.size()&&r<B.size()){
| ~^~~~~~~~~
potion.cpp:38:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | while(l<A.size()&&r<B.size()){
| ~^~~~~~~~~
potion.cpp: In function 'int question(int, int, int)':
potion.cpp:53:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(;I<C[x].size()&&C[x][I].first<v;I++)
| ~^~~~~~~~~~~~
potion.cpp:66:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for(;I<C[y].size()&&C[y][I].first<v;I++)
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16984 KB |
Output is correct |
2 |
Correct |
9 ms |
17116 KB |
Output is correct |
3 |
Correct |
9 ms |
16984 KB |
Output is correct |
4 |
Correct |
35 ms |
28488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
339 ms |
54660 KB |
Output is correct |
2 |
Correct |
363 ms |
54616 KB |
Output is correct |
3 |
Correct |
299 ms |
43668 KB |
Output is correct |
4 |
Correct |
1183 ms |
151536 KB |
Output is correct |
5 |
Correct |
555 ms |
63980 KB |
Output is correct |
6 |
Correct |
1525 ms |
208968 KB |
Output is correct |
7 |
Correct |
490 ms |
57988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
330 ms |
54724 KB |
Output is correct |
2 |
Correct |
1517 ms |
222640 KB |
Output is correct |
3 |
Correct |
1049 ms |
124596 KB |
Output is correct |
4 |
Correct |
1697 ms |
208268 KB |
Output is correct |
5 |
Correct |
504 ms |
64068 KB |
Output is correct |
6 |
Correct |
1830 ms |
208888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
19608 KB |
Output is correct |
2 |
Correct |
155 ms |
18776 KB |
Output is correct |
3 |
Correct |
203 ms |
19032 KB |
Output is correct |
4 |
Correct |
440 ms |
22276 KB |
Output is correct |
5 |
Correct |
411 ms |
22276 KB |
Output is correct |
6 |
Correct |
180 ms |
18656 KB |
Output is correct |
7 |
Correct |
415 ms |
21180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16728 KB |
Output is correct |
2 |
Correct |
9 ms |
16984 KB |
Output is correct |
3 |
Correct |
9 ms |
17116 KB |
Output is correct |
4 |
Correct |
9 ms |
16984 KB |
Output is correct |
5 |
Correct |
35 ms |
28488 KB |
Output is correct |
6 |
Correct |
339 ms |
54660 KB |
Output is correct |
7 |
Correct |
363 ms |
54616 KB |
Output is correct |
8 |
Correct |
299 ms |
43668 KB |
Output is correct |
9 |
Correct |
1183 ms |
151536 KB |
Output is correct |
10 |
Correct |
555 ms |
63980 KB |
Output is correct |
11 |
Correct |
1525 ms |
208968 KB |
Output is correct |
12 |
Correct |
490 ms |
57988 KB |
Output is correct |
13 |
Correct |
330 ms |
54724 KB |
Output is correct |
14 |
Correct |
1517 ms |
222640 KB |
Output is correct |
15 |
Correct |
1049 ms |
124596 KB |
Output is correct |
16 |
Correct |
1697 ms |
208268 KB |
Output is correct |
17 |
Correct |
504 ms |
64068 KB |
Output is correct |
18 |
Correct |
1830 ms |
208888 KB |
Output is correct |
19 |
Correct |
60 ms |
19608 KB |
Output is correct |
20 |
Correct |
155 ms |
18776 KB |
Output is correct |
21 |
Correct |
203 ms |
19032 KB |
Output is correct |
22 |
Correct |
440 ms |
22276 KB |
Output is correct |
23 |
Correct |
411 ms |
22276 KB |
Output is correct |
24 |
Correct |
180 ms |
18656 KB |
Output is correct |
25 |
Correct |
415 ms |
21180 KB |
Output is correct |
26 |
Correct |
689 ms |
92124 KB |
Output is correct |
27 |
Correct |
1162 ms |
125600 KB |
Output is correct |
28 |
Correct |
1198 ms |
118188 KB |
Output is correct |
29 |
Correct |
1256 ms |
152272 KB |
Output is correct |
30 |
Correct |
1892 ms |
208296 KB |
Output is correct |
31 |
Correct |
1681 ms |
229900 KB |
Output is correct |
32 |
Correct |
1837 ms |
208856 KB |
Output is correct |