思路:这题的思路很容易想到,把所有时间点离散化,然后按时间一步一步来,当到达时间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++ Name Mangling 为什么不编码返回值参数

    这篇文章主要是推荐下 http://www.cnblogs.com/skynet/archive/2010/09/05/1818636.html 这篇文章从编译器的角度看问题,比较深入. 回到题目,为 ...

  2. UVALive 6910 Cutting Tree(离线逆序并查集)

    [题目]:(地址:) http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97671#problem/E [题意]: 给出多棵树和两类操作:操作 ...

  3. 你应该知道的16个Linux服务器监控命令

    在不同的Linux发行版中,会有不同的GUI程序可以显示各种系统信息,比如SUSE Linux发行版中,就有非常棒的图形化的配置和管理工具YaST,KDE桌面环境里的KDE System Guard也 ...

  4. 经典代码-C宏 #转字符串【瓦特芯 笔记】

    在调试C语言程序时,有时需要打印宏的名字.可以通过定义宏,宏名字的数组来获得. 例如: #include <stdio.h> #define MACRO_STR(x) {x, #x} ty ...

  5. LCA算法

    LCA算法: LCA(Least Common Ancestor),顾名思义,是指在一棵树中,距离两个点最近的两者的公共节点.也就是说,在两个点通往根的道路上,肯定会有公共的节点,我们就是要求找到公共 ...

  6. [转]省市二级联动(纯js实现)

    转至:http://www.jb51.net/article/41556.htm 实现原理: set_city("省名称",市select对象); 判断市select对象是否为空, ...

  7. Autofac介绍

    原帖:http://www.cnblogs.com/xupng/archive/2011/07/12/2104766.html Autofac为何物?它是.NET世界里现存的几种IOC框架其中之一,传 ...

  8. Python if..else

    200 ? "200px" : this.width)!important;} --> 一.介绍 1.完整形式 if <条件判断1>: <执行1> e ...

  9. 下载Xml文件方法

    #region 下载Xml文件方法 //定义委托 private delegate void DownLoadDelegate(string url, string filename); privat ...

  10. C#中动态加载和卸载DLL

    在C++中加载和卸载DLL是一件很容易的事,LoadLibrary和FreeLibrary让你能够轻易的在程序中加载DLL,然后在任何地方卸载.在C#中我们也能使用Assembly.LoadFile实 ...