Poj 1990 moofest (tree array)

Source: Internet
Author: User

Reprint please indicate the source, thank you http://blog.csdn.net/ACM_cxlove? Viewmode = Contents
By -- cxlove

A number axis is provided. Some nodes have a cow, each cow has a position Xi, and a hearing value VI. When talking to two cows, the desired sound is the distance * max (Vi, vj) Ask the total sound http://poj.org/problem? Id = 1990
Max (Vi, vj) is easier to process. If you sort all cows in ascending order of VI, consider that the current oX I, and the max (Vi, vj) of the former ox is vi, the rest is to process the sum of distance to create two tree arrays. Count the number of heads of cattle whose coordinate values are less than X, S1, and S2 respectively. It can process the sum of all current cattle X, total. So it doesn't matter to the ox on the left, including yourself, that is, S1 * xi-s2, that is, SIGMA (XI, XJ) j <= I so for the ox on the right, that is (total-s2) -(i-s1) * Xi classic tree Array application, sad
#include<iostream>  #include<cstdio>  #include<map>  #include<cstring>  #include<cmath>  #include<vector>  #include<algorithm>  #include<set>  #include<string>  #include<queue>  #define inf 1600005  #define M 40  #define N 20000#define maxn 300005  #define eps 1e-7#define zero(a) fabs(a)<eps  #define Min(a,b) ((a)<(b)?(a):(b))  #define Max(a,b) ((a)>(b)?(a):(b))  #define pb(a) push_back(a)  #define mp(a,b) make_pair(a,b)  #define mem(a,b) memset(a,b,sizeof(a))  #define LL long long  #define MOD 1000000007#define lson step<<1#define rson step<<1|1#define sqr(a) ((a)*(a))  #define Key_value ch[ch[root][1]][0]  #define test puts("OK");  #define pi acos(-1.0)#define lowbit(x) ((x)&(-(x)))#pragma comment(linker, "/STACK:1024000000,1024000000")  using namespace std;  struct Node{int v,x;bool operator<(const Node n)const{return v<n.v;}}a[N+5];LL cnt[N+5],s[N+5];int n;void Update(LL *b,int x,LL val){    for(int i=x;i<=N;i+=lowbit(i))        b[i]+=val;}LL sum(LL *b,int x){    LL ret=0;    for(int i=x;i>0;i-=lowbit(i))        ret+=b[i];    return ret;}int main(){    //freopen("in.txt","r",stdin);    while(scanf("%d",&n)!=EOF){        for(int i=0;i<n;i++) scanf("%d%d",&a[i].v,&a[i].x);        sort(a,a+n);        mem(cnt,0);        mem(s,0);        LL ans=0,total=0;        for(int i=0;i<n;i++){            Update(cnt,a[i].x,1);            Update(s,a[i].x,a[i].x);            total+=a[i].x;            LL s1=sum(cnt,a[i].x);            LL s2=sum(s,a[i].x);            ans+=a[i].v*(s1*a[i].x-s2-a[i].x*(i+1-s1)+total-s2);        }        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.