#include<bits/stdc++.h>
#define mid (L+R)/2
using namespace std;
int n,i,ans,k;
int x[200001],sg[200001];
queue<int>q1[200001];
queue<int>q2[200001];
void upd(int u,int L,int R,int pos,int y){
if(R < pos || L > pos)return;
if(R == L){sg[u]=y;return;}
else{
upd(u*2,L,mid,pos,y);
upd(u*2+1,mid+1,R,pos,y);
sg[u]=sg[u*2]+sg[u*2+1];
}
}
int get(int u,int L,int R,int a,int b){
if(b < L || a > R)return 0;
if(a <= L && b >= R)return sg[u];
return(get(u*2,L,mid,a,b)+get(u*2+1,mid+1,R,a,b));
}
int count_swaps(std:: vector<int>x){
for(i=0;i<x.size();i++){
upd(1,1,x.size(),i,1);
}
for(i=0;i<x.size();i++){
if(x[i] < 0){
k=-(x[i]);
if(q2[k].size() == 0){
q1[k].push(i);
continue;
}
ans+=get(1,1,x.size(),q2[k].front(),i)-1;
upd(1,1,x.size(),q2[k].front(),2);
upd(1,1,x.size(),i,0);
q2[k].pop();
}
if(x[i] > 0){
k=x[i];
if(q1[k].size() == 0){
q2[k].push(i);
continue;
}
ans+=get(1,1,x.size(),q1[k].front(),i)-2;
upd(1,1,x.size(),q1[k].front(),2);
upd(1,1,x.size(),i,0);
q1[k].pop();
}
}
return ans;
}
Compilation message
shoes.cpp: In function 'int count_swaps(std::vector<int>)':
shoes.cpp:23:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<x.size();i++){
~^~~~~~~~~
shoes.cpp:26:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<x.size();i++){
~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
282 ms |
269560 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
282 ms |
269560 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
282 ms |
269560 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
278 ms |
269584 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
282 ms |
269560 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
282 ms |
269560 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |