思路:这题的思路很容易想到,把所有时间点离散化,然后按时间一步一步来,当到达时间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. HDU 4902 Nice boat (线段树)

    Nice boat 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4902 Description There is an old country a ...

  2. POJ 3237 Tree (树链剖分 路径剖分 线段树的lazy标记)

    题目链接:http://poj.org/problem?id=3237 一棵有边权的树,有3种操作. 树链剖分+线段树lazy标记.lazy为0表示没更新区间或者区间更新了2的倍数次,1表示为更新,每 ...

  3. hdu 4781 Assignment For Princess (2013ACMICPC 成都站 A)

    http://acm.hdu.edu.cn/showproblem.php?pid=4781 由于题目太长,这里就不直接贴了,直接说大意吧. 题目大意:有一个n个点,m条边的有向图,每条边的权值分别为 ...

  4. Proactor设计模式:单线程高并发

    Boost::Asio为同步和异步操作提供了并行支持,异步支持基于前摄器模式,这种模式的优点和缺点可能比只同步或反应器方法要低. 让我们检查一下Boost::Asio是如何实现前摄器模式的,没有引用基 ...

  5. [oracle]一个最简单的oracle存储过程"proc_helloworld"

    1.编写.编写一个最最简单的存储过程,给它起个名字叫做proc_helloworldCREATE OR REPLACE PROCEDURE proc_helloworldISBEGIN   DBMS_ ...

  6. cloudstack 修改显示名称

    http://192.168.153.245:8900/client/api?command=updateVirtualMachineid=922d15e1-9be0-44ac-9494-ce5afc ...

  7. map的两种取值方式

    public class MapUtil{ public static void iteratorMap1(Map m) { Set set=m.keySet();//用接口实例接口 Iterator ...

  8. notepad++ 输入中文无响应

    如果是win7,到用户文件夹 C:\Users\xxxxxxxx\AppData\Roaming\Notepad++ 里面的config.xml 删掉,然后重新打开,应该就可以了,  代价是会删除之前 ...

  9. Codeforces Round #276 (Div. 1) B. Maximum Value 筛倍数

    B. Maximum Value Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/prob ...

  10. 在Web上调用Ocx控件

    原文:http://blog.csdn.net/goodadult2012/article/details/6343369 在HTML页面中使用ActiveX控件包含三个基本操作:将控件放入HTML中 ...