【第一个线段树+第一个Java程序!】hdoj 1754 I Hate It

Java代码。。。。。纠结了好久。。。。

 

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

 

附上C++代码。。。。

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

你可能感兴趣的:(java,cmd,tree,Class,query,Build)