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)的更多相关文章

  1. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

  2. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

  3. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  4. POJ - 2528 Mayor's posters(dfs+分治)

    POJ - 2528 Mayor's posters 思路:分治思想. 代码: #include<iostream> #include<cstdio> #include< ...

  5. POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】

    任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  6. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  7. POJ 2528 - Mayor's posters - [离散化+区间修改线段树]

    题目链接:http://poj.org/problem?id=2528 Time Limit: 1000MS Memory Limit: 65536K Description The citizens ...

  8. poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化

    Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...

  9. POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59239   Accepted: 17157 ...

  10. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

随机推荐

  1. python内置模块——logging

    内置模块-logging loging模块是python提供的内置模块,用来做日志处理. 日志等级: 等级 释义 级别数值 CRITICAL(fatal) 致命错误,程序根本跑不起来 50 ERROR ...

  2. Enterprise Architect去掉元素背景渐变效果

    打开设置界面:TOOLS - Options 修改Gradients and Background的Gradient Fill Direction for属性为none即可

  3. Hive select查询语句

    创建表 CREATE TABLE t_usa_covid19( count_date string, county string, state string, fips int, cases int, ...

  4. LeetCode331:验证二叉树的前序序列化(递归)

    解题思路:把所有元素存成数组,设置一个全局下标next,表示当前节点如果要继续遍历应当从数组的哪个位置开始,然后从下标 0 开始DFS.如果DFS返回真并且next下标等于数组的长度,说明元素已经全部 ...

  5. flower插件-监视celery

    安装和使用: https://flower.readthedocs.io/en/latest/install.html#installation https://github.com/mher/flo ...

  6. 聊聊流式数据湖Paimon(四)

    Partial Update 数据打宽 通过不同的流写不同的字段,打宽了数据的维度,填充了数据内容:如下所示: --FlinkSQL参数设置 set `table.dynamic-table-opti ...

  7. springsecurity 使用浅谈(一)

    1. 背景 springsecurity框架主要用于Web应用的认证和授权.所谓认证就是验证当前访问系统的是不是本系统的用户,并且要确认具体是哪个用户.而授权就是经过认证后判断当前用户是否有权 限进行 ...

  8. vue 遍历的汉字显示不同的颜色

    <template> <div> <div class="stars"> <span v-for="(star, index) ...

  9. HTML&CSS基本知识

    HTML&CSS基本知识 一.HTML基本介绍 W3C标准(成立于1994年,web技术领域最权威和具影响力的国际中立性技术标准机构) world Wide web Consortium(万维 ...

  10. Cesium案例解析(九)——Rotatable2DMap旋转2D地图

    目录 Cesium的Rotatable 2D Map示例展示了一个旋转的二维地图: 'use strict'; var viewer = new Cesium.Viewer('cesiumContai ...