hdu 4325 树状数组+离散化
思路:这题的思路很容易想到,把所有时间点离散化,然后按时间一步一步来,当到达时间i的时候处理所有在i处的查询。
这个代码怎一个挫字了得
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define Maxn 100010
#define lowbit(x) (x&(-x))
using namespace std;
int C[Maxn*],n,ans[Maxn];
struct Flower{
int l,r;
}flower[Maxn];
struct Lisan{
int val,type,l;
int pos;
int operator <(const Lisan &temp) const
{
return val<temp.val;
}
}Index[Maxn*];
struct QT{
int val,i;
int operator <(const QT &temp) const
{
return val<temp.val;
}
}qt[Maxn];
int Sum(int pos)
{
int sum=;
while(pos)
{
sum+=C[pos];
pos-=lowbit(pos);
}
return sum;
}
void update(int pos,int val)
{
while(pos<=n)
{
C[pos]+=val;
pos+=lowbit(pos);
}
}
vector<int> q[Maxn*];
int main()
{
int i,j,m,t,Case=;
scanf("%d",&t);
while(t--)
{
memset(C,,sizeof(C));
scanf("%d%d",&n,&m);
int cnt=;
for(i=;i<=n;i++)
{
scanf("%d%d",&flower[i].l,&flower[i].r);
Index[++cnt].val=flower[i].l,Index[cnt].pos=i,Index[cnt].type=,Index[cnt].l=,Index[++cnt].val=flower[i].r,Index[cnt].type=,Index[cnt].pos=i,Index[cnt].l=;
}
for(i=;i<=m;i++)
{
scanf("%d",&qt[i].val);
qt[i].i=i;
Index[++cnt].val=qt[i].val;
Index[cnt].type=;
Index[cnt].pos=i;
}
sort(Index+,Index++cnt);
//cout<<"ok"<<endl;
int num=;
if(Index[].type==)
{
qt[Index[].pos].val=++num;
}
else
{
if(Index[].l)
flower[Index[].pos].l=++num;
else
flower[Index[].pos].r=++num;
}
for(i=;i<=cnt;i++)
{
if(Index[i].val>Index[i-].val)
{
if(Index[i].type==)
{
qt[Index[i].pos].val=++num;
}
else
{
if(Index[i].l)
flower[Index[i].pos].l=++num;
else
flower[Index[i].pos].r=++num;
}
}
else
if(Index[i].type==)
{
qt[Index[i].pos].val=num;
}
else
{
if(Index[i].l)
flower[Index[i].pos].l=num;
else
flower[Index[i].pos].r=num;
}
}
for(i=;i<=num;i++)
q[i].clear();
for(i=;i<=n;i++)
{
q[flower[i].l].push_back();
q[flower[i].r].push_back(-);
}
sort(qt+,qt+m+);
int r=;
n=num+;
for(i=;i<=num;i++)
{
cnt=;
if(r>m) break;
int size=q[i].size();
for(j=;j<size;j++)
{
update(i,q[i][j]);
if(q[i][j]<)
cnt++;
}
while(qt[r].val==i&&r<=m)
{
ans[qt[r].i]=Sum(qt[r].val)+cnt;
r++;
}
}
printf("Case #%d:\n",++Case);
for(i=;i<=m;i++)
printf("%d\n",ans[i]);
}
return ;
}
这个代码就简洁多了:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define Maxn 100010
#define lowbit(x) (x&(-x))
using namespace std;
int C[Maxn*],n,Index[Maxn*];
struct Node{
int val,pos;
int operator <(const Node &temp) const
{
return val<temp.val;
}
}node[Maxn*];
int Sum(int pos)
{
int sum=;
while(pos)
{
sum+=C[pos];
pos-=lowbit(pos);
}
return sum;
}
void update(int pos,int val)
{
while(pos<=n)
{
C[pos]+=val;
pos+=lowbit(pos);
}
}
int main()
{
int t,m,i,j,Case=;
scanf("%d",&t);
while(t--)
{
memset(C,,sizeof(C));
scanf("%d%d",&n,&m);
int nx=n<<;
int mx=nx+m;
for(i=;i<=mx;i++)
{
scanf("%d",&node[i].val);
node[i].pos=i;
}
sort(node+,node+mx+);
int cnt=;
Index[node[].pos]=++cnt;
for(i=;i<=mx;i++)
{
if(node[i].val==node[i-].val)
Index[node[i].pos]=cnt;
else
Index[node[i].pos]=++cnt;
}
n=cnt+;
for(i=;i<=nx;i++)
{
//cout<<Index[i]<<" * ";
update(Index[i++],);
update(Index[i]+,-);
// cout<<Index[i]+1<<endl;
}
printf("Case #%d:\n",++Case);
for(i=nx+;i<=mx;i++)
{
// cout<<Index[i]<<endl;
printf("%d\n",Sum(Index[i]));
}
}
return ;
}
hdu 4325 树状数组+离散化的更多相关文章
- HDU 1394 树状数组+离散化求逆序数
对于求逆序数问题,学会去利用树状数组进行转换求解方式,是很必要的. 一般来说我们求解逆序数,是在给定一串序列里,用循环的方式找到每一个数之前有多少个比它大的数,算法的时间复杂度为o(n2). 那么我们 ...
- hdu 5792 树状数组+离散化+思维
题目大意: Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a< ...
- [hdu 4417]树状数组+离散化+离线处理
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 把数字离散化,一个查询拆成两个查询,每次查询一个前缀的和.主要问题是这个数组是静态的,如果带修改 ...
- Disharmony Trees HDU - 3015 树状数组+离散化
#include<cstdio> #include<cstring> #include<algorithm> #define ll long long using ...
- Swaps and Inversions HDU - 6318 树状数组+离散化
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> us ...
- C - The Battle of Chibi HDU - 5542 (树状数组+离散化)
Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about ...
- hdu 4638 树状数组 区间内连续区间的个数(尽可能长)
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- hdu 4777 树状数组+合数分解
Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu4605 树状数组+离散化+dfs
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
随机推荐
- get_template_part() 函数详解备忘(转)
最近研究官方主题 Twenty Eleven ,有一些东西网上现成的中文资料不好找,在博客里记载下来,算是分享,也算是备忘,wordpress 3.0 以后就开始便有了get_template_par ...
- 基于EF的数据外键关联查询
现在很多ORM不自带外键关联的实体查询,比如我查询用户,用时将关联的角色信息查询出来,那么就要进行2次查询,很麻烦.而我现在要做的就是基于EF的外键关联查询.很方便的. 首先,创建基础查询的BaseS ...
- PHP linux spl_autoload_register区分大小写
一个PHP脚本用到spl_autoload_register,在WINDOWS下运行正常,但在LINUX就include不了,后来发现WINDOWS大小写不敏感,而在LINUX下区分大小写,WINDO ...
- UML图示
来源: http://www.uml.org.cn/oobject/200901041.asp 近些天学习设计.由于没有具体项目,原有项目也没有什么设计的概念,只是看相关的书籍,所以着实有些抓狂.然最 ...
- iterm2相关配置
使用 iterm2 登陆 公司堡垒机 进行 上传 下载文件 等维护操作.. 1.需要安装iterm2 软件 http://iterm2.com/ 下载安装 2.安装brew ruby -e &quo ...
- TC SRM 665 DIV2 A LuckyXor 暴力
LuckyXorTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description A lucky number is a positive int ...
- c# 自己制作一个简单的项目倒计时器
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- delphi 使用进度条查看浏览器状态
使用进度条查看浏览器状态 procedure TForm1.WebBrowser1ProgressChange(ASender: TObject; Progress, ProgressMax ...
- 使用sql生成UUID
在SQLServer中使用该sql语句可以生成GUID:select cast(NEWID() as varchar(36)) as uuid 通过一下语句将GUID中的'-'字符去掉: select ...
- 框架使用的技术主要是SpringMVC 在此基础上进行扩展
框架使用的技术主要是SpringMVC 在此基础上进行扩展 1 Web前端使用 2 前段控制器采用SpringMVC零配置 3 IOC容器Spring 4 ORM使用 Mybites或者hiberna ...