线段树区间更新,区间统计+离散化 POJ 2528 Mayor's posters
题意:有一个非常长的板子(10000000长),在上面贴n(n<=10000)张海报。问最后从外面能看到几张不同的海报。
由于板子有10000000长,直接建树肯定会爆,所以须要离散化处理,对于每张海报,有两个端点值,最后能看到几张海报跟他们的端点值的相对大小有关,跟绝对大小无关。所以就把全部海报的端点离散化处理,总共2n个端点,排序去重,相应p(p<=2n)个点。
然后建树,由于p不超过20000,所以这样就能够接受了。区间更新时,由于我们仅仅关心最外面海报的颜色有多少种。所以向下传递节点信息的时候把上面节点的信息去掉。这样在查询的时候就能方便一些,用一个标记数组记录总共同拥有多少种颜色就能够了。
代码:
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std; #define PB push_back
#define MP make_pair #define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define OI(X) printf("%d",X);
#define RS(X) scanf("%s", (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;} /*#ifdef HOME
freopen("in.txt","r",stdin);
#endif*/
const int MOD = 1e9+7;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
//#define HOME int Scan()
{
int res = 0, ch, flag = 0; if((ch = getchar()) == '-') //推断正负
flag = 1; else if(ch >= '0' && ch <= '9') //得到完整的数
res = ch - '0';
while((ch = getchar()) >= '0' && ch <= '9' )
res = res * 10 + ch - '0'; return flag ? -res : res;
}
/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/
int data[100000+10]; int n;
int a[20010];
int b[20010];
int u[40000]; void pushdown(int rt)
{
if(data[rt]!=-1)
{
data[rt<<1]=data[(rt<<1)+1]=data[rt];
data[rt]=-1;
}
}
void build(int l,int r,int rt)
{
if(l==r)
{
data[rt]=-1;
return;
}
int m=(l+r)>>1;
build(l,m,rt<<1);
build(m+1,r,(rt<<1)+1);
}
void update(int l,int r,int rt,int a,int b,int c)
{
if(a<=l&&r<=b)
{
data[rt]=c;
return;
}
pushdown(rt);
int m=(l+r)>>1;
if(a<=m)
update(l,m,rt<<1,a,b,c);
if(b>m)
update(m+1,r,(rt<<1)+1,a,b,c);
}
int vis[20010];
int sum;
void query(int l,int r,int rt)
{if(data[rt]!=-1)
{
if(!vis[data[rt]])
{
vis[data[rt]]=1;
sum++;
}
return;
}
int m=(l+r)>>1;
query(l,m,rt<<1);
query(m+1,r,(rt<<1)+1); } int main()
{
int c;
RI(c);
while(c--)
{RI(n);
REP(i,0,n)
{
RII(a[i],b[i]);
}
REP(i,0,n)
{
u[i]=a[i];
u[i+n]=b[i];
}
sort(u,u+2*n);
int p=unique(u,u+2*n)-u;
REP(i,0,n)
{int t=lower_bound(u,u+p,a[i])-u;
a[i]=t+1;
t=lower_bound(u,u+p,b[i])-u;
b[i]=t+1;
}
build(1,p,1);
REP(i,0,n)
{
update(1,p,1,a[i],b[i],i);
}
MS0(vis);
sum=0;
query(1,p,1);
printf("%d\n",sum);
} return 0;
}
线段树区间更新,区间统计+离散化 POJ 2528 Mayor's posters的更多相关文章
- POJ 2528 Mayor's posters 离散化+线段树
题目大意:给出一些海报和贴在墙上的区间.问这些海报依照顺序贴完之后,最后能后看到多少种海报. 思路:区间的范围太大,然而最多仅仅会有10000张海报,所以要离散化. 之后用线段树随便搞搞就能过. 关键 ...
- POJ 2528 Mayor's posters 离散化和线段树题解
本题就是要往墙上贴海报,问最后有多少可见的海报. 事实上本题的难点并非线段树,而是离散化. 由于数据非常大,直接按原始数据计算那么就会爆内存和时间的. 故此须要把数据离散化. 比方有海报1 6 7 ...
- poj 2528 Mayor's posters
这个题意是市长竞选,然后每一个人都能够贴广告牌.能够覆盖别人的看最后剩几个广告牌 这题目想了两个多小时,最后忍不住看了一下题解. 发现仅仅是简单地hash 和线段树成段更新 由于有10000个人竞选 ...
- poj 2528 Mayor's posters 【线段树 + 离散化】
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 50643 Accepted: 14675 ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- hdu 1166线段树 单点更新 区间求和
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu2795(线段树单点更新&区间最值)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要 ...
随机推荐
- foxmial 和 outlook设置问题
您可以使用支持POP3的客户端软件(例如Foxmail或Outlook)收发您的邮件.请配置您的电子邮件客户端,以下载QQ邮箱邮件. 了解如何进行配置,请单击您的电子邮件客户端名称: Foxmail设 ...
- CodeForces:699B-One Bomb
B. One Bomb time limit per test1 second memory limit per test256 megabytes Problem Description You a ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F题
The Heaviest Non-decreasing Subsequence Problem 解题心得 这个题就是一个简单的动态规划,非递减最长子序列的改版(加一个权重),只要把权重为5的改成5个权 ...
- JavaScript正则表达式-断言
(?=reg_pattern):正前向断言 只有当字符串右侧出现匹配reg_pattern的字符时才匹配正则表达式. str = "img1.jpg,img2.jpg,img3.bmp&qu ...
- 【04】Math图解
[04]Math知识图
- 【工具】Homebrew的安装及使用
Homebrew官网:http://brew.sh/index_zh-cn.html Homebrew是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件,相当于linux下的a ...
- OSPF 一 基础
本节介绍ospf路由选择协议 为链路状态 路由选择协议 一 分类 open shortest path first 开放最短路优先 公有协议 单区域的ospf实施 运行在一个自治系 ...
- 异常System.Threading.Thread.AbortInternal
异常信息: System.Threading.ThreadAbortException: 正在中止线程. 在 System.Threading.Thread.AbortInternal() 在 Sys ...
- xtrabackup实现全量备份和增量备份
mysql增量和完全备份innobackupex2.1.9版本1 yum安装: 官网地址:https://www.percona.com/doc/percona-xtrabackup/LATEST/i ...
- 安装oracle提示swap交换分区太小
1.用dd命令创建一个16G的文件 #dd if=/dev/zero of=/var/swapfile bs=1G count=16 2.将它创建为Linux Swap虚拟交换文件 #mkswap ...