题解:P11197 [COTS 2021] 赛狗游戏 Tiket
前两天模拟赛做到了这个题的扩展,不会这个 trick 但是用 cdq 卡常生吃 1e6,现在来补个题。
首先我们可以用 cdq 两 log 做,但是需要卡常。
我们设
扔到这题里面,就转化为
考虑如何求这仨。发现
于是我们就在
#include<bits/stdc++.h>
#define ll long long
#define gc getchar
//char buf[1<<20],*p1,*p2;
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
#define R read()
using namespace std;
int read()
{
int x=0,f=1;
char c=gc();
while(c>'9'||c<'0'){if(c=='-') f=-1;c=gc();}
while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+c-48,c=gc();
return x*f;
}
void write(ll x,char xx)
{
static int st[35],top=0;
if(x<0){x=-x;putchar('-');}
do
{
st[top++]=x%10,x/=10;
}while(x);
while(top) putchar(st[--top]+48);
putchar(xx);
}
#define N 1000010
int n,sum,vin[N],p[N];
ll ans;
struct node{int a,b,c;}s[N];
bool cmpa(node x,node y){return x.a<y.a;}
bool cmpb(node x,node y){return x.b<y.b;}
bool cmpc(node x,node y){return x.c<y.c;}
struct BIT
{
int s[N];
#define lb (x&-x)
void add(int x){while(x<=n)s[x]++,x+=lb;}
void del(int x){while(x<=n)s[x]=0,x+=lb;}
int que(int x){int su=0;while(x)su+=s[x],x-=lb;return su;}
}T1;
bool vis[N];
signed main()
{
n=R;
for(int i=1;i<=n;i++) s[i].a=R;
for(int i=1;i<=n;i++) s[i].a=R,p[s[i].a]=i;
for(int i=1;i<=n;i++) s[i].a=p[i];
for(int i=1;i<=n;i++) s[i].b=R,p[s[i].b]=i;
for(int i=1;i<=n;i++) s[i].b=p[i];
for(int i=1;i<=n;i++) s[i].c=R,p[s[i].c]=i;
for(int i=1;i<=n;i++) s[i].c=p[i];
sort(s+1,s+n+1,cmpa);
for(int i=1,su=0;i<=n;i++) ans+=1ll*T1.que(s[i].b),T1.add(s[i].b);
for(int i=1;i<=n;i++) T1.s[i]=0;
sort(s+1,s+n+1,cmpb);
for(int i=1,su=0;i<=n;i++) ans+=1ll*T1.que(s[i].c),T1.add(s[i].c);
for(int i=1;i<=n;i++) T1.s[i]=0;
sort(s+1,s+n+1,cmpc);
for(int i=1,su=0;i<=n;i++) ans+=1ll*T1.que(s[i].a),T1.add(s[i].a);
for(int i=1;i<=n;i++) T1.s[i]=0;
write((ans-1ll*(n-1)*n/2)/2,'\n');
return 0;
}