poj 3277 City Horizon(線段樹#2----離散化)

需要離散化數據;


219ms


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

#define N 100010
typedef long long ll; 


struct node{
       int l, r;
       int height;  
}Tree[N * 5];
///

int O;
int op[N][3];
int dat[N]; 

void build(int l, int r, int x)
{
     Tree[x].l = l;
     Tree[x].r = r;
     Tree[x].height = 0; 
     if (l + 1 ==  r)return;
     int mid = (l + r) / 2;
     build(l, mid, x * 2);
     build(mid, r, x * 2 + 1);
}




 
void update(int l, int r, int val, int x)
{
       if (dat[Tree[x].l] == l && dat[Tree[x].r] == r)
       {
                Tree[x].height = max(Tree[x].height, val); 
                return; 
       }
       int mid = dat[(Tree[x].l + Tree[x].r) / 2];
       if (mid <= l)update(l, r, val, x * 2 + 1);
       else if (mid >= r)update(l, r, val, x * 2);
       else {
            update(l, mid, val, x * 2);
            update(mid, r, val, x * 2 + 1);
       }
}


ll find(int faHeight, int x)
{ 
       Tree[x].height = max(Tree[x].height, faHeight);
       
       if (Tree[x].l + 1 == Tree[x].r)
       {
                return (ll)(dat[Tree[x].r] - dat[Tree[x].l]) * Tree[x].height;
       } 
       return find(Tree[x].height, x * 2) + find(Tree[x].height, x * 2 + 1);      
}      


int main()
{
   // FILE* fp = fopen("in.txt", "r"); 
    scanf( "%d", &O);
    int len = 1; 
    for (int i = 0; i < O; i++)
    {
           scanf( "%d %d %d", op[i], op[i] + 1, op[i] + 2);
           dat[len++] = op[i][0];
           dat[len++] = op[i][1];
    }
    
    sort(dat + 1, dat + len);
    int n = unique(dat + 1, dat + len) - dat - 1;
    build (1, n, 1); 
    for (int i = 0; i < O; i++)
    {
          update(op[i][0], op[i][1], op[i][2], 1); 
    }
    printf("%lld\n", find(0, 1)); 
//cout << find(0, 1)<<endl;
   // getchar();
    return 0;
} 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章