H1. Maximum Crossings (Easy Version)

思路:怎么才能有相交的点产生? 对于每一个段(从一到n)如果在此之前有大于等于a[i]的点,就说明可以产生相交的点(因为上面的段是递增的,而只要保证在a[i]后面有点就可以相交),那么就用树状数组循环一遍,每次先res加上前n项的点的数量-a[i]-1前面的点存在的数量,再在a[i]位置++。

代码

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃  	    ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/

#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long 
using namespace std;

const int N=1000000+100;
int n ,m,h;
int  s[N],a[N];


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

void add(int x)
{
	for(int i =x;i<=n;i+=lowbit(i)){
	s[i]++;
	}

}

int get(int x)
{
	int res=0;
	for(int i=x;i;i-=lowbit(i))
	{
		res+=s[i];
	}
	return res;
}


int main()
{
	int t;
	sc_int(t);
	while(t--)
	{
		cin>>n;
		for(int i =1;i<=n;i++)
		sc_int(a[i]);
		memset(s,0,sizeof s);

		int res=0;
		for(int i =1;i<=n;i++)
		{
		
			res+=get(n)-get(a[i]-1);	
			// cout<<"res="<<res<<endl;		
			add(a[i]);

		}
		printf("%d\n",res);
	}
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值