听说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 奇怪的测试数据的更多相关文章

  1. POJ 2528 Mayor's posters

    Mayor's posters Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

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

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

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

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

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

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

  5. poj 2528 (线段树+离散化)

    poj 2528 For each input data set print the number of visible posters after all the posters are place ...

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

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

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

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

  8. POJ 2528——Mayor's posters——————【线段树区间替换、找存在的不同区间】

    Mayor's posters Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  9. poj 2528 线段树区间修改+离散化

    Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...

随机推荐

  1. pt-table-checksum、pt-table-sync核对主从库一致性

    一.下载并安装工具http://www.percona.com/downloads/percona-toolkit/目前最新的版本是percona-toolkit_2.2.12.tar.gz上传到服务 ...

  2. openebula vm无法获取IP问题解决

    http://archives.opennebula.org/documentation:archives:rel2.2:cong Contextualizing Virtual Machines 2 ...

  3. =面试题:java面试基本方向 背1 有用 项目二技术学完再看

    一.Java基础 1. 集合框架A)集合中泛型优点? 将运行期的ClaasCastException 转到编译期异常.  泛型还提供通配符 1)HashMap---允许一个键为null,允许多个值为n ...

  4. boost库中sleep方法详解

    博客转载自:https://blog.csdn.net/huang_xw/article/details/8453506 boost库中sleep有两个方法: 1. 这个方法只能在线程中用, 在主线程 ...

  5. $.post()参数及返回值

    JQuery中的$.post()函数 先看一个例子:$.post("adduser.ashx", { "name": name, "sex" ...

  6. 简单的Cookie记录浏览记录案例

    books.jsp 界面 代码 <%@ page contentType="text/html;charset=UTF-8" language="java" ...

  7. html5 存储方式

    localstorage(永久保存)&&sessionstorage(重新打开浏览器会消失) sessionStorage用于本地存储一个会话(session)中的数据,这些数据只有在 ...

  8. C++面试基础

    自己整理了一些常见的面试题,频率挺高的都是,而且感觉这里这些基础的东西都会问,自己过几天也要面试了,所以发上来让大家一起看看,有什么错误的地方望提醒我纠正. 32位数据类型以及sizeof大小. ch ...

  9. (转)每位设计师都应该拥有的50个CSS代码片段

    原文地址:http://www.cnblogs.com/fengyuqing/archive/2013/06/15/css_50.html 面对每年如此多的 新趋势 ,保持行业的领先是个很困难问题. ...

  10. POJ1125 Stockbroker Grapevine(spfa枚举)

    Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a me ...