离散化/线段树 (POJ - 2528 Mayor's posters)
Mayor's posters https://vjudge.net/problem/POJ-2528#author=szdytom
线段树 + 离散化
讲解:https://blog.csdn.net/qq_35802619/article/details/98326267
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define sc(n) scanf("%c",&n)
#define sd(n) scanf("%d",&n)
#define pd(n) printf("%d\n", (n))
#define sdd(n,m) scanf("%d %d",&n,&m)
#define pdd(n,m) printf("%d %d\n",n, m)
#define ms(a,b) memset(a,b,sizeof(a))
#define all(c) c.begin(),c.end()
typedef long long ll;
const int maxn = 2e5 + 5;//数组注意别开小了
struct node {
int l, r, gg;
}tr[maxn << 2 + 5];
int lg[maxn], rg[maxn];
int lisan[maxn];
int cnt, ans;
bool book[maxn];
void build(int p, int l, int r) {
tr[p].l = l, tr[p].r = r;
if (l == r) {
tr[p].gg = 0; return;
}
int m = (l + r) >> 1;
build(p << 1, l, m);
build(p << 1 | 1, m + 1, r);
tr[p].gg = 0;
}
void spread(int q) {
if (tr[q].gg == 0) return;
tr[q << 1].gg = tr[q].gg;
tr[q << 1 | 1].gg = tr[q].gg;
tr[q].gg = 0;
}
void update(int q, int l, int r, int v) {
if (l <= tr[q].l && r >= tr[q].r) {
tr[q].gg = v; return;
}
spread(q);
int m = (tr[q].l + tr[q].r) >> 1;
if (l <= m) update(q << 1, l, r, v);
if (r > m) update(q << 1 | 1, l, r, v);
}
void ask(int q, int l, int r)
{
if (tr[q].gg && !book[tr[q].gg])
{
ans++;
book[tr[q].gg] = 1;
return;
}
if (l == r) return;
spread(q);
int mid = (l + r) >> 1;
ask(q << 1, l, mid);
ask(q << 1 | 1, mid + 1, r);
}
int main() {
//freopen("in.txt", "r", stdin);
//ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t, n; sd(t);
while (t--) {
cnt = 0; ms(book, false); sd(n);
for (int i = 0; i < n; ++i) {
sdd(lg[i], rg[i]);
lisan[cnt++] = lg[i];
lisan[cnt++] = rg[i];
}
//离散化
sort(lisan, lisan + cnt);
int m = unique(lisan, lisan + cnt) - lisan;
int t0 = m;
for (int i = 1; i <= m; ++i)
if (lisan[i] - lisan[i - 1] > 1)lisan[t0++] = lisan[i - 1] + 1;
sort(lisan, lisan + t0);
build(1, 1, t0);
for (int i = 0; i < n; ++i) {
int x = lower_bound(lisan, lisan + t0, lg[i]) - lisan + 1;
int y = lower_bound(lisan, lisan + t0, rg[i]) - lisan + 1;
// cout <<x << " " << y << endl;
update(1, x, y, i + 1);
}
ans = 0;
ask(1, 1, t0);
printf("%d\n", ans);
}
return 0;
}
离散化/线段树 (POJ - 2528 Mayor's posters)的更多相关文章
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ - 2528 Mayor's posters(dfs+分治)
POJ - 2528 Mayor's posters 思路:分治思想. 代码: #include<iostream> #include<cstdio> #include< ...
- POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】
任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- POJ 2528 - Mayor's posters - [离散化+区间修改线段树]
题目链接:http://poj.org/problem?id=2528 Time Limit: 1000MS Memory Limit: 65536K Description The citizens ...
- poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化
Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...
- POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 59239 Accepted: 17157 ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
随机推荐
- Vite4+Typescript+Vue3+Pinia 从零搭建(4) - 代码规范
项目代码同步至码云 weiz-vue3-template 要求代码规范,主要是为了提高多人协同和代码维护效率,结合到此项目,具体工作就是为项目配置 eslint 和 prettier. editorc ...
- .NET8极致性能优化CHRL
前言 .NET8在.NET7的基础上进行了进一步的优化,比如CHRL(全称:CORINFO_HELP_RNGCHKFAIL)优化技术,CORINFO_HELP_RNGCHKFAIL是边界检查,在.NE ...
- 深入浅出 PLT/GOT Hook与原理实践
动态链接 计算机程序链接时分两种形式:静态链接和动态链接. 静态链接在链接时将所有目标文件中的代码.数据等Section都组装到可执行文件当中,并将代码中使用到的外部符号(函数.变量)都进行了重定位. ...
- C#/.NET/.NET Core优秀项目和框架2023年11月简报
前言 公众号每月定期推广和分享的C#/.NET/.NET Core优秀项目和框架(公众号每周至少推荐两个优秀的项目和框架当然节假日除外),公众号推文有项目和框架的介绍.功能特点以及部分截图等(打不开或 ...
- vertx的学习总结4之异步数据和事件流
一.异步数据和事件流 1.为什么流是事件之上的一个有用的抽象? 2.什么是背压,为什么它是异步生产者和消费者的基础? 3.如何从流解析协议数据? 1. 答:因为它能够将连续的事件序列化并按照顺序进行 ...
- MySQL笔记01: MySQL入门_1.1 MySQL概述
1.1 MySQL概述 MySQL是一个关系数据库管理系统(Relational DataBase Management System,RDBMS).它是一个程序,可以存储大量的种类繁多的数据,并且提 ...
- jenkins安装部署、主从架构、slave镜像、K8S对接
介绍 CI/CD工具,自动化持续集成和持续部署,用于构建各种自动化任务. 官方提供了docker镜像https://hub.docker.com/r/jenkins/jenkins 使用Deploym ...
- Oracle-startup和shutdown
startup不同参数作用 startup nomount 非安装启动,以这种方式启动可执行: 1.重建控制文件. 2.重建数据库读取init.ora文件. 3.启动实例,即启动SGA和后台进程,需要 ...
- Scrapy在pipeline中集成mongodb
settings.py中设置配置项 MONGODB_HOST = "127.0.0.1" MONGODB_PORT = 27017 MONGODB_DB_NAME = " ...
- Kernel Memory 入门系列:异步管道
Kernel Memory 入门系列:异步管道 前面所介绍的处理流程都是基于同步管道的,即文档导入的时候,会等到文档处理完成之后才会返回. 但是在实际的应用中,文档很多,而且文档的处理时间也不确定,如 ...