听说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. 447. Number of Boomerangs 回力镖数组的数量

    [抄题]: Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple ...

  2. npm 升级

    当我们运行某个项目是 会提示 > my-first-vue-project@1.0.0 dev C:\Users\ASUS\my-project > node build/dev-serv ...

  3. rpmbuild spec 打包jar变小了、设置禁止压缩二进制文件Disable Binary stripping in rpmbuild

    Disable Binary stripping in rpmbuild 摘自:http://livecipher.blogspot.com/2012/06/disable-binary-stripp ...

  4. Spring:配置文件

    首先是bean.xml,配置所有的bean,一般也叫applicationContext.xml,应用程序上下文.示例: <?xml version="1.0" encodi ...

  5. Android绘图之Matrix

    一.概述 1. 在Android中,如果你用Matrix进行过图像处理,那么一定知道Matrix这个类.Android中的Matrix是一个3 x 3的矩阵,其内容如下 2.Matrix的对图像的处理 ...

  6. duilib入门简明教程 -- VS环境配置(2)

        会SVN和配置VS环境的请跳过此篇~          既然是入门教程,那当然得基础点,因为搜索duilib相关资料时,发现有些小伙伴到处都是编译错误,以及路径配置错误等等,还有人不知道SVN ...

  7. LSI SAS3008 RAID配置方法

    7.1  概述 LSI SAS3008 RAID 控制卡(以下简称LSI SAS3008)是基于Fusion-MPT™ (消息传递技术)架构的8端口12Gbit/s SAS控制器,并采用PCIe3.0 ...

  8. Socket网络通讯

    网络编程 使用C#进行网络编程时,通常都需要用到System.Net命名空间.System.Net.Sockets命名空间和System.Net.Mail命名空间: 1. System.Net命名空间 ...

  9. 在有主分支和个人分支情况下的TFS使用方法

    从事.NET开发的资深童鞋一定都知道VS有自带的代码管理工具TFS(Team Foundation Server ),但是开发萌新可能就不太了解了,下面我就介绍一下这个工具以及它的一些常用操作. TF ...

  10. 重拾C,一天一点点_7

    标准库,atof()函数包含在头文件<stdlib.h>中 /******把字符串s转换为相应的双精度浮点数*******/ #include <stdio.h> #inclu ...