Moofest Carnival bzoj-3378 Usaco-2004 Open
Topic : Given a sequence of n number, there is a distance between each of the two numbers, the weight between the two points is $max (A[i],a[j]) *dis (I,J) $.
Note : $1\le n\le 2\cdot 10^4$.
idea : The skirt says, this $max$ and $min$ question usually enumerates how much this $max$ and $min$ are.
In that case, we'll sort all the points from the big to the small.
into a tree-like array.
Query direct query can be.
last , with ugly code ...
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std;const int n=20000;int n,num[n+5],cnt;long long sum[n+5],ans,tot;struct node{int v,x;} A[N+5]; BOOL CMP (const node &X,CONST node &y) {return x.v>y.v;} inline int lowbit (int x) {return x& (-X);} void Add (int x,int v,int k) {for (int i=x;i<=n;i+=lowbit (i)) sum[i]+=v,num[i]+=k;} void query (int x) {for (int i=x;i>0;i-=lowbit (i)) tot+=sum[i],cnt+=num[i];} void work () {for (int i=1;i<=n;i++) {tot=cnt=0; Query (a[i].x); ans+=a[i].v* (Cnt*a[i].x-tot); Cnt=-cnt,tot=-tot; Query (N); ans+=a[i].v* (tot-cnt*a[i].x); Add (a[i].x,-a[i].x,-1); } cout << ans << endl;} void Init () {cin >> n; for (int i=1;i<=n;i++) scanf ("%d%d", &a[i].v,&a[i].x); Sort (a+1,a+1+n,cmp); for (int i=1;i<=n;i++) Add (a[i].x,a[i].x,1);} int main () {init (); Work (); return 0;}
Summary : Skirt Sometimes idea good $nb$ Ah! %%%
[bzoj3378] [Usaco2004 Open] Moofest Carnival _ tree-like array