POJ 2528 離散化+線段樹染色

來源:互聯網
上載者:User

九野的部落格,轉載請註明出處:http://blog.csdn.net/acmmmm/article/details/12027811

題意:

T個測試資料

n個操作

i.th line [u, v] 表示給區間 [u,v] 染上i色

 

問最後有幾種顏色

區間範圍很大,所以先離散化

注意區間更新的操作

 

#include <stdio.h>#include <string.h>#include <iostream>#include <math.h>#include <queue>#include <set>#define N 10100*2#define ll int#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) (x+y)>>1using namespace std;inline ll Min(ll a,ll b){return a>b?b:a;}inline ll Max(ll a,ll b){return a>b?a:b;}int n;int len[N][2];struct p{ll num,id;}line[N];bool cmp(p a,p b){return a.num<b.num;}struct node{int l,r;ll sum;}tree[N*4];void build(int l,int r,int id){tree[id].l=l , tree[id].r=r;tree[id].sum=0;if(l==r)return ;int mid = MID(l,r);build(l,mid,L(id));  build(mid+1,r,R(id));}void updata(int data,int l,int r,int id){if( l==tree[id].l && tree[id].r == r){ tree[id].sum=data; return ; }if(tree[id].sum!=0 && tree[id].sum!=data){tree[L(id)].sum=tree[id].sum;//注意當 當前區間有顏色且和要染的顏色不相同時:先把子區間染原色,再更新當前塗的顏色tree[R(id)].sum=tree[id].sum;tree[id].sum=0;}int mid=MID(tree[id].l,tree[id].r);if(r<=mid)updata(data, l, r, L(id));else if(mid<l)updata(data, l, r, R(id));else {updata(data, l, mid, L(id));updata(data, mid+1, r, R(id));}}int query(int pos,int id){if(tree[id].l == tree[id].r)return tree[id].sum;int mid = MID(tree[id].l,tree[id].r);if(pos<=mid)return query(pos, L(id));return query(pos, R(id));}set<ll>myset;void find_ans(int id){if(tree[id].sum!=0){myset.insert(tree[id].sum);return ;}if(tree[id].l == tree[id].r)return ;find_ans(L(id));find_ans(R(id));}int main(){int T,Cas=1,a,b,i;scanf("%d",&T);while(T--){scanf("%d",&n);for(i = 0;i < n;++i){//離散化scanf("%d%d",&len[i][0],&len[i][1]);line[2*i].num = len[i][0];line[2*i].id = -(i+1);//用負數表示 線段起點line[2*i+1].num = len[i][1];line[2*i+1].id = i+1;}sort(line,line+2*n,cmp);int temp = line[0].num, tp=1;for(i=0;i < 2*n;i++){if(line[i].num != temp){tp++;temp = line[i].num;}if(line[i].id < 0)len[-line[i].id-1][0] = tp;else len[ line[i].id-1][1] = tp;}build(1,tp,1);for(i=0;i<n;i++){//updata(i+1,len[i][0],len[i][1],1);updata(i+1,len[i][0],len[i][1],1);}myset.clear();find_ans(1);printf("%d\n",myset.size());}return 0;}/*9942 43 51 35 751 42 68 103 47 1031 101 36 1021 101 446 101 101 45 10ans:34322*/


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.