【NWERC2015】【BZOJ4430】Guessing Camels赌骆驼

Description

Jaap, Jan, and Thijs are on a trip to the desert after having attended the ACM ICPC World Finals 2015 in Morocco. The trip included a camel ride, and after returning from the ride, their guide invited them to a big camel race in the evening. The camels they rode will also participate and it is customary to bet on the results of the race.
One of the most interesting bets involves guessing the complete order in which the camels will finish the race. This bet offers the biggest return on your money, since it is also the one that is the hardest to get right.
Jaap, Jan, and Thijs have already placed their bets, but the race will not start until an hour from now, so they are getting bored. They started wondering how many pairs of camels they have put in the same order. If camel cc is before camel d on Jaap’s, Jan’s and Thijs’ bet, it means that all three of them put c and d in the same order. Can you help them to calculate the number of pairs of camels for which this happened?
Jaap,Jan和Thijs在摩洛哥参加完ACMICPC2015全球总决赛后去沙漠进行了一次旅行。旅行包括了骑骆驼。在骑完回来之后,他们的向导邀请他们在晚上去看一场盛大的骆驼赛跑。他们骑的骆驼也会参加,而赌比赛的结果是一个习惯。其中一个最有趣的赌涉及猜测完成比赛的骆驼的完整名单。这个赌可以为你赢得最多的钱,因为它也是最难猜对的。
Jaap,Jan和Thijs已经下了注,但比赛要在一个小时后才开始,所以他们觉得很无聊。他们开始想知道有多少对骆驼他们赌了同样的顺序。如果在他们三人的猜测名单中,骆驼c在骆驼d前,就意味着c和d在他们的名单中有相同的顺序。你能帮他们计算有多少对骆驼是这样的吗?
Input

The input consists of:
one line with an integer n ( 2≤n≤200000), the number of camels;
one line with n integers a1,…,ana1,…,an ( 1≤ai≤n for all i ), Jaap’s bet. Here a1 is the camel in the first position of Jaap’s bet, a2 is the camel in the second position, and so on;
one line with Jan’s bet, in the same format as Jaap’s bet;
one line with Thijs’ bet, in the same format as Jaap’s bet.
The camels are numbered 1,…,n . Each camel appears exactly once in each bet.
输入包括:
第一行是一个整数n(2<=n<=200000),骆驼的数量。
第二行有n个整数a1…an(1<=ai<=n),是Jaap的下赌名单。a1是名单中第一位,a2是第二位,等等。
第三行是Jan的名单,格式同上。
第四行是Thijs的名单,格式同上。
骆驼从1到n编号,每头骆驼在一份名单中只出现一次。
Output

Output the number of pairs of camels that appear in the same order in all 3 bets.
输出在三份名单中同样顺序的骆驼有多少对。
Sample Input

Sample input 1

3

3 2 1

1 2 3

1 2 3

Sample input 2

4

2 3 1 4

2 1 4 3

2 4 3 1
Sample Output

Sample output 1

0

Sample output 2

3

HINT

Source

上个周末两天考试,这周前两天给BC出题
今天终于把这些事都结束了,恢复日常刷水
【NWERC2015】【BZOJ4430】Guessing Camels赌骆驼_第1张图片
如上,BIT维护一下就好了

#include
#include
#include
#include
#include
#define MAXN 200010
#define lowbit(x) (x&(-x))
#define GET (ch>='0'&&ch<='9')
#define LL long long
using namespace std;
int n;
int p1[MAXN],p2[MAXN],p3[MAXN],pos[MAXN];
int c[MAXN];
inline void add(int x,int delta)    {   for (;x<=n;x+=lowbit(x))    c[x]+=delta;    }
inline int query(int x)
{
    int ret=0;
    for (;x;x-=lowbit(x))   ret+=c[x];
    return ret;
}
inline void in(int &x)
{
    char ch=getchar();x=0;
    while (!GET)    ch=getchar();
    while (GET) x=x*10+ch-'0',ch=getchar();
}
int main()
{
    in(n);LL ans=1ll*n*(n-1);
    for (int i=1;i<=n;i++)  in(p1[i]);
    for (int i=1;i<=n;i++)  in(p2[i]);
    for (int i=1;i<=n;i++)  in(p3[i]);
    for (int i=1;i<=n;i++)  pos[p1[i]]=i,c[i]=0;
    for (int i=n;i;i--) ans-=query(pos[p2[i]]),add(pos[p2[i]],1);
    for (int i=1;i<=n;i++)  pos[p2[i]]=i,c[i]=0;
    for (int i=n;i;i--) ans-=query(pos[p3[i]]),add(pos[p3[i]],1);
    for (int i=1;i<=n;i++)  pos[p3[i]]=i,c[i]=0;
    for (int i=n;i;i--) ans-=query(pos[p1[i]]),add(pos[p1[i]],1);
    cout<<(ans>>1)<

你可能感兴趣的:(随便搞搞)