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 ...
随机推荐
- 【转】http 缓存
原文地址:https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-c ...
- c语言解二元二次方程组
设a和b是正整数 a+b=30 且a*b=221 求a和b的值 思路就是穷举a和b的值,每次得到a和b的一个值,看是否同时满足a+b=30且a*b=221,如果满足,那么就输出. 那么a和b的的取值范 ...
- jq一行一行循环读取table中的元素
获取当前tr行号,可依据index 获取当前tr对象 获取某一tr下td的内容
- linux安装JDK后发现系统带有openjdk的处理
1.JDK下载. 官网下载网址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...
- 【原创】linux signal处理中的几个问题(suse下莫名其妙死锁的处理)
我在CSDN专栏写过的,老帖子最近发现在腾讯的CVM上,服务器总是平凡的死锁后查明真像为 当你发生sig 11的异常时,会进入处理函数 signalHandler同时此时生成相应的dump file时 ...
- HttpUploader6.2-process版本
1.优化JS逻辑,在上传前先同步相同文件进度,提高多用户上传效率. 2.优化文件块保存逻辑,减少相同文件块的写入操作,减少服务器IO操作,提高上传效率. js变化: up6.js新增UrlQuer ...
- svn下载安装
TortoiseSVN 下载地址:http://subversion.apache.org/packages.html#windows VisualSVN Server 下载地址:https://ww ...
- #随笔之java匿名内部类
随笔之java匿名内部类 从今天起开始每日一篇技术博客,当然这只是我当天所学的一些随笔,里面或多或少会有理解不当的地方,希望大家多多指教,一起进步! 在讲匿名内部类之前,先讲讲内部类的一些概念. 1. ...
- 数据库表转换成JavaBean
本人花了几个小时用C#开发了一款,数据表生成javabean的软件.目前只支持Mysql,内置类型映射器.开源,没有测试. 支持数据库注释,忘了获取表注释,见谅.使用之前配置一下config.xml文 ...
- go tcp使用
TCP clientThere have been countless times during penetration tests that I've neededto whip up a TCP ...