poj 2528 Mayor's posters

简介: 点击打开链接poj2528 思路:离散化+线段树成段更新 分析: 1 首先这一题的数据是错误的,这题的区间的最大值为10000000,如果我们按照正常的线段树的思路去做的话肯定是会超内存和超时的。


点击打开链接poj2528

思路:离散化+线段树成段更新
分析:
1 首先这一题的数据是错误的,这题的区间的最大值为10000000,如果我们按照正常的线段树的思路去做的话肯定是会超内存和超时的。
2 所以我们应该考虑离散化,我们把区间离散成集中的区间。但是这个地方会有个问题
给出下面两个简单的例子应该能体现普通离散化的缺陷:
例子一:1-10 1-4 5-10
例子二:1-10 1-4 6-10
普通离散化后都变成了[1,4][1,2][3,4]
线段2覆盖了[1,2],线段3覆盖了[3,4],那么线段1是否被完全覆盖掉了呢?
例子一是完全被覆盖掉了,而例子二没有被覆盖
为了解决这种缺陷,我们可以在排序后的数组上加些处理,比如说[1,2,6,10]
如果相邻数字间距大于1的话,在其中加上任意一个数字,比如加成[1,2,3,6,7,10],然后再做线段树就好了.

代码:

#include<set>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 10010;
struct Node{
    int left;
    int right;
    int mark;
};
Node node[4*MAXN] , tmp[MAXN];
int n , pos;
int num[2*MAXN];
set<int>s;

void hash_map(){
    int index = 1 , tmpNum[MAXN*2];
    for(int i = 0 ; i < pos ; i++){
       tmpNum[index++] = num[i];
       if(num[i+1]-num[i] > 1)
          tmpNum[index++] = num[i]+1;
    }
    pos = index;
    memcpy(num , tmpNum , sizeof(num));
}

int search(int x){
    int left , right , mid;
    left = 1 , right = pos-1;
    while(left <= right){
       int mid = (left+right)>>1;
       if(num[mid] == x) 
           return mid;
       else if(num[mid] < x)
           left = mid+1;
       else
           right = mid-1;
    }
}

void buildTree(int left , int right , int pos){
    node[pos].left = left;
    node[pos].right = right;
    node[pos].mark = 0;
    if(left == right)
      return;
    int mid = (left+right)>>1;
    buildTree(left , mid , pos<<1);
    buildTree(mid+1 , right , (pos<<1)+1);
}

void push_down(int pos){
    if(node[pos].mark){
       node[pos<<1].mark = node[pos].mark;
       node[(pos<<1)+1].mark = node[pos].mark;
       node[pos].mark = 0;
    }
}

void update(int left , int right , int value , int pos){
    if(left <= node[pos].left && right >= node[pos].right){
       node[pos].mark = value;
       return;
    }
    push_down(pos);
    int mid = (node[pos].left + node[pos].right)>>1;
    if(right <= mid)
       update(left , right , value , pos<<1);
    else if(left > mid)
       update(left , right , value , (pos<<1)+1);
    else{
       update(left , mid , value , pos<<1);
       update(mid+1 , right , value , (pos<<1)+1);
    }
}

int query(int index , int pos){
    if(node[pos].left == node[pos].right)
      return node[pos].mark;
    push_down(pos);
    int mid = (node[pos].left + node[pos].right)>>1;
    if(index <= mid)
       return query(index , pos<<1);
    else
       return query(index , (pos<<1)+1);
}

int main(){
    int Case , cnt;
    int x , y;
    scanf("%d" , &Case);
    while(Case--){
       scanf("%d" , &n);
       pos = 0 , cnt = 0;
       for(int i = 0 ; i < n ; i++){
          scanf("%d%d" , &tmp[i].left , &tmp[i].right);
          num[pos++] = tmp[i].left , num[pos++] = tmp[i].right;
       }
       //离散化
       sort(num , num+pos);
       pos = unique(num , num+pos)-num;
       hash_map();   
       for(int i = 0 ; i < n ; i++){
          int index = search(tmp[i].left);
          tmp[i].left = index;
          index = search(tmp[i].right);
          tmp[i].right = index;
       }
       buildTree(1 , pos-1 , 1);
       for(int i = 0 ; i < n ; i++)
           update(tmp[i].left , tmp[i].right , ++cnt , 1);
       s.clear();
       for(int i = 1 ; i < pos ; i++)
           s.insert(query(i , 1));
       printf("%d\n" , s.size());
    }
    return 0;
}





目录
相关文章
POJ 1012 Joseph
Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53862   Accepted: 20551 Description The Joseph's problem is notoriously known.
824 0
poj 3620
题意:给出一个矩阵,其中有些格子干燥、有些潮湿。       如果一个潮湿的格子的相邻的四个方向有格子也是潮湿的,那么它们就可以构成更大       的湖泊,求最大的湖泊。       也就是求出最大的连在一块儿的潮湿的格子的数目。
559 0
poj 1455
Description n participants of > sit around the table. Each minute one pair of neighbors can change their places.
607 0
POJ-1003-hangover
Description How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length.
744 0
|
算法 机器人 编译器
POJ-2632
#include int main() { int k,a,b,n,m,i,j,num,rep,rect[100][100],robot[100][3]; int flag; char c; for(scanf("%d...
894 0
|
算法 数据建模 机器学习/深度学习
poj1273Drainage Ditches
1 #include 2 /* 3 题意:就是寻找从源点到汇点的最大流! 4 要注意的是每两个点的流量可能有多个,也就是说有重边,所以要把两个点的所有的流量都加起来 5 就是这两个点之间的流量了! 6 ...
833 0