hdu3015,poj1990树状数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3015
题意:给定n组数,每组数有值x和值h,求n组数两两的val的总和。将所有x和所有h分别离散化(不去重)变成x'和h',val(i,j)为abs(x'i-x'j)*min(hi',hj')。
如:
x, h——>x',h'
10,100——>1,1
50,500——>4,4
20,200——>3,3
20,100——>1,1
思路:只要把n*n优化成n*logn就可以过。
tip1:按照h'的值sort,并动态加点,每次加的点如果是已加点中h'最小的,那么min()中要取的值就为h'
tip2:离散化但是不去重,不需要unique
tip3:关于abs()部分的处理,两棵树状数组,一棵统计数量,一棵统计sum。abs()值为(大于x'的sum-大于x'的数量*x')+(小于x'的数量*x'-小于x'的sum)
tip4:res和sum树状数组均需要开long long
附上代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1e5+;
int C1[maxn],C2[maxn],n;
int lowbit(int x)
{
return x&-x;
}
void add1(int x,int c)
{
for(;x<=n;x+=lowbit(x))
C1[x]+=c;
}
void add2(int x,int c)
{
for(;x<=n;x+=lowbit(x))
C2[x]+=c;
}
int query1(int x)
{
int res=;
for(;x;x-=lowbit(x))
res+=C1[x];
return res;
};
long long query2(int x)
{
long long res=;
for(;x;x-=lowbit(x))
res+=C2[x];
return res;
}
struct node
{
int x,h;
}p[maxn];
bool cmp(node a,node b)
{
if(a.h==b.h)return a.x>b.x;
else return a.h>b.h;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(C1,,sizeof C1);
memset(C2,,sizeof C2);
int x[maxn]={},h[maxn]={};
for(int i=;i<n;i++)scanf("%d%d",&p[i].x,&p[i].h),x[i]=p[i].x,h[i]=p[i].h;
sort(x,x+n);
sort(h,h+n);
sort(p,p+n,cmp);
long long res=;
for(int i=;i<n;i++)
{
int tmpx=lower_bound(x,x+n,p[i].x)-x+;
int tmph=lower_bound(h,h+n,p[i].h)-h+;
//cout<<tmpx<<" "<<tmph<<endl;
res+=1ll*tmph*(-1ll*tmpx*(query1(n)-query1(tmpx))+1ll*tmpx*query1(tmpx-)+query2(n)-query2(tmpx)-query2(tmpx-));
add1(tmpx,);
add2(tmpx,tmpx);
}
printf("%lld\n",res);
}
return ;
}
poj1990,一道类似的题
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=2e4+;
int n,C1[maxn],C2[maxn],maxp;
struct node
{
int v,place;
}p[maxn];
bool cmp(node a,node b)
{
if(a.v==b.v)return a.place>b.place;
else return a.v>b.v;
}
int lowbit(int x)
{
return x&-x;
}
void add1(int x,int c)
{
for(;x<=maxp;x+=lowbit(x))
C1[x]+=c;
}
void add2(int x,int c)
{
for(;x<=maxp;x+=lowbit(x))
C2[x]+=c;
}
int query1(int x)
{
int res=;
for(;x;x-=lowbit(x))
res+=C1[x];
return res;
}
long long query2(int x)
{
long long res=;
for(;x;x-=lowbit(x))
res+=C2[x];
return res;
}
int main()
{
scanf("%d",&n);
for(int i=;i<n;i++)scanf("%d%d",&p[i].v,&p[i].place),maxp=max(maxp,p[i].place);
sort(p,p+n,cmp);
long long res=;
for(int i=n-;i>=;i--)
{
//cout<<p[i].v<<" "<<p[i].place<<endl;
res+=1ll*p[i].v*(query2(maxp)-query2(p[i].place)-p[i].place*(query1(maxp)-query1(p[i].place))+p[i].place*query1(p[i].place-)-query2(p[i].place-));
add1(p[i].place,);
add2(p[i].place,p[i].place);
//cout<<res<<endl;
}
printf("%lld\n",res);
return ;
}
hdu3015,poj1990树状数组的更多相关文章
- poj1990树状数组
Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a social gather ...
- hdu3015树状数组 poj1990的离散化版本
都是一类题目,推导调试比较烦,想出来还是不难的 /* 给定n个点对,按一维升序排序一次,每个点的序号为Di,按二维升序排序一次,每个点的序号为Hi 求sum{w(i,j)} w(i,j)=abs(Di ...
- POJ-1990 MooFest---两个树状数组
题目链接: https://vjudge.net/problem/POJ-1990 题目大意: 一群牛参加完牛的节日后都有了不同程度的耳聋,第i头牛听见别人的讲话,别人的音量必须大于v[i],当两头牛 ...
- poj1990两个树状数组
垃圾poj交不上去 /* 按权值从小到大排序, 两个树状数组维护权值小于等于并且在i左边的点的个数和权值 */ #include<iostream> #include<cstring ...
- POJ_1990 MooFest 【树状数组】
一.题面 POJ1990 二.分析 一个简单的树状数组运用.首先要把样例分析清楚,凑出57,理解一下.然后可以发现,如果每次取最大的v就可以肆无忌惮的直接去乘以坐标差值就可以了,写代码的时候是反着来的 ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- bzoj1878--离线+树状数组
这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
随机推荐
- jquery微信浏览器阻止页面拖动
jquery微信浏览器阻止页面拖动<pre>function bodyScroll(event) { event.preventDefault();} document.body.addE ...
- Geotools求shapefile路网中任意两点之间最短路径的距离
前言:之前在博问求助过这个问题.经过几天的思考,算是解决了(但仍有不足),另一方面对Geotools不是很熟,有些描述可能不正确,希望大家批评指正. 问题:作为一个新手,我并没有发现Geotools中 ...
- java基础阶段几个面试题
1.说出你对面向对象的理解 在我理解,面向对象是向现实世界模型的自然延伸,这是一种“万物皆对象”的编程思想.在现实生活中的任何物体都可以归为一类事物,而每一个个体都是一类事物的实例.面向对象的编程是以 ...
- jquery正确获取iframe里元素的方法
<iframe id="_ae_frame" width="100%" height="100%" frameborder=" ...
- 结合参数接收响应转换原理讲解SpringBoot常用注解
一.常用注解回顾 1.1 @RequestBody与@ResponseBody //注意并不要求@RequestBody与@ResponseBody成对使用. public @ResponseBody ...
- vue-create 报错 command failed: yarn --registry=https://registry.npm.taobao.org --disturl=https://npm.taobao.org/dist 完美解决方案
@vue/cli 3.x 创建项目失败解决方案 报错信息 command failed: yarn --registry=https://registry.npm.taobao.org --distu ...
- 学习Java第一步:安装Intellij IDEA和JDK
注:其实真正学习一门新语言的第一步并不是安装开发工具,我是C#转JAVA,有一点编程经验了,所以可以直接跳过前面几步,直接上IDE. 1.下载IntelliJ IDEA [官网] http://www ...
- spark集群搭建(三台虚拟机)——spark集群搭建(5)
!!!该系列使用三台虚拟机搭建一个完整的spark集群,集群环境如下: virtualBox5.2.Ubuntu14.04.securecrt7.3.6_x64英文版(连接虚拟机) jdk1.7.0. ...
- 读取JDK API文档,并根据单词出现频率排序
1,拿到 API 文档 登录 https://docs.oracle.com/javase/8/docs/api/ , 选中特定的类,然后 copy 其中的内容, 放入 TXT 文件中 , 2,读取T ...
- <meta name="viewport" content="width=device-width,initial-scale=1.0">的意思
content属性值 : width:可视区域的宽度,值可为数字或关键词device-width height同理width intial-scale:页面首次被显示是可 ...