#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define pll pair<long long, long long>
#define loop(i,n) for(int i=1;i<=n;i++)
#define loop0(i,n) for(int i=0;i<n;i++)
using namespace std;
//pbds template
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
void solve(){
ll n;
cin >> n;
ll a[n+5] = {0};
ll b[n+5] = {0};
vector<pair<pair<ll,ll>,ll>> v;
map<ll,vector<ll>> m;
for(int i=1;i<=n;i++){
cin >> a[i];
}
for(int i=1;i<=n;i++){
cin >> b[i];
}
ll ans = 0;
for(int i=1;i<=n;i++){
v.pb({{a[i],b[i]},i});
}
sort(v.begin(),v.end());
// for(auto i:v){
// cout << i.fi.fi << ' ' << i.fi.se << ' ' << i.se << endl;
// }
ordered_set<ll> o_set;
for(auto i:v){
// cout << i.fi.fi << ' ' << i.fi.se << ' ' << m[i.fi.fi].size() << endl;
stack<ll> buffer;
if(m.count(i.fi.fi)){
for(auto j:m[i.fi.fi]){
ll idx1 = o_set.order_of_key(i.se);
ll idx2 = o_set.order_of_key(j);
// cout << idx1 << ' ' << idx2 << endl;
if(idx1==idx2){
ans++;
buffer.push(j);
}
}
while(!buffer.empty()){
o_set.insert(buffer.top());
buffer.pop();
}
}
if(i.fi.fi==i.fi.se){
ans++;
o_set.insert(i.se);
}
else{
m[i.fi.se].pb(i.se);
}
}
cout << ans << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tc = 1;
// cin >> tc;
while(tc--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |