其他分享
首页 > 其他分享> > CCPC2019河北省赛 E.Paper Plane Fly Away

CCPC2019河北省赛 E.Paper Plane Fly Away

作者:互联网

#include<bits/stdc++.h>
using namespace std;
map<int, int>mp; //girl的编号对应的位置号(从左到右1-n)
const int N = 1e5 + 5;
int g[N], l[N], r[N], res[N];
int n;
int lowbit(int x)
{
    return x & (-x);
}
void addl(int x, int k)
{
    while(x <= n)
    {
        l[x] += k;
        x += lowbit(x);
    }
}
void addr(int x, int k)
{
    while(x <= n)
    {
        r[x] += k;
        x += lowbit(x);
    }
}
int queryl(int x)
{
    int res = 0;
    while(x)
    {
        res += l[x];
        x -= lowbit(x);
    }
    return res;
}
int queryr(int x)
{
    int res = 0;
    while(x)
    {
        res += r[x];
        x -= lowbit(x);
    }
    return res;
}
int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        int x;
        cin >> x >> g[i]; //g[i]:第i个男生喜欢的女生编号
        mp[x] = i; //第x号女生对应的位置
    }
    for(int i = 1; i <= n; i++)
    {
        g[i] = mp[g[i]];
        addl(i, 1); //从左到右的树状数组置1
        addr(i, 1); //从右到左的树状数组置1
    }
    for(int i = 1; i <= n; i++)
    {
        int pos = g[i];
        res[i] += queryl(pos-1);
        addl(pos, -1); //该女生已被碰撞则置0
        g[i] = n - pos + 1;
    }
     for(int i = n; i >= 1; i--)
    {
        int pos = g[i];
        res[i] += queryr(pos-1);
        addr(pos, -1); //该女生已被碰撞则置0
    }
    for(int i = 1; i <= n; i++)
        cout << res[i] << endl;
    return 0;
}

标签:Fly,CCPC2019,女生,int,Away,男孩,mp,编号,男生
来源: https://www.cnblogs.com/K2MnO4/p/14800629.html