您的位置:首页 > 大数据 > 人工智能

2014 Multi-University Training Contest 5

2014-08-06 09:21 387 查看
hdu4911

max(逆序数-k,0)

#include <iostream>
#include<stdio.h>
#include<vector>
#include<queue>
#include<stack>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
#define LL __int64
#define gcd(a,b) (b==0?a:gcd(b,a%b))
#define lcm(a,b) (a*b/gcd(a,b))
#define N 100005
#define mod 1000000007
struct node
{
int a,b;
int id;
} p
;
LL lz[N<<2],s[N<<2];
LL sum[N<<2];
int po
;
LL mul(LL a,LL b,LL m)
{
LL d,t;
d=1;
t=a;
while (b>0)
{
if (b%2==1)
d=(d*t)%m;
b/=2;
t=(t*t)%m;
}
return d;
}
bool cmp(node x,node y)
{
return x.a<y.a;
}
bool cmpp(node x,node y)
{
return x.b<y.b;
}
void up(int w)
{
s[w] = (s[w<<1]+s[w<<1|1])%mod;
}
void build(int l,int r,int w)
{
lz[w] = 1;
if(l==r)
{
s[w] = 0;
return ;
}
int m = (l+r)>>1;
build(l,m,w<<1);
build(m+1,r,w<<1|1);
up(w);
}
void down(int w,int m)
{
if(lz[w]!=1)
{
lz[w<<1] = (lz[w<<1]*lz[w])%mod;
lz[w<<1|1] = (lz[w<<1|1]*lz[w])%mod;
s[w<<1] = (s[w<<1]*lz[w])%mod;
s[w<<1|1] = (s[w<<1|1]*lz[w])%mod;
lz[w] = 1;
}
}
void update(int p,LL d,int l,int r,int w)
{
if(l==r)
{
s[w] = d;
return ;
}
down(w,r-l+1);
int m = (l+r)>>1;
if(p<=m)
update(p,d,l,m,w<<1);
else
update(p,d,m+1,r,w<<1|1);
up(w);
}
void update1(int a,int b,LL d,int l,int r,int w)
{
if(a<=l&&b>=r)
{
s[w] = (s[w]*d)%mod;
lz[w] = (lz[w]*d)%mod;
return ;
}
down(w,r-l+1);
int m = (l+r)>>1;
if(a<=m) update1(a,b,d,l,m,w<<1);
if(b>m) update1(a,b,d,m+1,r,w<<1|1);
up(w);
}
LL query(int a,int b,int l,int r,int w)
{
if(a<=l&&b>=r) return s[w];
int m = (l+r)>>1;
LL res = 0;
if(a<=m) res+=query(a,b,l,m,w<<1);
if(b>m) res = (res+query(a,b,m+1,r,w<<1|1))%mod;
return res;
}
void up1(int w)
{
sum[w] = sum[w<<1]+sum[w<<1|1];
}
void build1(int l,int r,int w)
{
if(l==r)
{
sum[l] = 0;
return ;
}
int m = (l+r)>>1;
build1(l,m,w<<1);
build1(m+1,r,w<<1|1);
up1(w);
}
void update2(int p,int l,int r,int w)
{
if(l==r)
{
sum[w] = 1;
return ;
}
int m = (l+r)>>1;
if(p<=m)
update2(p,l,m,w<<1);
else update2(p,m+1,r,w<<1|1);
up1(w);
}
int query1(int a,int b,int l,int r,int w)
{
if(a<=l&&b>=r)
{
return sum[w];
}
int m = (l+r)>>1;
int res=0;
if(a<=m) res+=query1(a,b,l,m,w<<1);
if(b>m) res+=query1(a,b,m+1,r,w<<1|1);
return res;
}
int main()
{
int n,i;
while(scanf("%d",&n)!=EOF)
{
memset(sum,0,sizeof(sum));
for(i = 1; i<= n; i++)
{
scanf("%d%d",&p[i].a,&p[i].b);
p[i].id = i;
}
sort(p+1,p+n+1,cmp);
for(i = 1; i <= n ; i++)
{
po[p[i].id] = i;
}
sort(p+1,p+n+1,cmpp);
build(1,n,1);
LL ans = 0;
build1(1,n,1);
for(i = 1; i <= n; i++)
{
int k = query1(1,po[p[i].id],1,n,1);
//cout<<k<<" "<<p[i].a<<" "<<po[p[i].id]<<endl;
update2(po[p[i].id],1,n,1);
update(po[p[i].id],mul(2,p[i].a+k,mod),1,n,1);
LL ss = query(po[p[i].id],n,1,n,1);
// cout<<ss<<endl;
ans=(ans+(mul(3,p[i].b,mod)*ss)%mod)%mod;
if(po[p[i].id]<n)
update1(po[p[i].id]+1,n,2,1,n,1);
}
printf("%I64d\n",ans);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: