POJ - 2528 奇怪的测试数据
听说POJ内部测试数据有问题
我这份代码是WA的(UPD:第二份是AC代码),不过目前把discuss的数据试了一下没毛病
自己试了几组好像也没毛病?
感觉线段树部分的简单hash处理方法还是值得学习的,贴出来吧
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#define rep(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
const int maxn = 2e6+11;
bool Hash[30030];
int li[30030],ri[30030];
int arr[maxn],n;
struct ST{
int cover[maxn<<2];
#define lc o<<1
#define rc o<<1|1
void init(){memset(cover,0,sizeof cover);}
void build(int o,int l,int r){
cover[o]=0;
if(l==r) return;
int m=l+r>>1;
build(lc,l,m);
build(rc,m+1,r);
}
void pd(int o){
if(cover[o]){
cover[lc]=cover[rc]=cover[o];
cover[o]=0;
}
}
void update(int o,int l,int r,int L,int R,int v){
if(L<=l&&r<=R){
cover[o]=v;
return;
}
pd(o);
int m=l+r>>1;
if(L<=m) update(lc,l,m,L,R,v);
if(R>m) update(rc,m+1,r,L,R,v);
}
int query(int o,int l,int r,int L,int R){
if(cover[o]){
if(!Hash[cover[o]]){
Hash[cover[o]]=1;
return 1;
}
}
if(l==r) return 0;//base: not found
int m=l+r>>1;
int ans=0;
if(L<=m) ans+=query(lc,l,m,L,R);
if(R>m) ans+=query(rc,m+1,r,L,R);
return ans;
}
}st;
int C(int x){
if(x>=10000000) return 10000000;
else if(x<=1) return 1;
return x;
}
int main(){
int T; scanf("%d",&T);
while(T--){
scanf("%d",&n);
memset(Hash,0,sizeof Hash);
rep(i,1,n){
scanf("%d%d",&li[i],&ri[i]);
arr[i]=li[i];arr[i+n]=ri[i];
}
arr[2*n+1]=1;arr[2*n+2]=10000000;
int tot=2*n+2;
sort(arr+1,arr+1+tot);
int tmp=tot;
rep(i,1,tmp-1){
if(arr[i+1]-arr[i]>1){
arr[++tot]=C(arr[i+1]-1);
arr[++tot]=C(arr[i]+1);
}
}
sort(arr+1,arr+1+tot);
tot=unique(arr+1,arr+1+tot)-arr-1;
rep(i,1,n){
li[i]=lower_bound(arr+1,arr+1+tot,li[i])-arr;
ri[i]=lower_bound(arr+1,arr+1+tot,ri[i])-arr;
// cout<<i<<" "<<li[i]<<" "<<ri[i]<<endl;
}
st.init(); st.build(1,1,tot);
rep(i,1,n){
st.update(1,1,tot,li[i],ri[i],i);
}
printf("%d\n",st.query(1,1,tot,1,tot));
}
return 0;
}
两小时后的UPDATE:
AC代码(忘了pushdown ORZ)
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#define rep(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
const int maxn = 2e6+11;
bool Hash[30030];
int li[30030],ri[30030];
int arr[maxn],n;
struct ST{
int cover[maxn<<2];
#define lc o<<1
#define rc o<<1|1
void build(int o,int l,int r){
cover[o]=0;
if(l==r) return;
int m=l+r>>1;
build(lc,l,m);
build(rc,m+1,r);
}
void pd(int o){
if(cover[o]){
cover[lc]=cover[rc]=cover[o];
cover[o]=0;
}
}
void update(int o,int l,int r,int L,int R,int v){
if(L<=l&&r<=R){
cover[o]=v;
return;
}
pd(o);
int m=l+r>>1;
if(L<=m) update(lc,l,m,L,R,v);
if(R>m) update(rc,m+1,r,L,R,v);
}
int query(int o,int l,int r,int L,int R){
if(cover[o]){
if(!Hash[cover[o]]){
Hash[cover[o]]=1;
return 1;
}
else return 0;
}
pd(o);
if(l==r) return 0;//base: not found
int m=l+r>>1;
int ans=0;
if(L<=m) ans+=query(lc,l,m,L,R);
if(R>m) ans+=query(rc,m+1,r,L,R);
return ans;
}
}st;
int C(int x){
if(x>=10000000) return 10000000;
else if(x<=1) return 1;
return x;
}
int main(){
int T; scanf("%d",&T);
while(T--){
scanf("%d",&n);
memset(Hash,0,sizeof Hash);
rep(i,1,n){
scanf("%d%d",&li[i],&ri[i]);
arr[i]=li[i];arr[i+n]=ri[i];
}
arr[2*n+1]=1;arr[2*n+2]=10000000;
int tot=2*n+2;
sort(arr+1,arr+1+tot);
int tmp=tot;
rep(i,1,tmp-1){
if(arr[i+1]-arr[i]>1){
arr[++tot]=C(arr[i+1]-1);
arr[++tot]=C(arr[i]+1);
}
}
sort(arr+1,arr+1+tot);
tot=unique(arr+1,arr+1+tot)-arr-1;
rep(i,1,n){
li[i]=lower_bound(arr+1,arr+1+tot,li[i])-arr;
ri[i]=lower_bound(arr+1,arr+1+tot,ri[i])-arr;
}
st.build(1,1,tot);
rep(i,1,n){
st.update(1,1,tot,li[i],ri[i],i);
}
printf("%d\n",st.query(1,1,tot,1,tot));
}
return 0;
}
POJ - 2528 奇怪的测试数据的更多相关文章
- POJ 2528 Mayor's posters
Mayor's posters Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 59239 Accepted: 17157 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- poj 2528 (线段树+离散化)
poj 2528 For each input data set print the number of visible posters after all the posters are place ...
- POJ - 2528 Mayor's posters(dfs+分治)
POJ - 2528 Mayor's posters 思路:分治思想. 代码: #include<iostream> #include<cstdio> #include< ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ 2528——Mayor's posters——————【线段树区间替换、找存在的不同区间】
Mayor's posters Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- poj 2528 线段树区间修改+离散化
Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...
随机推荐
- 使用Python定时执行一任务,自动登录某web系统,生成报表,然后发送邮件给指定人员
一.项目需求 每周从A系统生成一张Excel报表,发送此报表给指定人员,相关人员依据此报表去完成后续的工作. 项目限制: 1.无法通过EDI系统交互的方式从后台读取数据 2.由于公司网络环境限制,不能 ...
- 修改QPushButton北京颜色和字体背景
项目需要修改按钮背景的颜色 QPalette pal = startBtn.palette(); //startBtn是我已经定义好的QPushButton对象 pal.setColor(QPalet ...
- 关于wamp中升级PHP+Apache 的问题
首先个人不建议wamp中升级php版本,如果你不信可以试一试,当你php升级后发想,奥,Apache版本不匹配,然后又去升级Apache,结果搞了半天,弄出来了就好,要是没出来,可能你会气死(好吧,气 ...
- Python基础入门-For循环
For循环的功能比较强大,他可以帮助我们实现很多重复性的工作.而且for循环能迭代不同的数据结构.他的应用也十分的广泛,作为初学者,我们需要对循环的概念多加理解和练习.接下来我们就来学习for循环的一 ...
- HDU 4081 Peach Blossom Spring (最小生成树+dfs)
题意:给定一个 n 个点和相应的权值,要求你用 n-1 条边连接起来,其中一条边是魔法边,不用任何费用,其他的边是长度,求该魔法边的两端的权值与其他边费用的尽量大. 析:先求出最小生成树,然后再枚举每 ...
- 关于 XML 字段内容查询
找到个总结相当好的知识点的归纳,记在自己的博客里也方便查询 /* sql xml 入门: --by jinjazz --http://blog.csdn.net/jinjazz ...
- Topshelf + Quartz2.5 创建基于windows服务
1.创建一个定时调度Quartz类 using Quartz; using Quartz.Impl; using System; using System.Collections.Generic; u ...
- CentOS 进程操作
ps -ef:查看所有进程, ps -ef |grap firewalld 查看与firewalld相关的进程 which :查看进程:which firewalld kill 进程id:杀掉进程 k ...
- Android系列一: 环境搭建
相关软件 JAVA JDKAndroid StudioHAXM JDK的安装和Java环境变量的设置 1.JDK下载地址: http://www.oracle.com/technetwork/j ...
- Android app如何加密?
欢迎访问网易云社区,了解更多网易技术产品运营经验. Android App包含的内容有dex文件,so文件,res,assets资源文件.对应的加密按此内容分为三大方面:dex保护.so加密.资源保护 ...