Hdu 4417 Super Mario/decision tree, hdu4417

Source: Internet
Author: User

Hdu 4417 Super Mario/decision tree, hdu4417

Original question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 4417

The meaning of the question is very simple. Given a sequence, find the number of elements in the range [L, R,] smaller than or equal to H.

It seems that the functional Line Segment tree can be solved, but the weak sand tea has never understood its essence, so it has to use the tree to crush it.

The number tree covers each node of the line tree.

When you query the number of values in the [l, r] interval less than or equal to H, first use the line segment tree to find the corresponding interval,

Then, we can query the number of equal to or smaller than H in the corresponding balance tree in this interval and accumulate the number.

I always thought it would time out, and the result would be over 400 ms. The data should be weak (I made a group (N, M) A 10-W multi-o (cost □cost) o) was run at a scale of 2 s ).

1 # include <cstdio> 2 # include <cstdlib> 3 # include <cstring> 4 # include <algorithm> 5 # define lc root <1 6 # define rc root <1 | 1 7 const int Max_N = 100100; 8 struct SBT {9 int v, s, c; 10 SBT * ch [2]; 11 inline void set (int _ v = 0) {12 v = _ v, c = s = 1; 13 ch [0] = ch [1] = null; 14} 15 inline void push_up () {16 s = ch [0]-> s + ch [1]-> s + c; 17} 18 inline int cmp (int x) const {19 return v = x? -1: x> v; 20} 21} * null, stack [Max_N <3], * ptr [Max_N <2]; 22 int sz = 0, sum = 0, arr [Max_N]; 23 void init () {24 null = & stack [sz ++]; 25 null-> v = null-> s = null-> c = 0; 26} 27 inline void rotate (SBT * & x, int d) {28 SBT * k = x-> ch [! D]; 29 x-> ch [! D] = k-> ch [d]; 30 k-> ch [d] = x; 31 k-> s = x-> s ;; 32 x-> push_up (); 33 x = k; 34} 35 void Maintain (SBT * & x, int d) {36 if (x-> ch [d] = null) return; 37 if (x-> ch [d]-> ch [d]-> s> x-> ch [! D]-> s) {38 rotate (x ,! D); 39} else if (x-> ch [d]-> ch [! D]-> s> x-> ch [d]-> s) {40 rotate (x-> ch [d], d), rotate (x ,! D); 41} else {42 return; 43} 44 Maintain (x, 0), Maintain (x, 1); 45} 46 void insert (SBT * & x, int v) {47 if (x = null) {48 x = & stack [sz ++]; 49 x-> set (v ); 50} else {51 x-> s ++; 52 int d = x-> cmp (v); 53 if (-1 = d) {54 x-> c ++; 55 return; 56} 57 insert (x-> ch [d], v); 58 x-> push_up (); 59 Maintain (x, d); 60} 61} 62 int sbt_rank (SBT * x, int key) {63 int t, cur; 64 for (t = cur = 0; x! = Null;) {65 t = x-> ch [0]-> s; 66 if (key <x-> v) x = x-> ch [0]; 67 else if (key> = x-> v) cur + = x-> c + t, x = x-> ch [1]; 68} 69 return cur; 70} 71 void seg_built (int root, int l, int r) {72 ptr [root] = null; 73 for (int I = l; I <= r; I ++) insert (ptr [root], arr [I]); 74 if (l = r) return; 75 int mid = (l + r)> 1; 76 seg_built (lc, l, mid); 77 seg_built (rc, mid + 1, r); 78} 79 void seg_rank (int root, int l, int r, int x, int y, int v) {80 if (x> r | y <l) return; 81 if (x <= l & y> = r) {82 sum + = sbt_rank (ptr [root], v); 83 return; 84} 85 int mid = (l + r)> 1; 86 seg_rank (lc, l, mid, x, y, v); 87 seg_rank (rc, mid + 1, r, x, y, v); 88} 89 int main () {90 # ifdef LOCAL 91 freopen ("in.txt", "r", stdin); 92 freopen ("out.txt", "w +", stdout ); 93 # endif 94 int I, t, n, m, a, B, c, k = 1; 95 scanf ("% d", & t ); 96 while (t --) {97 sz = 0, init (); 98 scanf ("% d", & n, & m ); 99 printf ("Case % d: \ n", k ++); 100 for (I = 1; I <= n; I ++) scanf ("% d ", & arr [I]); 101 seg_built (1, 1, n); 102 while (m --) {103 scanf ("% d", &, & B, & c); 104 sum = 0; 105 seg_rank (1, 1, n, a + 1, B + 1, c ); 106 printf ("% d \ n", sum); 107} 108 return 0; 109}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.