Codeforces 61E Enemy is weak 乞讨i<j<k && a[i]>a[j]>a[k] 对数的 树阵

主题链接:点击打开链接

意大利正在寻求称号 i<j<k && a[i]>a[j]>a[k] 的对数

假设仅仅有2元组那就是求逆序数的做法

三元组的话就用一个树状数组x表示 数字i前面有多少个比自己大的个数

然后每次给这个y数组求和,再把x中>a[i]的个数存入y中就可以

#include <algorithm>
#include <cctype>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <climits>
#include <vector>
#include<iostream>
using namespace std;
#define ll long long
inline void rd(int &ret)
{
	char c;
	do { c = getchar();
	} while(c < '0' || c > '9');
	ret = c - '0';
	while((c=getchar()) >= '0' && c <= '9')
		ret = ret * 10 + ( c - '0' );
}
#define N 1000005
#define eps 1e-8
#define inf 1000000

ll n;
struct node{
	ll c[N];
	inline ll lowbit(ll x){return x&-x;}
	void init(){memset(c, 0, sizeof c);}
	ll sum(ll x){
		ll ans = 0;
		while(x<=n+10)
			ans += c[x], x+=lowbit(x);
		return ans;
	}
	void change(ll x, ll y){
		while(x)
			c[x] +=y, x-=lowbit(x);
	}
}x, y;
int haifei[1000000], panting[1000000];
int main()
{
	ll i, j;
	while(cin>>n)
	{
		ll ans = 0;
		for(i = 0; i < n; i++)rd(haifei[i]), panting[i] = haifei[i];
		x.init(); y.init();
		sort(haifei, haifei+n);
		for(i = 0; i < n; i++)
		{
			ll b = (lower_bound(haifei, haifei+n, panting[i]) - haifei) +1;

			ll siz = y.sum(b);
			ans += siz;
			y.change(b, x.sum(b));
			x.change(b, 1);
		}
		cout<<ans<<endl;
	}
	return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

时间: 2024-10-27 11:03:32

Codeforces 61E Enemy is weak 乞讨i&lt;j&lt;k &amp;&amp; a[i]&gt;a[j]&gt;a[k] 对数的 树阵的相关文章

CodeForces 61E Enemy is weak 求i&lt;j&lt;k &amp;&amp; a[i]&gt;a[j]&gt;a[k] 的对数 树状数组

题目链接:点击打开链接 题意是求 i<j<k && a[i]>a[j]>a[k] 的对数 如果只有2元组那就是求逆序数的做法 三元组的话就用一个树状数组x表示 数字i前面有多少个比自己大的个数 然后每次给这个y数组求和,再把x中>a[i]的个数存入y中即可 #include <algorithm> #include <cctype> #include <cassert> #include <cstdio> #in

三元逆序对 求i&lt;j&lt;k &amp;&amp; a[i]&gt;a[j]&gt;a[k] 的对数 树状数组Codeforces 61E Enemy is weak

http://codeforces.com/problemset/problem/61/E E. Enemy is weak time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output The Romans have attacked again. This time they are much more than the Persian

CodeForces - 61E Enemy is weak

Description The Romans have attacked again. This time they are much more than the Persians but Shapur is ready to defeat them. He says: "A lion is never afraid of a hundred sheep". Nevertheless Shapur has to find weaknesses in the Roman army to

Codeforces 741B Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses

[题目链接] http://codeforces.com/problemset/problem/741/B [题目大意] 给出一张图,所有连通块构成分组,每个点有价值和代价, 要么选择整个连通块,要么只能在连通块中选择一个,或者不选,为最大价值 [题解] 首先我们用并查集求出连通块,然后对连通块进行分组背包即可. [代码] #include <cstdio> #include <vector> #include <algorithm> #include <cstr

codeforces 742D Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses ——(01背包变形)

题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #include <vector> 5 using namespace std; 6 const int N = 1000 + 5;

cf 61 E. Enemy is weak 离散化+树状数组

题意: 给出一个数组,数组的每一个元素都是不一样的,求出对于3个数组下标 i, j, k such that i < j < k and ai > aj > ak where ax is the value at position x. 的个数 明显数组的值太大了 先离散化,然后就是简单的树状数组了 对于每一个i,只要统计i前面的数中比a[i]大的数的个数,和i后面的数中比a[i]小的数的个数即可 #include <cstdio> #include <cstri

Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses CodeForces - 742D

Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship grou

hust 1027 Enemy Target!

题目描述 In the Game Red Alert, a group of soviet infantry marches towards our base. And we have N Prism Tanks to defend our base. Suppose the coming infantry marches in a ROW*COLUMN rectangle grid, and keeps the shape unchanged. A Prism Tank can elimina

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th