思路:这题的思路很容易想到,把所有时间点离散化,然后按时间一步一步来,当到达时间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. Shell字符串使用十进制转换

    其实不知道该起什么题目. 先说下需求,线上的log是按照五分钟为粒度切分的,即每五分钟产生一个文件,19:04的log写入到 1900结尾的log文件中,19:05写入到1905结尾的log文件中. ...

  2. thymeleaf中的模板布局

    一.包括模板片段: 1:定义和引用片段,我们经常会想要包含在模板片段来自其他模板.常见的用途是页脚.标题.菜单…; 为了做到这一点,Thymeleaf需要我们定义包含可用的片段,我们可以通过使用th: ...

  3. Codeforces Round #271 (Div. 2) D. Flowers (递推)

    题目链接:http://codeforces.com/problemset/problem/474/D 用RW组成字符串,要求w的个数要k个连续出现,R任意,问字符串长度为[a, b]时,字符串的种类 ...

  4. 关于名称重整(name mangling)、多态性的一些简单介绍

    在看GCC源码的时候看到mangles这个单词,于是google了一下. 在面向对象编程语言出现之前,如果你想要打印不同类型的数据,需要写多个方法,例如PrintInteger(int i),Prin ...

  5. TypeScript学习笔记(七):模块

    JavaScript中的模块 在学习TypeScript的模块之前我们先看看在JavaScript中的模块是如何实现的. 模块的好处 首先我们要了解使用模块的好处都有什么? 模块化.可重用: 封装变量 ...

  6. IoC/DI

    From:http://jinnianshilongnian.iteye.com/blog/1471944 我对IoC/DI的理解 博客分类: spring杂谈 IoCDI  IoC IoC: Inv ...

  7. appcompat_v7/res/values-v21/themes_base.xml No resource found that matches the given name

    今天晕死了 将工作区里的appcompat_v7删除掉了, 然后随意新建了一个工程,因为已经升级到5.0了,appcompat_v7内容有所改变, 以前的工程引用旧的appcompat_v7的某些属性 ...

  8. 使用Unity制作游戏关卡的教程(一)

    转自: http://gamerboom.com/archives/74131 作者:Matthias Zarzecki 我正在制作<Looking For Group – The Fork O ...

  9. sessionapplicationStruts2中访问web元素

    本文是一篇关于sessionapplication的帖子 取得Map类型request,session,application,实在类型 HttpServletRequest, HttpSession ...

  10. Codeforces Gym 100114 A. Hanoi tower 找规律

    A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...