lightoj 1089 【离散化+线段树】
题意:
给你n个区间,然后给你m个i点问你这个点在几个所给定的区间里;
思路:
离散化+区间覆盖
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=5e4+10; struct SegT{
int left,right;
int val;
int flag;
};
SegT q[N*12];
int n,Q;
int arr[N*4]; void Build(int num,int L,int R)
{
q[num].left=L;
q[num].right=R;
q[num].flag=q[num].val=0;
if(L==R)
return;
int mid=(L+R)/2;
Build(2*num, L,mid);
Build(2*num+1,mid+1,R);
} void Pushdown(int num)
{
if(q[num].flag)
{
q[2*num].val+=(q[2*num].right-q[2*num].left+1)*q[num].flag;
q[2*num+1].val+=(q[2*num+1].right-q[2*num+1].left+1)*q[num].flag;
q[2*num].flag+=q[num].flag;
q[2*num+1].flag+=q[num].flag;
q[num].flag=0;
}
} void Update(int num,int s,int t)
{
if(q[num].left>=s&&q[num].right<=t)
{
q[num].flag+=1;
q[num].val+=(q[num].right-q[num].left+1); //wa在了这里。。只要加1就行了,无奈给乘了。。。q[num].flag...不过还是有长进///
return;
}
Pushdown(num);
int mid=(q[num].left+q[num].right)/2;
if(mid>=t)
Update(2*num,s,t);
else if(mid<s)
Update(2*num+1,s,t);
else
{
Update(2*num,s,mid);
Update(2*num+1,mid+1,t);
}
q[num].val=q[2*num].val+q[2*num+1].val;
} int query(int num,int id)
{
if(q[num].left==id&&q[num].left==q[num].right)
return q[num].val;
Pushdown(num);
int mid=(q[num].right+q[num].left)/2;
if(mid>=id)
return query(2*num,id);
else
return query(2*num+1,id);
} vector<int>xs;
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&Q);
xs.clear();
for(int i=1;i<=2*n;i+=2)
{
scanf("%d%d",&arr[i],&arr[i+1]);
xs.push_back(arr[i]);
xs.push_back(arr[i+1]);
}
for(int i=1;i<=Q;i++)
{
scanf("%d",&arr[i+2*n]);
xs.push_back(arr[i+2*n]);
}
int num=0;
sort(xs.begin(),xs.end());
vector<int>::iterator e=unique(xs.begin(),xs.end());
for(int i=1;i<=2*n+Q;i++)
{
arr[i]=lower_bound(xs.begin(),e,arr[i])-xs.begin()+1;
num=max(num,arr[i]);
}
Build(1,1,num); for(int i=1;i<=2*n;i+=2)
Update(1,arr[i],arr[i+1]); printf("Case %d:\n",cas++);
for(int i=1;i<=Q;i++)
printf("%d\n",query(1,arr[i+2*n]));
}
return 0;
} /*
5 4
6 12
8 8
10 12
8 11
0 12
11
12
2
20 3 3
1 3
2 4
5 6
1
2
3
*/
lightoj 1089 【离散化+线段树】的更多相关文章
- 南阳理工 题目9:posters(离散化+线段树)
posters 时间限制:1000 ms | 内存限制:65535 KB 难度:6 描述 The citizens of Bytetown, AB, could not stand that ...
- SGU 180 Inversions(离散化 + 线段树求逆序对)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...
- 【POJ】2528 Mayor's posters ——离散化+线段树
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Description The citizens of Bytetown, A ...
- hpu校赛--雪人的高度(离散化线段树)
1721: 感恩节KK专场——雪人的高度 时间限制: 1 Sec 内存限制: 128 MB 提交: 81 解决: 35 [提交][状态][讨论版] 题目描述 大雪过后,KK决定在春秋大道的某些区间 ...
- 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树
[BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...
- 【bzoj4636】蒟蒻的数列 离散化+线段树
原文地址:http://www.cnblogs.com/GXZlegend/p/6801379.html 题目描述 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个 ...
- 离散化+线段树/二分查找/尺取法 HDOJ 4325 Flowers
题目传送门 题意:给出一些花开花落的时间,问某个时间花开的有几朵 分析:这题有好几种做法,正解应该是离散化坐标后用线段树成端更新和单点询问.还有排序后二分查找询问点之前总花开数和总花凋谢数,作差是当前 ...
- Mayor's posters (离散化线段树+对lazy的理解)
题目 题意: n(n<=10000) 个人依次贴海报,给出每张海报所贴的范围 li,ri(1<=li<=ri<=10000000) .求出最后还能看见多少张海报. 思路: 由于 ...
- 干物妹小埋 (离散化 + 线段树 + DP)
链接:https://ac.nowcoder.com/acm/contest/992/B来源:牛客网 题目描述 在之前很火的一个动漫<干物妹小埋>中,大家对小埋打游戏喝可乐的印象十分的深刻 ...
- poj/OpenJ_Bailian - 2528 离散化+线段树
传送门:http://bailian.openjudge.cn/practice/2528?lang=en_US //http://poj.org/problem?id=2528 题意: 给你n长海报 ...
随机推荐
- PythonCookBook笔记——数据结构和算法
数据结构和算法 解包赋值 p = [1, 2, 3] a, b, c = p # _表示被丢弃的值 _, d, _ = p # 可变长解包 *a, b = p # 字串切割解包 line = 'nob ...
- Greenplum使用简明手册
GP服务启停 su - gpadmin gpstart #正常启动 gpstop #正常关闭 gpstop -M fast #快速关闭 gpstop –r #重启 gpstop –u #重新加载配置文 ...
- MonoTouch.Dialog简介
新建一个Single View Application项目 添加程序集 MonoTouch.Dialog.dll引用 删除 MainStoryboard.storyboard 添加空类Task.cs ...
- WPF控件模板和数据模板 - 醉意人间
来自:http://www.th7.cn/Program/WPF/2011/12/21/51676.shtml ControlTemplate用于描述控件本身. 使用TemplateBinding来绑 ...
- [Cocoa]深入浅出Cocoa之Bonjour网络编程
本文转载至 http://www.cnblogs.com/kesalin/archive/2011/09/15/cocoa_bonjour.html 深入浅出Cocoa之Bonjour网络编程 罗 ...
- kernel.panic
sysctl -a kernel.panic = 0kernel.panic_on_io_nmi = 0kernel.panic_on_oops = 1kernel.panic_on_stackove ...
- HTML 常用组件
1.下拉选择 <select id="select" name="select" class="form-control"> & ...
- 区块链+AI将给区块链带来怎样的改变?
区块链和人工智能技术都是互联网时代最新.最热的技术,不仅可以改变我们生活,还能产生巨大的财富,为此国家大力支持发展,科技巨头们也纷纷布局.那区块链与人工智能结合,对区块链技术而言会产生什么样的化学反应 ...
- CentOS 64位 6.4 命令行安装 oracle 11g 客户端
1.官网下载oracle 客户端(需登录) http://download.oracle.com/otn/linux/instantclient/11204/oracle-instantclient1 ...
- Android Weekly Notes Issue #240
Android Weekly Issue #240 January 15th, 2017 Android Weekly Issue #240 Hello, 各位亲, 从本篇笔记开始, 以后并不包含An ...