Educational Codeforces Round 56 (Rated for Div. 2) E. Intersection of Permutations(树套树)

题目链接: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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值