标签:position ssi ESS nbsp rb_tree sequence com str etc
E. Intersection of Permutations
You are given two permutations a and b, both consisting of n elements. Permutation of nn elements is such a integer sequence that each value from 1 to n appears exactly once in it.
You are asked to perform two types of queries with them:
Print the answer for each query of the first type.
It is guaranteed that there will be at least one query of the first type in the input.
/************************************************************************************** * This code is written by akonoh. * * It can only be compiled by c++17 due to the usage of bits/extc++.h * * I used pb_ds to reduce the length of my code * * ************************************************************************************/ #include <bits/stdc++.h> #include <bits/extc++.h> using namespace std; using namespace __gnu_pbds; #define N 200*1000 int n,m; tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> tr[N+7],sb; inline int lb(int x){return x&-x;} void addup(int x,int y) { for(int i = x; i <= n ; i += lb(i)) { tr[i].insert(y); } } void del(int x, int y) { for(int i = x; i <= n; i += lb(i)) { tr[i].erase(y); } } int getans(int x, int y) { int ans = 0; for(int i = x; i > 0; i -= lb(i)) { int p = tr[i].order_of_key(y); if(*tr[i].lower_bound(y)==y)p++; ans += p; //this part is to find how many numbers are smaller or equal to y. } return ans; } int a[N+8],b[N+8]; int main() { scanf("%d%d",&n,&m); for(int i = 1 ; i<= n; i ++) { int x; scanf("%d",&x); a[x]=i; } for(int i = 1; i <= n ;i ++) { int x; scanf("%d",&x); b[i]=a[x];//renumeration } for(int i = 1; i <= n; i ++) addup(i,b[i]); for(int i = 1; i <= m; i ++) { int t; scanf("%d",&t); if(t==1) { int la,ra,lb,rb,ans=0; scanf("%d%d%d%d",&la,&ra,&lb,&rb); ans +=getans(rb,ra); ans -=getans(lb-1,ra); ans -=getans(rb,la-1); ans +=getans(lb-1,la-1); printf("%d\n",ans); } else { int x,y; scanf("%d%d",&x,&y); del(x,b[x]); del(y,b[y]); swap(b[x],b[y]); addup(x,b[x]); addup(y,b[y]); } } }
Record of coding:Codeforces 1093E
标签:position ssi ESS nbsp rb_tree sequence com str etc
原文地址:https://www.cnblogs.com/akonoh/p/10233989.html