Hdu 4585 Shaolin, hdu4585shaolin

Source: Internet
Author: User

Hdu 4585 Shaolin, hdu4585shaolin

Original question link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action? Id = 46093

1 # include <cstdio> 2 # include <cstdlib> 3 # include <iostream> 4 # include <algorithm> 5 const int Max_N = 101000; 6 const int INF = 0x7fffffff; 7 struct Node {8 int v, id, s; 9 Node * ch [2]; 10 inline void 11 set (int _ v = 0, int _ id = 0, int _ s = 0, Node * p = NULL) {12 ch [0] = ch [1] = p; 13 v = _ v, id = _ id, s = _ s; 14} 15 inline void push_up () {16 s = ch [0]-> s + ch [1]-> s + 1; 17} 18 inline int cmp (in T x) const {19 return x> v; 20} 21}; 22 struct SizeBalanceTree {23 Node * tail, * root, * null; 24 Node stack [Max_N]; 25 void init () {26 tail = & stack [0]; 27 null = tail ++; 28 null-> set (); 29 root = null; 30} 31 inline Node * newNode (int v, int id) {32 Node * p = tail ++; 33 p-> set (v, id, 1, null ); 34 return p; 35} 36 inline void rotate (Node * & x, int d) {37 Node * k = x-> ch [! D]; 38 x-> ch [! D] = k-> ch [d]; 39 k-> ch [d] = x; 40 k-> s = x-> s; 41 x-> push_up (); 42 x = k; 43} 44 inline void Maintain (Node * & x, int d) {45 if (x-> ch [d] = null) return; 46 if (x-> ch [d]-> ch [d]-> s> x-> ch [! D]-> s) {47 rotate (x ,! D); 48} else if (x-> ch [d]-> ch [! D]-> s> x-> ch [! D]-> s) {49 rotate (x-> ch [d], d), rotate (x ,! D); 50} else {51 return; 52} 53 Maintain (x, 0), Maintain (x, 1); 54} 55 inline void insert (Node * & x, int v, int id) {56 if (x = null) {57 x = newNode (v, id); 58 return; 59} else {60 x-> s ++; 61 int d = x-> cmp (v); 62 insert (x-> ch [d], v, id ); 63 x-> push_up (); 64 Maintain (x, d); 65} 66} 67 inline void insert (int v, int id) {68 insert (root, v, id); 69} 70 inline Node * select (Node * x, int v, int Dx) {71 Node * pre = null; 72 while (x! = Null & x-> v! = V) {73 int d = x-> cmp (v); 74 if (d =! Dx) pre = x; 75 x = x-> ch [d]; 76} 77 x = x-> ch [dx]; 78 if (x = null) {79 return pre; 80} else {81 while (x-> ch [! Dx]! = Null) x = x-> ch [! Dx]; 82 return x; 83} 84} 85 inline int calc (Node * r, int k) {86 if (r = null) return INF; 87 return std :: abs (r-> v-k); 88} 89 inline void gogo (int id, int v) {90 int ans = 0; 91 Node * k1 = select (root, v, 0); 92 Node * k2 = select (root, v, 1); 93 int d1 = calc (k1, v), d2 = calc (k2, v ); 94 ans = d1> d2? K2-> id: k1-> id; 95 printf ("% d \ n", id, ans); 96} 97} sbt; 98 int main () {99 # ifdef LOCAL100 freopen ("in.txt", "r", stdin); 101 freopen ("out.txt", "w +", stdout); 102 # endif103 int n, id, v; 104 while (~ Scanf ("% d", & n) {105 sbt. init (); 106 sbt. insert (int) 1e9, 1); 107 for (int I = 0; I <n; I ++) {108 scanf ("% d", & id, & v); 109 sbt. insert (v, id); 110 sbt. gogo (id, v); 111} 112} 113 return 0; 114}View Code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.