思路:这题的思路很容易想到,把所有时间点离散化,然后按时间一步一步来,当到达时间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 树状数组+离散化的更多相关文章

  1. HDU 1394 树状数组+离散化求逆序数

    对于求逆序数问题,学会去利用树状数组进行转换求解方式,是很必要的. 一般来说我们求解逆序数,是在给定一串序列里,用循环的方式找到每一个数之前有多少个比它大的数,算法的时间复杂度为o(n2). 那么我们 ...

  2. hdu 5792 树状数组+离散化+思维

    题目大意: Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a< ...

  3. [hdu 4417]树状数组+离散化+离线处理

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 把数字离散化,一个查询拆成两个查询,每次查询一个前缀的和.主要问题是这个数组是静态的,如果带修改 ...

  4. Disharmony Trees HDU - 3015 树状数组+离散化

    #include<cstdio> #include<cstring> #include<algorithm> #define ll long long using ...

  5. Swaps and Inversions HDU - 6318 树状数组+离散化

    #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> us ...

  6. 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 ...

  7. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  8. hdu 4777 树状数组+合数分解

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  9. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

随机推荐

  1. 用C语言实现ipv4地址字符串是否合法

    用程序实现ipv4地址字符串是否合法,主要考察的是C字符串的操作. 搜索了下,网上没有特别好的实现,自己实现了下,见笑于大家,请指正. #include <stdio.h> #includ ...

  2. Android实例-获取程序版本号(XE10+小米2)

    相关资料: 383675978群号 实例源码: unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, Sy ...

  3. BestCoder Round #69 (div.2) Baby Ming and Weight lifting(hdu 5610)

    Baby Ming and Weight lifting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  4. [C语言 - 6] static & extern

    A. extern函数 一个c文件生成一个obj文件   外部函数:允许其他文件访问.调用的函数(默认函数为外部函数),不允许存在同名的外部函数   my.c //define a extern fu ...

  5. 微软企业库5.0学习-Security.Cryptography模块

    一.微软企业库加密应用模块提供了两种加密: 1.Hash providers :离散加密,即数据加密后无法解密 2.Symmetric Cryptography Providers:密钥(对称)加密法 ...

  6. ASP.NET网站中设置404自定义错误页面

    在用ASP.NET WebForm开发一个网站时,需要自定义404错误页面. 做法是这样的 在网站根目录下建立了一个404.html的错误页面,然后在Global.asax文件中,加入如下代码: &l ...

  7. POJ 3177 Redundant Paths(强连通分量)

    题目链接:http://poj.org/problem?id=3177 题目大意是一个无向图给你n个点m条边,让你求出最少加多少条边 可以让任意两个点相通两条及以上的路线(每条路线点可以重复,但是每条 ...

  8. PHP实现基于Swoole简单的HTTP服务器

    引用Swoole官方定义: PHP语言的异步.并行.高性能网络通信框架,使用纯C语言编写,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,数据库连接池,AsyncTa ...

  9. Python3爬取百度百科(配合PHP)

    用PHP写了一个网页,可以获取百度百科词条.源代码已分享至github:https://github.com/1049451037/xiaobaike/tree/master 那么通过Python来爬 ...

  10. 在Android项目中使用AndroidAnnotations(配置框架,显示Hello World!)

    使用这个框架可以极大的简化在开发Android过程中的代码.提高开发的效率.这里简单说一下配置方式.和使用办法. 项目的地址为:AndroidAnnotations Jar包下载地址:3.0.1 下载 ...