POJ 1990 Moofest Tree-like array

Source: Internet
Author: User

Title Source: POJ 1990 Moofest

Test instructions: Even if the sound between two cows is 2 the maximum value of the cow v * * The distance between the cows

Idea: Follow v from small to large insert a tree array because from small to large sort each insert a cow I current v maximum is the V of the ox I

Statistics x is smaller than his number S1 x is larger than his number S2 S2 is the number of cattle in the current tree-like array minus S1 in minus one.

Statistic X is smaller than his distance L1 and X greater than his distance L2 L2 is all bull X and-L1

X*S1-L1 is the sum of the distance of all cows and cows I on the left side of the ox I

L2-X*S2 is the sum of all cows and cows I on the right left side of the ox.


#include <cstdio> #include <cstring> #include <algorithm> using namespace std;
const int MAXN = 20010; struct Node {int v, x;}
A[MAXN];
Long Long C[2][MAXN];
int n, m;     
BOOL CMP (Node A, Node B) {return A.V < B.V;} int lowbit (int x) {return x& (-X);
		} void Update (int x, int y, int d) {while (x <= 20000) {c[d][x] + = y;
	x + = Lowbit (x);
	}} long long sum (int x, int d) {int ret = 0;
		while (x > 0) {ret + = c[d][x];
	X-= Lowbit (x);
} return ret;
	} int main () {scanf ("%d", &n);
	for (int i = 1; I <= n; i++) scanf ("%d%d", &AMP;A[I].V, &a[i].x);
	Sort (a+1, a+n+1, CMP);
	A long long ans = 0;
		for (int i = 1; I <= n; i++) {int v = A[I].V;
		int x = a[i].x;
		Long Long S1 = SUM (20000, 0);
		Long Long s2 = SUM (x, 0);
		Long Long L1 = SUM (20000, 1);	
		Long Long L2 = SUM (x, 1);
		Ans + = (x*s2-l2+l1-l2-x* (i-s2-1)) *v;
		Update (a[i].x, 1, 0);
	Update (a[i].x, a[i].x, 1);
	} printf ("%lld\n", ans);
return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.