ZOJ 1610 Count the Colors (线段树区间更新)
生活随笔
收集整理的這篇文章主要介紹了
ZOJ 1610 Count the Colors (线段树区间更新)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目鏈接
題意 : 一根木棍,長(zhǎng)8000,然后分別在不同的區(qū)間涂上不同的顏色,問(wèn)你最后能夠看到多少顏色,然后每個(gè)顏色有多少段,顏色大小從頭到尾輸出。
思路 :線段樹(shù)區(qū)間更新一下,然后標(biāo)記一下,最后從頭輸出。
//ZOJ 1610 #include <cstdio> #include <cstring> #include <iostream>using namespace std ;int p[8010*4],lz[8010*4] ,hashh[8010*4],hash1[8101*4];//void pushup(int rt) //{ // if(p[rt << 1] == p[rt << 1 | 1]) // p[rt] = p[rt << 1] ; // else p[rt] = -1 ; //} void pushdown(int rt) {if(lz[rt] != -1){lz[rt << 1] = lz[rt << 1 | 1] = lz[rt] ;p[rt << 1] = p[rt << 1 | 1] = lz[rt] ;lz[rt] = -1 ;} } //void build(int l,int r,int rt) //{ // lz[rt] = -1 ; // if(l == r) // { // p[rt] = -1 ; // return ; // } // int mid = (l + r) >> 1 ; // build(l,mid,rt << 1) ; // build(mid+1,r,rt << 1 | 1) ; // pushup(rt) ; //} void update(int L,int R,int l,int r,int rt,int sc) {if(l >= L && r <= R){lz[rt] = sc ;p[rt] = sc ;return ;}pushdown(rt) ;int mid = (l+r) >> 1;if(mid >= L)update(L,R,l,mid,rt << 1,sc) ;if(mid < R)update(L,R,mid+1,r,rt << 1 | 1 ,sc) ;// pushup(rt) ; } void query(int l,int r,int rt) {if(l == r){hashh[l] = p[rt] ;return ;}pushdown(rt) ;int mid = (l+r) >> 1 ;query(l,mid,rt << 1) ;query(mid+1,r,rt << 1 | 1) ; } void Init() {memset(lz,-1,sizeof(lz)) ;memset(hashh,0,sizeof(hashh)) ;memset(hash1,0,sizeof(hash1)) ;memset(p,-1,sizeof(p)) ; } int main() {int n ,x1,x2,c;while(cin >> n ){Init() ;for(int i = 0 ; i < n ; i++){cin >> x1 >> x2 >> c ;update(x1,x2-1,0,8000,1,c) ;}query(0,8000,1) ;for(int i = 1 ; i <= 8000 ; i++){if(hashh[i] != hashh[i-1]){if(hashh[i-1] != -1)hash1[hashh[i-1]] ++ ;}}if(hashh[8000] != -1)hash1[hashh[8000]]++ ;for(int i = 0 ; i <= 8000 ;i++){if(hash1[i]){printf("%d %d\n",i,hash1[i]) ;}}puts("") ;}return 0 ; } View Code?
轉(zhuǎn)載于:https://www.cnblogs.com/luyingfeng/p/3888890.html
超強(qiáng)干貨來(lái)襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的ZOJ 1610 Count the Colors (线段树区间更新)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 自动化运帷之分布式监控 zabbix 使
- 下一篇: NSUserDefaults的用法(轻量