P3658 好像很多人从13调到100。13分萌新求教治疗手段。
查看原帖
P3658 好像很多人从13调到100。13分萌新求教治疗手段。
180924
FLAT_LCH楼主2022/2/10 11:48
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

struct node
{
	long long a,b,c;
}p[2000000];

long long aaa[2000000]={},n,k,ans;

inline long long lowbit(long long x){return x&(-x);}
inline long long getsum(long long x){x=max(x,1ll);long long ans=0;while(x>0){ans+=aaa[x];x-=lowbit(x);}return ans;}
inline void add(long long x,long long s){while(x<1500000){aaa[x]+=s;x+=lowbit(x);}}

inline long long rd()
{
	long long s=0;char x='x';
	while(x<'0'||x>'9')x=getchar();
	while(x>='0'&&x<='9'){s=s*10+(x^48);x=getchar();}
	return s;
}

inline bool cmp1(node x,node y){return x.a>y.a;}
inline bool cmp2(node x,node y){return x.b<y.b;}

inline void readd()
{
	long long x;
	n=rd();k=rd();
	for(long long i=1;i<=n;i++)
	{
		x=rd();
		p[x].a=i;
	}
	for(long long i=1;i<=n;i++)
	{
		p[i].c=i;
		x=rd();
		p[x].b=i;
		//p[x].c=x;
	}
}

void cdq(long long l,long long r)
{
	if(l==r)return;
	
	long long mid=(l+r)/2;
	cdq(l,mid);cdq(mid+1,r);
	sort(p+l,p+mid+1,cmp2);sort(p+mid+1,p+r+1,cmp2);
	
	long long j=l;
	for(long long i=mid+1;i<=r;i++)
	{
		while(p[j].b<p[i].b&&j<mid+1)
		{
			add(p[j].c,1);
			j++;
		}
		ans+=getsum(p[i].c-k-1);
		//if(getsum(p[i].c-k-1))cout<<p[i].c<<' '<<k<<'#'<<getsum(p[i].c-k-1)<<endl;
	}
	for(long long i=l;i<j;i++)add(p[i].c,-1);
	//cout<<getsum(99999);
}

int main()
{
	//freopen("P3658_2.in","r",stdin);
	readd();
	sort(p+1,p+1+n,cmp1);
	cdq(1,n);
	cout<<ans;
	return 0;
}
/*
4 2
4
3
2
1
1
4
2
3
*/
2022/2/10 11:48
加载中...