poj 2528 线段树区间修改+离散化
Mayor's posters POJ 2528
线段树区间修改加离散化
#include <cstdio>
#include <iostream>
#include <queue>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
#define ll long long
#define P pair<int,int>
const ll INF=1e18;
const int N=1e6+10;
int ans[N];
int l[N],r[N];
struct SegmentTree{
int l,r;
ll dat;
}t[N*4];
void build(int p,int l,int r)
{
t[p].l = l;
t[p].r = r;
t[p].dat = 0;
if(t[p].l == t[p].r){t[p].dat = 0;return ;}
int mid = (l+r)/2;
build(p*2,l,mid);
build(p*2+1,mid+1,r);
}
void spread(int p)
{
if(t[p].dat)
{
t[p*2].dat = t[p].dat;
t[p*2+1].dat = t[p].dat;
t[p].dat = 0;
}
}
void change(int p,int l,int r,ll v)
{
if(l <= t[p].l && t[p].r <= r)
{
t[p].dat = v;
return ;
}
int mid = (t[p].l+t[p].r)/2;
spread(p);
if(l<=mid) change(p*2,l,r,v);
if(r>mid) change(p*2+1,l,r,v);
}
void ask(int p,int l,int r)
{
if(t[p].dat)
{
ans[t[p].dat] = 1;
return ;
}
if(t[p].l == t[p].r)
return;
int mid = (t[p].l+t[p].r)/2;
spread(p);
ask(p*2,l,r);
ask(p*2+1,l,r);
}
vector<int>num;
int n;
int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while(T--)
{
cin >> n;
num.clear();
for(int i=1;i<=n;i++)
{
cin >> l[i] >> r[i];
num.push_back(l[i]);
num.push_back(r[i]);
}
sort(num.begin(),num.end());
num.erase(unique(num.begin(),num.end()),num.end());
int m = num.size();
for(int i=1;i<m;i++)
{
if(num[i] - num[i-1] > 1)
num.push_back(num[i-1]+1);
}
sort(num.begin(),num.end());
m = num.size();
build(1,0,m-1);
for(int i=1;i<=n;i++)
{
ans[i] = 0;
int x = lower_bound(num.begin(),num.end(),l[i])-num.begin();
int y = lower_bound(num.begin(),num.end(),r[i])-num.begin();
change(1,x,y,i);
}
int res = 0;
ask(1,0,m-1);
for(int i=1;i<=n;i++)
{
if(ans[i])
res++;
}
cout << res << "\n";
}
return 0;
}
poj 2528 线段树区间修改+离散化的更多相关文章
- Mayor's posters POJ - 2528 线段树区间覆盖
//线段树区间覆盖 #include<cstdio> #include<cstring> #include<iostream> #include<algori ...
- LightOJ 1089 - Points in Segments (II) 线段树区间修改+离散化
http://www.lightoj.com/volume_showproblem.php?problem=1089 题意:给出许多区间,查询某个点所在的区间个数 思路:线段树,由于给出的是区间,查询 ...
- POJ - 3468 线段树区间修改,区间求和
由于是区间求和,因此我们在更新某个节点的时候,需要往上更新节点信息,也就有了tree[root].val=tree[L(root)].val+tree[R(root)].val; 但是我们为了把懒标记 ...
- POJ 3468 线段树区间修改查询(Java,c++实现)
POJ 3468 (Java,c++实现) Java import java.io.*; import java.util.*; public class Main { static int n, m ...
- A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询
//add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...
- POJ-2528 Mayor's posters (线段树区间更新+离散化)
题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...
- Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)
题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...
- 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)
Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
随机推荐
- 初识Redis,看这一篇就够了
环境的搭建和安装网上有很多教程,在这里就不再重复了. 1. Redis是什么? Redis(全称:Remote Dictionary Server 远程字典服务)是一个开源的使用ANSI C语言编写. ...
- windows下使用pycharm配置python的virtualenv环境
环境:win10 python2.7.10(64) 在path中配置python环境 注意:我这边的环境是py2和py3共存的,但是我这边默认使用的是py2. 在virtualenv环境中安装指定的p ...
- kvm命令
查询:virsh -c qemu:///system list 查看当前的虚拟系统 brctl show 列出当前所有的网桥接口virsh list 列出运行的虚拟机virs ...
- 基于TensorFlow的MNIST手写数字识别-深入
构建多层卷积神经网络时需要多组W和偏移项b,我们封装2个方法来产生W和b 初级MNIST中用0初始化W和b,这里用噪声初始化进行对称打破,防止产生梯度0,同时用一个小的正值来初始化b避免dead ne ...
- [urllib]urlretrieve在python3
python3下面要使用:urllib.request.urlretrieve()这种形式的调用 from urllib.request import urlretrieve urlretrieve( ...
- ubuntu 16.04 anaconda 4.2.0 安装tensorflow 报错
ubuntu 16.04 anaconda 4.2.0 安装tensorflow 报错. 安装pyenv后,在pyenv环境内安装 anaconda,然后再安装tensorflow不再报错,比较奇怪, ...
- HYSBZ_1588_set
http://www.lydsy.com/JudgeOnline/problem.php?id=1588 直接set做就好了,注意set的迭代器只能--之类的操作. OJ崩了,也不知道对错. #inc ...
- android studio闪退的原因
可能是因为某个监听的问题,比如没有把Mainacticity中定义的button和布局文件中的按钮控件关联起来,就会出现这个问题
- 【OpenGL】LNK1104 无法打开文件“freeglutd.lib”
新建的OpenGL程序编译时经常会出现[LNK1104 无法打开文件“freeglutd.lib”]问题,如果freeglutd.lib确实放到了正确的路径下,通常可以通过添加“NDEBUG”宏定义解 ...
- Java 添加、读取、删除Excel文档属性
在文档属性中,可以设置诸多关于文档的信息,如创建时间.作者.单位.类别.关键词.备注等摘要信息以及一些自定义的文档属性.下面将通过Java程序来演示如何设置,同时对文档内的已有信息,也可以实现读取和删 ...