zoj 1610 線段的塗色

2021-05-27 09:30:25 字數 916 閱讀 8426

要學會的是懶操作。這個題目跟北大的2777差不多,只是在計數的時候,有了變化。還有這個題目建樹的時候需要注意一下。我建樹建立錯誤,得到segmentation fault ,從來沒遇到過這樣的錯誤,現在長見識了。現在得好好總結一下這種線段樹的做法了。類似於線段塗色的這種題目,建樹要建立到元線段,而非點。更新的時候,要利用懶操作。沒必要更新到葉子節點,主要是那樣太浪費時間。如果更新父節點所在區間的一部分並且更新的時候同父節點原來儲存的資訊部喲樣的話,需要先把父節點的資訊傳到孩子節點,對父節點進行特殊的標記,這樣可以知道這個父節點所在區間儲存的資訊部唯一。這可能就是懶操作吧!感覺線段樹有很多的變形,很靈活,要想很好的掌握,還得多練習啊!

#include#include#includeusing namespace std;

#define n 8015

struct segtree

tree[n*4];

int len[n],col[n];

void buildtree(int root,int l,int r)

void updata(int root,int l,int r,int c)

if(tree[root].col==c)return;

if(tree[root].col>=0)

if(r<=tree[root].mid)updata(2*root,l,r,c);

else if(l>=tree[root].mid)updata(2*root+1,l,r,c);

else

}void count(int root,int l,int r)

}int main()

memset(col,-1,sizeof(col));

memset(len,0,sizeof(len));

count(1,0,8000);

for(int i=0;i

ZOJ 1610 思維 線段樹

zoj 1610 思維 include include using namespace std const int maxn 1e4 int n int e maxn 3 int color maxn int ans maxn int main for int i 1 i 8000 i 遍歷每乙個位...

ZOJ 1610 線段樹區間染色

給長度8000公尺的板,對其中區間染色,問最後能看到的顏色,和該顏色一共出現了幾段 線段覆蓋法 資料比較水 也可以暴力水過 線段樹 include stdio.h include string.h struct node data 40010 int color 8011 void build in...

ZOJ 1610 (線段樹區間set)

題意 在一段線段上塗色,新塗的顏色會覆蓋舊的,問最後每種顏色各有多少段。思路 線段樹區間覆蓋,最後dfs查詢一遍把每個位置的顏色查詢出來,再掃一遍統計有多少段。include include include include include include include include includ...