HDU 6183 線段樹新玩法 + 離散化

題目

Color it

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 872    Accepted Submission(s): 265


Problem Description
Do you like painting? Little D doesn't like painting, especially messy color paintings. Now Little B is painting. To prevent him from drawing messy painting, Little D asks you to write a program to maintain following operations. The specific format of these operations is as follows.

0 : clear all the points.

1 x y c : add a point which color is c at point (x,y).

2 x y1 y2 : count how many different colors in the square (1,y1) and (x,y2). That is to say, if there is a point (a,b) colored c, that 1ax and y1by2, then the color c should be counted.

3 : exit.
 

Input
The input contains many lines. 

Each line contains a operation. It may be '0', '1 x y c' ( 1x,y106,0c50 ), '2 x y1 y2' (1x,y1,y2106 ) or '3'. 

x,y,c,y1,y2 are all integers.

Assume the last operation is 3 and it appears only once.

There are at most 150000 continuous operations of operation 1 and operation 2. 

There are at most 10 operation 0. 

 

Output
For each operation 2, output an integer means the answer .
 

Sample Input
0 1 1000000 1000000 50 1 1000000 999999 0 1 1000000 999999 0 1 1000000 1000000 49 2 1000000 1000000 1000000 2 1000000 1 1000000 0 1 1 1 1 2 1 1 2 1 1 2 2 2 1 1 2 1 2 2 2 2 1 1 2 1 2 1 3 2 2 1 2 2 10 1 2 2 10 2 2 0 1 1 1 1 2 1 1 1 1 1 2 1 2 1 1 2 1 2 2 1 2 1 1 2 1 2 1 1 2 2 1 2 2 10 1 2 2 10 2 2 3
 

Sample Output
2 3 1 2 2 3 3 1 1 1 1 1 1 1
 
題意

  給我一個區間讓我們求出這個區間內有多少個不同的顏色

解題思路
  
  第一次做用的二分TLE , 本題用的線段樹,離散化的方式儲存了51棵虛樹 ,因爲只有51個點,構建51棵樹, 我們只需要維護縱座標就可以了 , 因爲橫座標固定了一個,只有一個橫座標是變化的,這樣子這個題就有點像區間內求最小值的問題,只是在這裏變成了區間(縱座標的區間)內求最近的橫座標,相互轉化一下就可以了,這裏的離散化處理很值得學習,數據結構學的東西都還給老師了真是慚愧。

代碼
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
const int maxn = 1000000 + 10 ;
int x , y , yc , cnt , flag , root[55] , lson[maxn] , rson[maxn] , v[maxn] ;
void update(int &node , int l , int r )
{
    int mid = (l + r) >> 1 ;
    if(!node)
    {
        node = cnt ;
        lson[node] = 0 ;
        rson[node] = 0 ;
        v[cnt++] = x ;
    }
    v[node] = min(v[node] , x) ;
    if(l == r)
        return ;
    if(y <= mid)
        update(lson[node] , l , mid ) ;
    else
        update(rson[node] , mid + 1 , r ) ;

}
void query(int node , int l , int r)
{
    if(flag || !node)
        return ;
    if(y <= l && r <= yc)
    {
        if(v[node] <= x)
            flag = 1 ;
        return ;
    }
    int mid = (l + r) >> 1 ;
    if(y <= mid)     ///愚了愚了 記錯了 給寫顛倒了
        query(lson[node] , l , mid) ;
    if(yc > mid)
        query(rson[node] , mid + 1 , r) ;

}
int main()
{
    int ca ;
    cnt = 1 ;
    memset(root , 0 , sizeof(root)) ;
    while(scanf("%d" , &ca) && ca != 3)
    {
        if(ca == 0) {
        memset(root , 0 , sizeof(root)) ;
        cnt = 1 ; continue ;
         }
        scanf("%d %d %d" , &x , &y , &yc) ;
        if(ca == 1)
        {
           update(root[yc] , 1 , 1000000) ;
        }
        if(ca == 2)
        {
            int ans = 0 ;
            for(int i = 0 ; i<= 50 ; i++)
          {
                flag = 0 ;
                query(root[i] , 1 , 1000000) ;
                ans += flag ;
          }
           printf("%d\n" , ans) ;
        }
    }
    return 0 ;
}


 

發佈了69 篇原創文章 · 獲贊 58 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章