POJ 2777 Count Color 線段樹

來源:互聯網
上載者:User

 題意:對長木板進行塗色,有兩種操作, 一種插入,一種統計,輸出一定範圍內的顏色種數。

#include<cstdio>#include<algorithm>#include<cstring>using namespace std;#define L(u) (u<<1)#define R(u) (u<<1|1)#define N 150000struct TreeNode { int l,r,c;};TreeNode node[N*3];int kind, mark[31];void build ( int u, int l, int r ){    node[u].l = l;    node[u].r = r;    node[u].c = 1;if ( l == r ) return;int mid = ( l + r ) >> 1;build ( L(u), l, mid );build ( R(u), mid + 1, r );}void update ( int u, int l, int r, int c ){    if ( node[u].c == c ) return;if ( node[u].l == l && node[u].r == r ){node[u].c = c; return;}if ( node[u].c != -1 ){    node[L(u)].c = node[R(u)].c = node[u].c;node[u].c = -1;    }int mid = (node[u].l + node[u].r) >> 1;if ( r <= mid ) update (L(u),l,r,c);else if ( l > mid ) update (R(u),l,r,c);else update (L(u),l,mid,c), update(R(u),mid+1,r,c);}void query ( int u, int l, int r ){if ( node[u].c != -1 )    {if( !mark[node[u].c] )        {            mark[node[u].c] = 1;            kind++;        }return;}int mid = (node[u].l + node[u].r) >> 1;if ( r <= mid ) query (L(u),l,r);else if ( l > mid ) query (R(u),l,r);else query(L(u),l,mid), query(R(u),mid+1,r);}int main(){int L, T, O, a, b, c;char oper[10];scanf("%d%d%d", &L, &T, &O);build ( 1, 1, L );while ( O-- ){scanf("%s %d %d",oper, &a, &b);if ( a > b ) swap(a,b);if ( oper[0] == 'P' ){kind = 0;memset(mark,0,sizeof(mark));query ( 1, a, b );printf("%d\n", kind );}else{scanf("%d",&c);update ( 1, a, b, c );}}return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.