HDU6183(線段樹)

Color it

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


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
 

Source

解題思路:剛開始覺得這題很麻煩,因爲可能會有點覆蓋的情況,就是一個點先被一種顏色覆蓋,然後被另外一種顏色覆蓋,那麼之前的顏色就沒有了,但是,這題不會出現這種情況,就算被覆蓋了,也算有兩種顏色,從樣例就可以看出,那麼問題就變得很簡單了,我們用50棵線段樹搞一搞就行,我們按y座標建樹,維護x的最小值(爲什麼這樣做,因爲每次詢問的矩形的左下點的橫座標是1,所以這樣做可以化雙邊界約束爲單邊界,我們維護最小值就行),這裏50棵線段樹可能會超內存,所以要邊更新,邊建樹,這樣動態建樹的話空間複雜度爲O(n * logn),因爲每次更新,最多消耗logn個頂點。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000 + 10;
int inf = 0x3f3f3f3f;
struct node
{
    int l, r;
    int lson, rson;
    int sum;
    int Min;
    node()
    {
        sum = 0;
        Min = inf;
        lson = rson = 0;
    }
}Node[maxn<<2];
int tot;
int root[55];
int judge;
void update(int &rt, int l, int r, int value, int Mi)
{
    if(!rt) rt = ++tot;
    Node[rt].l = l;
    Node[rt].r = r;
    Node[rt].sum++;
    Node[rt].Min = min(Node[rt].Min, Mi);
    if(l == r) return;
    int mid = (l + r)>>1;
    if(value <= mid) update(Node[rt].lson, l, mid, value, Mi);
    else update(Node[rt].rson, mid + 1, r, value, Mi);
}
void query(int rt, int l, int r, int up)
{
    if(!rt || judge) return;
    if(Node[rt].l == l && Node[rt].r == r)
    {
        if(Node[rt].Min <= up && Node[rt].sum > 0)
        {
            judge = 1;
            return;
        }
        return;
    }
    int mid = (Node[rt].l + Node[rt].r)>>1;
    if(r <= mid) query(Node[rt].lson, l, r, up);
    else if(l >= mid + 1) query(Node[rt].rson, l, r, up);
    else
    {
        query(Node[rt].lson, l, mid, up);
        query(Node[rt].rson, mid + 1, r, up);
    }

}
void init()
{
    tot = 0;
    memset(root, 0, sizeof(root));
    for(int i = 0; i < (maxn<<2); i++)
    {
        Node[i].lson = Node[i].rson = Node[i].sum = 0;
        Node[i].Min = inf;
    }
}
int main()
{
    int op;
    init();
    while(~scanf("%d", &op) && op != 3)
    {
        if(op == 0) init();
        else if(op == 1)
        {
            int x, y, c;
            scanf("%d%d%d", &x, &y, &c);
            update(root[c], 1 , 1000000, y, x);
        }
        else if(op == 2)
        {
            int x, y1, y2;
            scanf("%d%d%d", &x, &y1, &y2);
            int ans = 0;
            for(int i = 0; i <= 50; i++)
            {
                judge = 0;
                query(root[i], y1, y2, x);
                if(judge)
                {
                    ans++;
                }
            }
            printf("%d\n", ans);
        }
    }
    return 0;
}


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