树状数组 初探 - BenFromHRBUST

洛谷 P1908 逆序对

题目描述

猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游戏,现在他们喜欢玩统计。最近,TOM老猫查阅到一个人类称之为“逆序对”的东西,这东西是这样定义的:对于给定的一段正整数序列,逆序对就是序列中ai>aj且i<j的有序对。知道这概念后,他们就比赛谁先算出给定的一段正整数序列中逆序对的数目。
Update:数据已加强。

输入格式

第一行,一个数n,表示序列中有n个数。
第二行n个数,表示给定的序列。序列中每个数字不超过109

输出格式

给定序列中逆序对的数目。

输入输出样例

输入 #1
6
5 4 2 6 3 1
输出 #1
11

说明/提示

对于25%的数据,n≤2500
对于50%的数据,n≤4×104
对于所有数据,n≤5×105
请使用较快的输入输出


代码

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N=5e5+5;

int n;
ll a[N];
ll tree[N];

vector<int> G;

int lowbit(int x)
{
    return x&(-x);
}

int getid(ll x)
{
    return lower_bound(G.begin(),G.end(),x)-G.begin()+1;
}

//单点更新,x为更新的位置,y为更新后的数
void add(int x,int y){
    for(int i=x;i<=n;i+=lowbit(i))
        tree[i]+=y;
}

//区间查询,查询1到x的和
int getsum(int x){
    int ans = 0;
    for(int i=x;i;i-=lowbit(i))
        ans+=tree[i];
    return ans;
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        G.push_back(a[i]);
    }
    sort(G.begin(),G.end());
    ll ans=0;
    for(int i=n;i>=1;i--)
    {
        int id=getid(a[i]);
        add(id,1);
        ans+=getsum(id-1);
    }
    printf("%lld\n",ans);
    return 0;
}


652D Nested Segments(Educational Codeforces Round 10)

Problem Description

You are given n segments on a line. There are no ends of some segments that coincide. For each segment find the number of segments it contains.

Input

The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of segments on a line.
Each of the next n lines contains two integers li and ri ( - 109 ≤ li < ri ≤ 109) — the coordinates of the left and the right ends of the i-th segment. It is guaranteed that there are no ends of some segments that coincide.

Output

Print n lines. The j-th of them should contain the only integer aj — the number of segments contained in the j-th segment.

Examples

input
4
1 8
2 3
4 7
5 6
output
3
0
1
0

input
3
3 4
1 5
2 6
output
0
1
1


代码

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N=2e5+5;

int n;
int tree[2*N];
int ans[N];
vector<int> G;

struct S
{
    int x,y;
    int pos;
}s[N];

bool cmp(S a,S b)
{
    if(a.x==b.x)
        return a.y>b.y;
    return a.x>b.x;
}

int lowbit(int x)
{
    return x&(-x);
}

int getid(ll x)
{
    return lower_bound(G.begin(),G.end(),x)-G.begin()+1;
}

//单点更新,x为更新的位置,y为更新后的数
void add(int x,int y){
    for(int i=x;i<=2*n;i+=lowbit(i))
        tree[i]+=y;
}

//区间查询,查询1到x的和
int getsum(int x){
    int ans=0;
    for(int i=x;i;i-=lowbit(i))
        ans+=tree[i];
    return ans;
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&s[i].x,&s[i].y);
        s[i].pos=i;
        G.push_back(s[i].x);
        G.push_back(s[i].y);
    }
    sort(G.begin(),G.end());
    sort(s+1,s+n+1,cmp);
    for(int i=1;i<=n;i++)
    {
        int id=getid(s[i].y);
        ans[s[i].pos]=getsum(id);
        add(id,1);
    }
//    for(int i=1;i<=n;i++)
//    {
//        cout<<"test"<<tree[i]<<endl;
//    }
    for(int i=1;i<=n;i++)
        printf("%d\n",ans[i]);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值