标签:ber ring car fun isp vector ios eps tle
Input
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\n",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\n",ans); return0; }
标签:ber ring car fun isp vector ios eps tle
原文地址:https://www.cnblogs.com/ZGQblogs/p/10495005.html