[XJOI] Burning Crusade

Topic Link

The Burning Crusade

Subject description:

In order to resist Torun star's attack, Hammerhead's parent company, decadent interstellar trust GTD's CEO TS sent expeditionary security main fleet, and the selection of an elite squad of GTD go forward operating field. According to CEO learned intelligence, Torun star's leader - called the beast • Young has called a "shock" terrible skills, this skill can kill in an instant set of people. Thankfully, GTD public belief in two gods: C Z God and God. C for God's special loyalty can place the resurrection after death, belief in the firm, the more full recovery state after the resurrection; Z for God's special loyalty to take advantage of the high-decibel noise • Young called the beast attacked, the more steadfast faith, The higher damage output.
In God bless C and Z God, GTD elite squad of Torun star who launched a war.
Whether this Burning Crusade will achieve the final victory by the GTD it? Or because the ability terrible beast called • Young, the end of response rang forever, this war will eventually lasted for thousands of years it?

All members of the elite squad (total N name), CEO in accordance with them two C ratings on God and God Z loyalty. Most loyal score C God \ (N \) , second loyalty is \ (N-1 \) ...... most disloyal score of 1. Z degree of loyalty to God also scored in accordance with such standards.
CEO decided to pick two people as the vanguard of the beast called • Young attack. In order to ensure the balance of the team, CEO requires members of the two teams meet such conditions should be: If a member of loyalty to God C higher than another, then his loyalty to God Z must be lower than the other one; if one member of loyalty to God C lower than another, then his loyalty to Z of God must be higher than another.
Now, CEO hoping to find a number of possible options there are.

Input formats:

File input. Enter the file name tbc.in
input of the first acts of \ (\ N) , that is, the total number of team members.
The next \ (N \) rows, each row represents the number of two for each member of the loyalty - the first number of loyalty God C, the second number of loyalty Z God.

Output formats:

File output. The output file name tbc.out
output line only, a total of two qualifying portfolio.

Sample input:

5
5 4
3 2
4 3
1 5
2 1

Sample output:

4

data range:

50% of the data, (N≤1000 \) \
Data for 100%, \ (N≤100000 \)

time limit:

2s

prompt:

The possible combinations are:
first (54) and fourth (15)
second (32) and fourth (15)
third (43) and fourth (15)
fourth (15) and fifth (21)

[Special thanks]
• Joe Haldeman: "Millennium War"
blizzard: mountain pass
Ural: acm.timus.ru
GTD: Everyone

answer

Meaning of the title is \ (N \) individuals, each person has two values, the first time being set these two numbers is \ (x \) and \ (y \)
asked to find two people so \ (x_1> x_2 \ ) and \ (Y_1 <y_2 \) ,
is not that the number of reverse seeking to do.
As we all know, we can put everyone in accordance with \ (the X-\) , from small to large, and then seek to reverse with msort number.
How to find reverse with msort I will not explain, we can look at the code to understand.
Note that \ (N \) the range is \ (N≤100000 \) , and the number of the reverse stage is the number of \ (N ^ 2 \) , so \ (ANS \) need \ (long long \) was stored under.
Time complexity is \ (O (NlogN) \)
msort seeking reverse order:

#include<bits/stdc++.h>
using namespace std;
int n,x,y;
long long ans;
int a[100009];
void msort(int l,int r){
    if(l==r) return;
    int mid=(l+r)/2;
    msort(l,mid);
    msort(mid+1,r);
    int j=l,i=mid+1;
    while(j<=mid || i<=r){
        while(j>mid && i<=r)i++;
        if(j>mid && i>r) break;
        while(j<=mid && i>r)j++;
        if(j>mid && i>r) break;
        if(a[j]<=a[i]) j++;
        else{
            ans+=mid-j+1;
            i++;
        }
    }
    sort(a+l,a+r+1);
}
int main(){
    scanf("%d",&n);
    for(int j=1;j<=n;j++){
        scanf("%d%d",&x,&y);
        a[x]=y;
    }
    msort(1,n);
    printf("%lld",ans);
    return 0;
}

In fact, the methods seek to reverse more than a useful msort seeking this kind,
the use of Fenwick tree we can also seek to achieve reverse right.
We can put everyone in accordance with \ (x \) in descending order, (descending here for convenience in mind when adding a few back, if here from small to large, can be followed backwards added to each number)
us from 1 to \ (N \) were added sequentially to each person,
each array us on the tree \ (a [Y] ++ \) ,
prior to the addition to each number we ask \ (y-1 \) prefix and added \ (ans \) inside.
Fenwick tree seeking to reverse time complexity is \ (O (NlogN) \)
Fenwick tree seeking to reverse:

#include<bits/stdc++.h>
using namespace std;
int n,x,y;
long long ans;
struct aa{
    int x,y;
}a[100009];
int p[400009];
bool cmp(aa x,aa y){
    return x.x>y.x;
}
int lb(int x){
    return x&(-x);
}
void add(int x){
    int u=x;
    while(u<=400000){
        p[u]++;
        u+=lb(u);
    }
}
int fd(int x){
    int ans=0;
    while(x){
        ans+=p[x];
        x-=lb(x);
    }
    return ans;
}
int main(){
    scanf("%d",&n);
    for(int j=1;j<=n;j++)
        scanf("%d%d",&a[j].x,&a[j].y);
    sort(a+1,a+n+1,cmp);
    for(int j=1;j<=n;j++){
        ans+=fd(a[j].y);
        add(a[j].y);
    }
    printf("%lld",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/linjiale/p/11317703.html