Balanced Lineup(線段樹—指標實現)
線段樹這一類樹狀結構一般可以用兩種形式來實現—陣列和指標。 下面學習了一下別人的指標實現的線段樹。
和陣列實現的一樣分為三步:建樹,新增值,查詢。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int INF_MAX = -999999999; const int INF_MIN = 999999999; int n,q,a,b,t=1,MAX=INF_MAX,MIN=INF_MIN; struct node{ int l,r; int MAX,MIN; node *left, *right; }; node tree[1000000];//可以事先建立結點陣列,也可以在建樹的過程中動態建立結點,但是這樣可以節省遞迴清記憶體的時間 void built(node *root,int l,int r){ root->l = l; root->r = r; root->MAX = INF_MAX; root->MIN = INF_MIN; //將結點維護的最值初始化。 if(l!=r){ root->left = &tree[t++]; root->right = &tree[t++]; built(root->left,l,(root->l+root->r)/2); built(root->right,(root->l+root->r)/2+1,r); } } void update(node *root,int i,int v){ if(root->l == i&&root->r == i) { root->MAX = root->MIN = v; return ; //找到該結點就賦值 } root->MIN = min(root->MIN,v); //遞迴改變母節點的值 root->MAX = max(root->MAX,v); if(i<=(root->l+root->r)/2) update(root->left,i,v); else update(root->right,i,v); } void query(node *root,int l,int r){ if(root->MAX<=MAX&&root->MIN>=MIN) return ; if(l==root->l&&r==root->r) { MIN = min(root->MIN,MIN); MAX = max(root->MAX,MAX); return ; } if(r<=(root->l+root->r)/2) query(root->left,l,r); else if(l>=(root->l+root->r)/2+1) query(root->right,l,r); else { query(root->left,l,(root->l+root->r)/2); query(root->right,(root->l+root->r)/2+1,r); } } int main(){ scanf("%d%d",&n,&q); built(tree,1,n); for(int i=1;i<=n;i++){ scanf("%d",&a); update(tree,i,a); } while(q--){ scanf("%d%d",&a,&b); MAX = INF_MAX; MIN = INF_MIN; query(tree,a,b); printf("%d\n",MAX-MIN); } return 0; }
相關推薦
Balanced Lineup(線段樹—指標實現)
線段樹這一類樹狀結構一般可以用兩種形式來實現—陣列和指標。 下面學習了一下別人的指標實現的線段樹。 和陣列實現的一樣分為三步:建樹,新增值,查詢。 #include<cstdio> #include<cstring> #include<iost
Balanced Lineup(線段樹區間查詢)
D - Balanced LineupTime Limit: 5000 MS Memory Limit: 0 KB64-bit integer IO format: %I64d , %I64u Java class name: MainDescriptionFor the d
POJ 3264 Balanced Lineup(線段樹區間查詢)
Balanced Lineup Description For the daily milking, Farmer John'sNcows (1 ≤N≤ 50,000) always line
Balanced Lineup(線段樹的簡單了解)
個人 ica tree ngs mat can scanf rate class 個人心得:線段樹就是將一段序列拆分為一個個單獨的節點,不過每倆個節點又可以聯系在一起,所以就能很好的結合,比如這一題, 每次插入的時候都將這一段區間的最大最小值更新,就能大大減少時間。 這個線
POJ 3264 Balanced Lineup(線段樹 區間最值)
lld color href .org balanced stream ios void def 題目鏈接:http://poj.org/problem?id=3264 題意:n個數,給定m個區間,求出每個區間內最大值和最小值之差 題解:區間最值問題,挺裸的一道題
G - Balanced Lineup ( 線段樹+區間查詢無更新)
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to orga
Balanced Lineup (線段樹)
Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the
Balanced Lineup(線段樹單點或RMQ)
A - Balanced Lineup Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Description For the daily
[51nod 1208] Stars in Your Window(線段樹,掃描線)
51nod clas html 題目 val col while cto pro 題目鏈接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1208 題意:也是矩形框點問題,不過每個點有權值,希望
HDU 4027 Can you answer these queries?(線段樹區間開方)
sizeof sqrt .cn swap %d nes nts following clr Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 6576
51nod 1463 找朋友(線段樹+離線處理)
query max nbsp update ring tdi 包含 覆蓋 vector http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1463 題意: 思路: 好題! 先對所有查
HDU 3974 Assign the task(線段樹 時間戳)
truct stream char %d 節點 正在 原因 scanf 全部 題目鏈接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3974 題意:n名員工組成一棵樹,分配任務給其中一名員工,那麽他和他的手下(就是該節點
P2880 [USACO07JAN]平衡的陣容Balanced Lineup(RMQ的倍增模板)
code balanced .org pac pri ret 線段 iostream 元素 題面: P2880 [USACO07JAN]平衡的陣容Balanced Lineup RMQ問題:給定一個長度為N的區間,M個詢問,每次詢問Li到Ri這段區間元素的最大值/最小值。
最簡單的問題(重慶市第八屆大學生程序設計大賽D) (線段樹+離線思想)
return ans img 個數 pre 子序列 clear 可持久化 sort 考場上的時候直接一臉懵逼了,啥? 區間裏面又要求子區間,還TM有上下界? 略加思索後倒是發現沒有那麽麻煩,因為很容易得出如下結論: 1.對於一個滿足條件的區間[L , R],對於他所有
HDU 1264 Counting Squares(Hash)或者(線段樹+線掃描)
bsp pan 解決 printf 就是 大小 lag 線段 ash http://acm.hdu.edu.cn/showproblem.php?pid=1264 題意:給你矩形的左下角和右上角兩個坐標,讓你求這些矩形覆蓋的面積的大小!~ 分析:一看就是線段樹+線掃描的問題
2017 ICPC 西安站現場賽 A.XOR (線段樹+線性基)
getchar tput 線性 calculate ext following case all pri XORConsider an array A with n elements. Each of its element is A[i] (1 ≤ i ≤ n). Th
Lost Cows(線段樹+二分判定)
div ascend printf for () instead site uil red 4835: [Usaco2003 Open]Lost Cows Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 21 Solved:
降臨(線段樹優化dp)
main spa space odi pri line 除了 發現 獲得 降臨 選定點i會有代價\(c_i\),如果一個區間j內的點全被選擇,就可以獲得回報\(p_j\)。點數和區間個數\(<1e5\)。 還以為是線段樹優化網絡流(50萬個點200萬條邊看上去很可
BZOJ2212 Poi2011Tree Rotations(線段樹合並)
|| 線段樹 merge stdin using add clu ota pac 顯然子樹內的操作不會對子樹外產生影響。於是貪心,若交換之後子樹內逆序對減少就交換。 這個東西可以用權值線段樹計算。操作完畢後需要對兩棵權值線段樹合並,這個的復雜度是兩棵線段樹的重復節點個數。那
bzoj 2212 : [Poi2011]Tree Rotations (線段樹合並)
sin lse online space 量變 ++ ota hup 左右 題目鏈接:https://www.lydsy.com/JudgeOnline/problem.php?id=2212 思路:用線段樹合並求出交換左右兒子之前之後逆序對的數量,如果數量變小則交換.