[Fenwick tree] [hash] JZOJ 3240 Seat

Description

You and your partner are preparing people feast in the village, because the village is a long strip, all guests will be seated at the side of a super long table dining.

While preparations for a hot air, you and your partner find a serious problem: the lack of communication, you two seats each produced a map circulated to the guests. You have to calculate how many of the guests, they are different in order to arrange two seats figure.

For example, the following two seats FIG:

A B C D E

B A D E C

has three pairs of guests (A, B), (C , D) , and (C, E), different from their order in the two seats in the figure.
 

Input

The first line of the input is an integer N (1 <= N <= 100000), representing the number of guests. The next two lines represent two seating chart. Each row consists of N strings separated by spaces, these strings representative of guests, the guests were different from each other, contain only letters, no longer than five characters. Two seats on the guest list is intended to guarantee is the same.

Output

Output an integer representing the number of guests is two seating chart arranged in a different order.
 

Sample Input

Input. 1: 
. 3
Frank Billy Sam
Sam Frank Billy

Input 2:
. 5
A B C D E
B C A D E
 

Sample Output

Output 1: 
1

Output 2:
3
 

Data Constraint

1<=N<=100000

analysis

Meaning of the questions is simple: Given a standard, so that you seek another sequence in reverse order of the number of

Sequence is a string, the string to the hash, here I choose map (able to live anyway)

Seeking can then use Fenwick tree classic

 

#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
#include <string>
#define lowbit(x) x&-x
using namespace std;
typedef long long ll;
const int N=1e5+10;
ll t[N],ans;
int a[N];
map<string,int> h;
string c;
int n,cnt;

void Add(int x) {
    for (;x<N;x+=lowbit(x)) t[x]++;
}

ll Sum(int x) {
    ll ans=0;
    for (;x;x-=lowbit(x)) ans+=t[x];
    return ans;
}

int main() {
    scanf("%d",&n);
    for (int i=1;i<=n;i++) {    
        cin>>c;
        h[c]=++cnt;
    }
    for (int i=1;i<=n;i++) {
        cin>>c;
        a[i]=h[c];
    }
    for (int i=n;i;i--)
     ans+=Sum(a[i]),Add(a[i]);
    printf("%lld",ans);
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/mastervan/p/11117918.html