码迷,mamicode.com
首页 > 其他好文 > 详细

POJ2761---Feed the dogs (Treap求区间第k大)

时间:2015-03-09 23:49:25      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

题意 就是求区间第k大,区间 不互相包含。

尝试用treap解决一下 第k大的问题。

  1 #include <set>
  2 #include <map>
  3 #include <cmath>
  4 #include <ctime>
  5 #include <queue>
  6 #include <stack>
  7 #include <cstdio>
  8 #include <string>
  9 #include <vector>
 10 #include <cstdlib>
 11 #include <cstring>
 12 #include <iostream>
 13 #include <algorithm>
 14 using namespace std;
 15 typedef unsigned long long ull;
 16 typedef long long ll;
 17 const int inf = 0x3f3f3f3f;
 18 const double eps = 1e-8;
 19 const int maxn = 1e5+10;
 20 struct Node
 21 {
 22     Node *ch[2];
 23     int key, siz, r;
 24     Node (int val)
 25     {
 26         ch[0] = ch[1] = NULL;
 27         siz = 1;
 28         key = val;
 29         r = rand();
 30     }
 31     bool operator < (const Node &rhs)const
 32     {
 33         return r < rhs.r;
 34     }
 35     int cmp(int x)const
 36     {
 37         if (key == x)
 38             return -1;
 39         return key > x ? 0 : 1;
 40     }
 41     void maintain()
 42     {
 43         siz = 1;
 44         if (ch[0] != NULL)
 45             siz += ch[0] -> siz;
 46         if (ch[1] != NULL)
 47             siz += ch[1] -> siz;
 48     }
 49 };
 50 void rotate(Node* &root, int d)
 51 {
 52     Node *tmp = root -> ch[d^1];
 53     root -> ch[d^1] = tmp -> ch[d];
 54     tmp -> ch[d] = root;
 55     root -> maintain();
 56     tmp -> maintain();
 57     root = tmp;
 58 }
 59 void insert (Node* &root, int x)
 60 {
 61     if (root == NULL)
 62         root = new Node (x);
 63     else
 64     {
 65         int d = x < root -> key ? 0 : 1;
 66         insert(root -> ch[d], x);
 67         if (root -> ch[d] -> r > root -> r)
 68             rotate(root, d^1);
 69     }
 70     root -> maintain();
 71 }
 72 void dele(Node* &root, int x)
 73 {
 74     int d = root -> cmp(x);
 75     if (d == -1)
 76     {
 77         Node* tmp = root;
 78         if (root -> ch[0] != NULL && root -> ch[1] != NULL)
 79         {
 80             int d1 = (root -> ch[0] -> r > root -> ch[1] -> r  ? 1 : 0);
 81             rotate(root, d1);
 82             dele(root -> ch[d1], x);
 83         }
 84         else
 85         {
 86             if (root -> ch[0] == NULL)
 87                 root = root -> ch[1];
 88             else
 89                 root = root -> ch[0];
 90             delete tmp;
 91         }
 92     }
 93     else
 94         dele(root -> ch[d], x);
 95     if (root != NULL)
 96         root -> maintain();
 97 }
 98 int Get_kth(Node* root,int k)
 99 {
100     if (root == NULL || k <= 0 || k > root -> siz)
101         return 0;
102     int s = (root -> ch[0] == NULL ? 0 : root -> ch[0] ->siz);
103     if (s + 1 == k)
104         return root -> key;
105     else if (s >= k)
106         return Get_kth(root -> ch[0], k);
107     else
108         return Get_kth(root -> ch[1], k - s -1);
109 }
110 struct Query
111 {
112     int l, r, k,idx;
113     bool operator < (const Query &rhs)const
114     {
115         return r < rhs.r;
116         //return l < rhs.l || (l == rhs.l&&r < rhs.r);
117     }
118 }Q[50005];
119 int a[100005], ans[50005];
120 Node* treap;
121 int main()
122 {
123     #ifndef ONLINE_JUDGE
124         freopen("in.txt","r",stdin);
125     #endif
126     int n, m;
127     while  (~scanf ("%d%d",&n, &m))
128     {
129         for (int i = 0; i < n; i++)
130             scanf ("%d", a+i+1);
131         for (int i = 0; i < m; i++)
132         {
133             scanf ("%d%d%d",&Q[i].l, &Q[i].r, &Q[i].k);
134             Q[i].idx = i;
135             if (Q[i].l > Q[i].r)
136                 swap(Q[i].l,Q[i].r);
137         }
138         sort (Q, Q+m);
139         int index = Q[0].l, lidx = 0;
140         for (int i = 0; i < m; i++)
141         {
142             for (int j = lidx; j && j < min(Q[i].l, Q[i-1].r+1); j++)
143                 dele(treap, a[j]);
144             lidx = Q[i].l;
145             for (int j = max(Q[i].l,index); j <= Q[i].r; j++)
146             {
147                 insert(treap, a[j]);
148             }
149             index = Q[i].r+1;
150             ans[Q[i].idx] = Get_kth(treap, Q[i].k);
151 
152         }
153         for (int i = 0; i < m; i++)
154         {
155             printf("%d\n",ans[i]);
156         }
157     }
158     return 0;
159 }

 

POJ2761---Feed the dogs (Treap求区间第k大)

标签:

原文地址:http://www.cnblogs.com/oneshot/p/4324763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!