【第一個線段樹+第一個Java程式!】hdoj 1754 I Hate It

來源:互聯網
上載者:User

Java代碼。。。。。糾結了好久。。。。

 

import java.io.*;<br />import java.util.*;</p><p>class Tree {<br />public Tree L, R;<br />public int ValueL, ValueR;<br />public int maxscore;<br />};</p><p>public class Main {<br />static int score[] = new int[200010];<br />public static void main(String[] args) throws IOException<br />{<br />class IntervalTree {<br />public int pos;<br />public Tree nodes[] = new Tree[400010];<br />public IntervalTree()<br />{<br />for (int j = 0; j < 400010; j++) {<br />nodes[j] = new Tree();<br />}<br />}<br />public Tree Build(int L, int R)<br />{<br />Tree rootTree = nodes[pos++];<br />rootTree.ValueL = L;<br />rootTree.ValueR = R;<br />if (L == R) {<br />rootTree.maxscore = Math.max(score[L], score[R]);<br />return rootTree;<br />} else {<br />int mid;<br />mid = (L+R)/2;<br />rootTree.L = Build(L, mid);<br />rootTree.R = Build(mid+1, R);<br />rootTree.maxscore = Math.max(rootTree.L.maxscore, rootTree.R.maxscore);<br />}<br />return rootTree;<br />}</p><p>public int Query(Tree root, int LL, int RR)<br />{<br />int ret = 0;<br />if (LL == root.ValueL && RR == root.ValueR) {<br />return root.maxscore;<br />} else {<br />int mid;<br />mid = (root.ValueL+root.ValueR)/2;<br />if (RR <= mid)<br />ret = Query(root.L, LL, RR);<br />else if (LL > mid)<br />ret = Query(root.R, LL, RR);<br />else<br />ret = Math.max(Query(root.L, LL, mid), Query(root.R, mid + 1, RR));<br />return ret;<br />}<br />}</p><p>public int Update(Tree root, int LL, int RR, int value) {<br />int ret = 0;<br />if (LL == root.ValueL && RR == root.ValueR) {<br />return root.maxscore = value;<br />} else {<br />int mid;<br />mid = (root.ValueL + root.ValueR)/2;<br />if (RR <= mid)<br />ret = Update(root.L, LL, RR, value);<br />else if (LL > mid)<br />ret = Update(root.R, LL, RR, value);<br />else<br />ret = Math.max(Update(root.L, LL, mid, value), Update(root.R, mid + 1, RR, value));<br />root.maxscore = ret;<br />return ret;<br />}<br />}<br />}<br />// TODO Main Start<br />//System.setIn(new BufferedInputStream(new FileInputStream("c://1754.in")));</p><p>int n, m;<br />int a, b;<br />int i;<br />IntervalTree iTree = new IntervalTree();<br />Tree rootTree;<br />String cmd;<br />Scanner cin = new Scanner(new BufferedInputStream(System.in));<br />while (cin.hasNext())<br />{<br />n = cin.nextInt();<br />m = cin.nextInt();<br />for(i = 1; i<=n; i++)<br />score[i] = cin.nextInt();<br />iTree.pos = 0;<br />rootTree = iTree.Build(1, n);<br />for(i = 0; i<m; i++)<br />{<br />cmd = cin.next();<br />a = cin.nextInt();<br />b = cin.nextInt();<br />if (cmd.equals("Q")) {<br />if(a == b)<br />System.out.println(score[a]);<br />else<br />System.out.println(iTree.Query(rootTree, a, b));<br />} else {<br />score[a] = b;<br />iTree.Update(rootTree, a, a, b);<br />}<br />}<br />}<br />}<br />}

 

附上C++代碼。。。。

#include <cstdio><br />template <class T> inline T Max(T a, T b) { if (a < b) a = b; return a; }</p><p>int score[200010];</p><p>class Tree {<br />public:<br />Tree *L, *R;<br />int ValueL, ValueR;<br />int maxscore;<br />};<br />Tree nodes[400010];<br />class IntervalTree<br />{<br />public:<br />int pos;<br />IntervalTree()<br />{<br />pos = 0;<br />}</p><p>Tree* NewNode()<br />{<br />return &nodes[pos++];<br />}</p><p>Tree* Build(int L, int R)<br />{<br />Tree *rootTree = NewNode();<br />rootTree->ValueL = L;<br />rootTree->ValueR = R;<br />if (L == R) {<br />rootTree->maxscore = Max(score[L], score[R]);<br />return rootTree;<br />} else {<br />int mid;<br />mid = (L+R)/2;<br />rootTree->L = Build(L, mid);<br />rootTree->R = Build(mid+1, R);<br />rootTree->maxscore = Max(rootTree->L->maxscore, rootTree->R->maxscore);<br />}<br />return rootTree;<br />}</p><p>int Query(Tree* root, int LL, int RR)<br />{<br />int ret = 0;<br />if (LL == root->ValueL && RR == root->ValueR) {<br />return root->maxscore;<br />} else {<br />int mid;<br />mid = (root->ValueL+root->ValueR)/2;<br />if (RR <= mid)<br />ret = Query(root->L, LL, RR);<br />else if (LL > mid)<br />ret = Query(root->R, LL, RR);<br />else<br />ret = Max(Query(root->L, LL, mid), Query(root->R, mid + 1, RR));<br />return ret;<br />}<br />}</p><p>int Update(Tree* root, int LL, int RR, int value) {<br />int ret = 0;<br />if (LL == root->ValueL && RR == root->ValueR) {<br />return root->maxscore = value;<br />} else {<br />int mid;<br />mid = (root->ValueL + root->ValueR)/2;<br />if (RR <= mid)<br />ret = Update(root->L, LL, RR, value);<br />else if (LL > mid)<br />ret = Update(root->R, LL, RR, value);<br />else<br />ret = Max(Update(root->L, LL, mid, value), Update(root->R, mid + 1, RR, value));<br />root->maxscore = ret;<br />return ret;<br />}<br />}<br />};</p><p>int main()<br />{<br />int n, m;<br />int i, j;<br />Tree *rootTree;<br />IntervalTree iTree;<br />char cmd[3];<br />int a, b;<br />while (scanf("%d%d", &n, &m) != EOF)<br />{<br />for (i = 1; i<=n; i++)<br />scanf("%d", &score[i]);<br />iTree.pos = 0;<br />rootTree = iTree.Build(1, n);<br />for(i = 0; i<m; i++)<br />{<br />scanf("%s%d%d", cmd, &a, &b);<br />if (cmd[0] == 'Q') {<br />if(a == b)<br />printf("%d/n", score[a]);<br />else<br />printf("%d/n", iTree.Query(rootTree, a, b));<br />} else {<br />score[a] = b;<br />iTree.Update(rootTree, a, a, b);<br />}<br />}<br />}<br />return 0;<br />}<br />

相關文章

聯繫我們

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