[bzoj2243][SDOI2011]染色

來自FallDream 的博客,未經允許,請勿轉載,謝謝qaq


給定一棵有n個節點的無根樹和m個操作,操作有2類:

1、將節點a到節點b路徑上所有點都染成顏色c

2、詢問節點a到節點b路徑上的顏色段數量(連續相同顏色被認為是同一段),如“112221”由3段組成:“11”、“222”和“1”。

請你寫一個程序依次完成這m個操作。

n,m<=100000

?

題解:顏色信息考慮維護區間左右端點和答案,明顯可以合并,所以考慮樹剖之后線段樹維護。復雜度$O(nlog^{2}n)$

#include<iostream>
#include<cstdio>
#define pa pair<int,int>
#define mp(x,y) make_pair(x,y)
#define MN 100000
using namespace std;
inline int read()
{int x = 0 , f = 1; char ch = getchar();while(ch < '0' || ch > '9'){ if(ch == '-') f = -1;  ch = getchar();}while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}return x * f;
}struct data{int l,r,x;data(int _x=0):l(_x),r(_x){x=1;}data(int x,int y,int z):l(x),r(y),x(z){}data operator+(data b){return data(l,b.r,x+b.x-(r==b.l));}data operator~(){return data(r,l,x);}
};
struct node{int l,r,val;bool tag;data x;}T[MN*4+6];
struct edge{int to,next;}e[MN*2+5];
int n,m,head[MN+5],a[MN+5],cnt=0,fa[MN+5],top[MN+5],mx[MN+5],size[MN+5],dfn[MN+5],dn=0,p[MN+5],dep[MN+5];
pa q1[MN+5],q2[MN+5];int top1,top2;
char op[5];
inline void ins(int f,int t)
{e[++cnt]=(edge){t,head[f]};head[f]=cnt;e[++cnt]=(edge){f,head[t]};head[t]=cnt;
}void dfs1(int x,int f)
{fa[x]=f;size[x]=1;mx[x]=0;for(int i=head[x];i;i=e[i].next)if(e[i].to!=f){dep[e[i].to]=dep[x]+1;dfs1(e[i].to,x);size[x]+=size[e[i].to];if(size[e[i].to]>size[mx[x]]) mx[x]=e[i].to;}
}void dfs2(int x,int tp)
{top[x]=tp;p[dfn[x]=++dn]=x;if(mx[x]) dfs2(mx[x],tp);for(int i=head[x];i;i=e[i].next)if(e[i].to!=fa[x]&&e[i].to!=mx[x])dfs2(e[i].to,e[i].to);
}void pushdown(int x)
{int l=x<<1,r=x<<1|1;T[l].x=T[r].x=data(T[x].val);T[l].val=T[r].val=T[x].val;T[x].val=0;T[l].tag=T[r].tag=1;T[x].tag=0;
}void build(int x,int l,int r)
{if((T[x].l=l)==(T[x].r=r)){T[x].x=data(a[p[l]]);return;}int mid=l+r>>1;build(x<<1,l,mid);build(x<<1|1,mid+1,r);T[x].x=T[x<<1].x+T[x<<1|1].x;
}void renew(int x,int l,int r,int c)
{if(T[x].l==l&&T[x].r==r){T[x].x=data(c);T[x].val=c;T[x].tag=1;return;}if(T[x].tag) pushdown(x);int mid=T[x].l+T[x].r>>1;if(r<=mid) renew(x<<1,l,r,c);else if(l>mid) renew(x<<1|1,l,r,c);else renew(x<<1,l,mid,c),renew(x<<1|1,mid+1,r,c);T[x].x=T[x<<1].x+T[x<<1|1].x;
}void Renew(int y,int x,int c)
{while(top[x]!=top[y]){if(dep[top[x]]<dep[top[y]]) swap(x,y);renew(1,dfn[top[x]],dfn[x],c);x=fa[top[x]];}renew(1,min(dfn[y],dfn[x]),max(dfn[x],dfn[y]),c);
}data query(int x,int l,int r)
{if(T[x].l==l&&T[x].r==r) return T[x].x;if(T[x].tag) pushdown(x);int mid=T[x].l+T[x].r>>1;if(r<=mid) return query(x<<1,l,r);else if(l>mid) return query(x<<1|1,l,r);else return query(x<<1,l,mid)+query(x<<1|1,mid+1,r);
}int Query(int x,int y)
{top1=top2=0;data ans;bool flag=true;while(top[x]!=top[y]){if(dep[top[x]]>dep[top[y]])    {q1[++top1]=mp(dfn[top[x]],dfn[x]);x=fa[top[x]];}else{q2[++top2]=mp(dfn[top[y]],dfn[y]);y=fa[top[y]];}}    for(int i=1;i<=top1;i++) ans=flag?(flag=false,~query(1,q1[i].first,q1[i].second)):(ans+~query(1,q1[i].first,q1[i].second));if(dep[x]<dep[y]) ans=flag?(flag=false,query(1,dfn[x],dfn[y])):(ans+query(1,dfn[x],dfn[y]));else ans=flag?(flag=false,~query(1,dfn[y],dfn[x])):(ans+~query(1,dfn[y],dfn[x]));for(int i=top2;i;i--) ans=ans+query(1,q2[i].first,q2[i].second);return ans.x;
}int main()
{n=read();m=read();for(int i=1;i<=n;i++ )a[i]=read();for(int i=1;i<n;i++) ins(read(),read());dfs1(1,0);dfs2(1,1);build(1,1,n);for(int i=1;i<=m;i++){scanf("%s",op+1);int x=read(),y=read();if(op[1]=='Q') printf("%d\n",Query(x,y));else Renew(x,y,read());}return 0;
}

?

轉載于:https://www.cnblogs.com/FallDream/p/bzoj2243.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/252922.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/252922.shtml
英文地址,請注明出處:http://en.pswp.cn/news/252922.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Linux學習筆記——例說makefile 增加宏定義

從學習C語言開始就慢慢開始接觸makefile&#xff0c;查閱了很多的makefile的資料但總感覺沒有真正掌握makefile&#xff0c;如果自己動手寫一個makefile總覺得非常吃力。所以特意借助博客總結makefile的相關知識&#xff0c;通過例子說明makefile的具體用法。 例說makefile…

Android基本組件是什么?

1、ImageView繼承View組件,不單單用于顯示圖片,用 XML代碼 編寫的Drawable也可以顯示出來。其中的XML屬性 android:scaleType(設置圖片如何縮放或移動以適應ImageView的大小) 有很多的屬性值,如:matrix(使用矩形方式進行縮放)fitXY(對圖片橫向縱向縮放)center(圖片放在ImageVie…

Java 運算符及優先級

運算符 分割符&#xff1a;  ,  ;  []  ()算數運算符&#xff1a;    -  *  /  %    --關系運算符&#xff1a;  >  <  >  <    !邏輯運算符&#xff1a;  !  &  |  ^  &&  ||賦值運算符&#xff1a; …

array sort - 2 : quick sort

遞歸實現&#xff1a; #include <stdio.h>int arr[10] {3, 2, 4, 1, 9, 7, 5, 6, 0, 8};void print_array(){ int i 0; for (i 0; i < 10; i) printf("arr[%d]:%d ", i, arr[i]); printf("\n");}void swap(int *i, int *j){ …

Linux C 讀取文件夾下所有文件(包括子文件夾)的文件名

本文&#xff1a;http://www.cnblogs.com/xudong-bupt/p/3504442.html Linux C 下面讀取文件夾要用到結構體struct dirent&#xff0c;在頭#include <dirent.h>中&#xff0c;如下&#xff1a; #include <dirent.h> struct dirent {long d_ino; /* inode number 索…

報表工具實現單據套打

【摘要】 單據套打再也不用手動測量&#xff0c;反復調試了&#xff0c;報表工具實現單據套打&#xff0c;去乾學院看個究竟&#xff1a;報表工具實現單據套打!實際項目開發中&#xff0c;很多情況會涉及到單據的打印。即在一張印刷好的空白單據上&#xff0c;準確無誤地打印上…

每隔10秒鐘打印一個“Helloworld”

/*** 每隔10秒鐘打印一個“Helloworld”*/ public class Test03 {public static void main(String[] args) throws InterruptedException {ThreadImp threadImp new ThreadImp();Thread thread1 new Thread(threadImp);thread1.start();} }class ThreadImp extends Thread {p…

C++ STL 優先隊列

//優先隊列//Priority_queue //STL#include<iostream>#include<cstdio>#include<cstdlib>#include<queue>using namespace std;struct cmp{ bool operator() (const int a,const int b) const{//用const定義的a,b是包裹著變量外衣的常數&#xff0c;不…

GDB調試core文件樣例(如何定位Segment fault)

core dump又叫核心轉儲, 當程序運行過程中發生異常, 程序異常退出時, 由操作系統把程序當前的內存狀況存儲在一個core文件中, 叫core dump. (Linux中如果內存越界會收到SIGSEGV信號&#xff0c;然后就會core dump) 在程序運行的過程中&#xff0c;有的時候我們會遇到Segment fa…

管理信息系統的開發與管理

{% extendsmuban.html %} {% block head %}輸入{% endblock %} {% block main %} <div><div class"form-group"><label for"question">標題</label><textarea class"form-control" cols"50" rows"2&q…

python11-28筆記(1.6-1.7)

1.6 多類型傳值和冗余參數多類型傳值&#xff1a;比如def fun(x,y)&#xff0c;定義2個形參定義一個元組t(1,2),如果把元組當做實參傳入到函數中&#xff0c;會報錯 如何將元組當做不同類型的參數傳入到函數中fun(t) 代表傳入的是元組或者這樣調用fun((1,2))注意實參的個數要和…

session機制詳解以及session的相關應用

session是web開發里一個重要的概念&#xff0c;在大多數web應用里session都是被當做現成的東西&#xff0c;拿來就直接用&#xff0c;但是一些復雜的web應用里能拿來用的session已經滿足不了實際的需求&#xff0c;當碰到這樣的情況時候我們需要更加深入的理解session的機制&am…

(轉)Shell中獲取字符串長度的七種方法

Shell中獲取字符串長度的七種方法 原文&#xff1a;http://blog.csdn.net/jerry_1126/article/details/51835119 求字符串操作在shell腳本中很常用&#xff0c;下面歸納、匯總了求字符串的幾種可能方法: 【方法一】:利用${#str}來獲取字符串的長度 【方法二】:利用awk的length方…

linux下用core和gdb查詢出現段錯誤的地方

有些時候我們在一段C代碼的時候&#xff0c;由于對一個非法內存進行了操作&#xff0c;在程序運行的過程中&#xff0c;出現了"段錯誤"。呵呵&#xff0c;這種問題我想很多人會經常遇到。遇到這種問題是非常無語的&#xff0c;只是提示了"段錯誤"&#xff…

第一篇-Html標簽中head標簽,body標簽中input系列,textarea和select標簽

第十四周課程&#xff08;1-12章節&#xff09; HTML 裸體 CSS 穿華麗衣服 Javascript 動起來 一 HTML &#xff08;20個標簽&#xff09; 1.我們的瀏覽器是socket客戶端 2.一套規則&#xff0c;瀏覽器認識的規則 3.開發者&#xff1a; 學習html規則 開發后臺程序&#xff1a…

opencv3.2.0 Cmake 3.8.0 + tdm-gcc-5.1.0-3

實測 tdm-gcc-5.1.0-3 tdm32-1 32位版本無法正確編譯Opencv 3.2.0 會遇到諸多編譯問題 解決辦法 使用tdm-gcc-5.1.0-2 tdm64-1 64位版本轉載于:https://www.cnblogs.com/fundou/p/6710209.html

什么是商品屬性

一、什么是商品屬性&#xff1a; Definition of Product Attributes A product attribute is a characteristic that defines a particular product and will affect a consumers purchase decision. Product attributes can be tangible (or physical in nature) or intangibl…

linux用戶管理(1)----創建用戶(adduser和useradd)和刪除用戶(userdel)

arm linux的系統用戶管理&#xff1a; 1、刪除root用戶&#xff1a;deluser root2、刪除tt用戶:deluser tt3、建立root用戶&#xff1a;adduser root4、修改用戶密碼&#xff1a;登錄相應的用戶后&#xff0c;用passwd來修改密碼4、linux用戶和密碼的管理&#xff08;ftp&#…

前端性能優化之圖像優化原理

前端性能優化中&#xff0c;圖像的優化是非常重要的一環&#xff0c;為什么要說圖像的優化呢&#xff0c;而不是我們常見的圖片優化&#xff1f;因為這里的圖像包括矢量圖和位圖&#xff0c;我們常說的圖片優化是指位圖的優化。這篇文章轉載至奇舞周刊&#xff0c;大佬總結的非…

Lua開發學習4-普通循環和迭代器循環

說句實話&#xff0c;每當看到Lua代碼&#xff0c;我都感覺是半個SQL代碼&#xff0c;寫起來還是感覺有點恐怖。 while循環&#xff1a; 與C#的while循環類似&#xff0c;沒有什么好說的&#xff1b; --------Lua的while循環 while(condition)dostatementsend For循環 exp1為起…