题目链接:http://codeforces.com/contest/1093/problem/E
非常裸的带修改二维数点问题,直接树套树暴力就好了,mark一下pb_ds写法
代码:
#include<bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/rope>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef long long ll;
typedef pair<int,int> pii;
template <class T> using Tree=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const int MAXN=2e5+5;
const int INF=0x3f3f3f3f;
template<int SZ>
struct mstree
{
Tree<int> val[SZ+1];// for offline queries use vector with binary search instead
void upd(int x,int y,int opt=1)
{// x-coordinate between 1 and SZ inclusive
for(int i=x;i<=SZ;i+=i&-i)
{
if(opt) val[i].insert(y);
else val[i].erase(y);
}
}
int query(int x,int y)
{
int ret=0;
for(;x>0;x-=x&-x) ret+=val[x].order_of_key(y+1);
return ret;
}
int query(int lox, int hix, int loy, int hiy)
{// query number of elements within a rectangle
return query(hix,hiy)-query(lox-1,hiy)
-query(hix,loy-1)+query(lox-1,loy-1);
}
};
mstree<MAXN> se;
int a[MAXN],b[MAXN],pos[MAXN];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
pos[a[i]]=i;
}
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
se.upd(pos[b[i]],i);
}
for(int i=1;i<=m;i++)
{
int op;
scanf("%d",&op);
if(op==1)
{
int la,ra,lb,rb;
scanf("%d%d%d%d",&la,&ra,&lb,&rb);
int ans=se.query(la,ra,lb,rb);
printf("%d\n",ans);
}
else
{
int x,y;
scanf("%d%d",&x,&y);
se.upd(pos[b[x]],x,0);se.upd(pos[b[y]],y,0);
swap(b[x],b[y]);
se.upd(pos[b[x]],x);se.upd(pos[b[y]],y);
}
}
return 0;
}