MooFest POJ - 1990 (树状数组)
Posted zgqblogs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MooFest POJ - 1990 (树状数组)相关的知识,希望对你有一定的参考价值。
Each cow i has an associated "hearing" threshold v(i) (in the range 1..20,000). If a cow moos to cow i, she must use a volume of at least v(i) times the distance between the two cows in order to be heard by cow i. If two cows i and j wish to converse, they must speak at a volume level equal to the distance between them times max(v(i),v(j)).
Suppose each of the N cows is standing in a straight line (each cow at some unique x coordinate in the range 1..20,000), and every pair of cows is carrying on a conversation using the smallest possible volume.
Compute the sum of all the volumes produced by all N(N-1)/2 pairs of mooing cows.
Input
* Lines 2..N+1: Two integers: the volume threshold and x coordinate for a cow. Line 2 represents the first cow; line 3 represents the second cow; and so on. No two cows will stand at the same location.
Output
Sample Input
4 3 1 2 5 2 6 4 3
Sample Output
57
开两个树状数组,一个记录区间坐标和,一个记录区间奶牛数,然后用公式即可算出当前奶牛与之前插入树状数组的奶牛的坐标差之和。
然后把插入过程设定为不耳背的先插入,那么当前奶牛的耳背值就是最大的,用当前奶牛的耳背值乘上坐标差之和,就是答案了。
计算坐标和之差需要分左右,详见代码。
#include<iostream> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<map> #include<set> #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #define fuck(x) cout<<#x<<" = "<<x<<endl; #define ls (t<<1) #define rs ((t<<1)+1) using namespace std; typedef long long ll; typedef unsigned long long ull; const int maxn = 20086; const int inf = 2.1e9; const ll Inf = 999999999999999999; const int mod = 1000000007; const double eps = 1e-6; const double pi = acos(-1); struct node { int v,x; }a[maxn]; int n=20002; bool cmp(node a,node b) { return a.v<b.v; } ll num1[maxn],num2[maxn]; int lowbit(int x) { return x&(-x); } void update(int x) { int t=x; while(x<=n){ num1[x]+=1; num2[x]+=t; x+=lowbit(x); } } ll query1(int x) { ll ans=0; while(x){ ans+=num1[x]; x-=lowbit(x); } return ans; } ll query2(int x) { ll ans=0; while(x){ ans+=num2[x]; x-=lowbit(x); } return ans; } int main() { // ios::sync_with_stdio(false); // freopen("in.txt","r",stdin); int n; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d%d",&a[i].v,&a[i].x); } sort(a+1,a+1+n,cmp); ll ans=0; for(int i=1;i<=n;i++){ ll temp1=query1(a[i].x)*a[i].x-query2(a[i].x-1); // fuck(a[i].x) ll temp2=query2(20001)-query2(a[i].x)-a[i].x*(query1(20001)-query1(a[i].x)); // fuck(temp2) ans+=(temp1+temp2)*a[i].v; update(a[i].x); } printf("%lld ",ans); return 0; }
usingnamespacestd; typedeflonglong ll; typedefunsignedlonglong ull; constint maxn = 20086; constint inf = 2.1e9; const ll Inf = 999999999999999999; constint mod = 1000000007; constdouble eps = 1e-6; constdouble pi = acos(-1); struct node { int v,x; }a[maxn]; int n=20002; bool cmp(node a,node b) { return a.v<b.v; } ll num1[maxn],num2[maxn]; int lowbit(int x) { return x&(-x); } void update(int x) { int t=x; while(x<=n){ num1[x]+=1; num2[x]+=t; x+=lowbit(x); } } ll query1(int x) { ll ans=0; while(x){ ans+=num1[x]; x-=lowbit(x); } return ans; } ll query2(int x) { ll ans=0; while(x){ ans+=num2[x]; x-=lowbit(x); } return ans; } int main() { // ios::sync_with_stdio(false);// freopen("in.txt","r",stdin);int n; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d%d",&a[i].v,&a[i].x); } sort(a+1,a+1+n,cmp); ll ans=0; for(int i=1;i<=n;i++){ ll temp1=query1(a[i].x)*a[i].x-query2(a[i].x-1); // fuck(a[i].x) ll temp2=query2(20001)-query2(a[i].x)-a[i].x*(query1(20001)-query1(a[i].x)); // fuck(temp2) ans+=(temp1+temp2)*a[i].v; update(a[i].x); } printf("%lld ",ans); return0; }
以上是关于MooFest POJ - 1990 (树状数组)的主要内容,如果未能解决你的问题,请参考以下文章