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

[hdu5204]水题

时间:2015-04-12 06:44:59      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

思路:插入的数按指数级增长,所以范围内最多存在logR个数。并且最近i次插入的数,首位置为2^(i-1),且每隔2^i出现一次,于是暴力之。。可以用插入排序维护,也可查询时在排下序。

一:

技术分享
  1 #pragma comment(linker, "/STACK:10240000,10240000")
  2 
  3 #include <iostream>
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <map>
  9 #include <queue>
 10 #include <deque>
 11 #include <cmath>
 12 #include <vector>
 13 #include <ctime>
 14 #include <cctype>
 15 #include <set>
 16 #include <bitset>
 17 #include <functional>
 18 #include <numeric>
 19 #include <stdexcept>
 20 #include <utility>
 21 
 22 using namespace std;
 23 
 24 #define mem0(a) memset(a, 0, sizeof(a))
 25 #define lson l, m, rt << 1
 26 #define rson m + 1, r, rt << 1 | 1
 27 #define define_m int m = (l + r) >> 1
 28 #define rep0(a, b) for (int a = 0; a < (b); a++)
 29 #define rep1(a, b) for (int a = 1; a <= (b); a++)
 30 #define all(a) (a).begin(), (a).end()
 31 #define lowbit(x) ((x) & (-(x)))
 32 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
 33 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
 34 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
 35 #define pchr(a) putchar(a)
 36 #define pstr(a) printf("%s", a)
 37 #define sint(a) ReadInt(a)
 38 #define sint2(a, b) ReadInt(a);ReadInt(b)
 39 #define sint3(a, b, c) ReadInt(a);ReadInt(b);ReadInt(c)
 40 #define pint(a) WriteInt(a)
 41 
 42 typedef double db;
 43 typedef long long LL;
 44 typedef pair<int, int> pii;
 45 typedef multiset<int> msi;
 46 typedef set<int> si;
 47 typedef vector<int> vi;
 48 typedef map<int, int> mii;
 49 
 50 const int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
 51 const int dy[8] = {1, 0, -1, 0, -1, 1, 1, -1};
 52 const int maxn = 1e3 + 7;
 53 const int maxm = 1e5 + 7;
 54 const int maxv = 1e7 + 7;
 55 const int max_val = 1e6 + 7;
 56 const int MD = 1e9 +7;
 57 const int INF = 1e9 + 7;
 58 const double PI = acos(-1.0);
 59 const double eps = 1e-10;
 60 
 61 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
 62 template<class T>void ReadInt(T &x){char c=getchar();while(!isdigit(c))c=getchar();x=0;while(isdigit(c)){x=x*10+c-0;c=getchar();}}
 63 template<class T>void WriteInt(T i) {int p=0;static int b[20];if(i == 0) b[p++] = 0;else while(i){b[p++]=i%10;i/=10;}for(int j=p-1;j>=0;j--)pchr(0+b[j]);}
 64 
 65 
 66 struct abc {
 67     pii a[100007];
 68     int l, r;
 69     void Init() { l = r = 0; }
 70     void push_back(int x) {
 71         a[r++] = make_pair(x, 0);
 72         for(int i = l; i < r - 1; i++) a[i].second++;
 73         if (r - l >= 62) {
 74             int pos;
 75             for (int i = l; i < r; i++) {
 76                 if (a[i].second == 61) {
 77                     pos = i;
 78                     break;
 79                 }
 80             }
 81             for (int i = pos; i > l; i--) a[i] = a[i - 1];
 82             l++;
 83         }
 84         int p = r - 1;
 85         while (p > l && a[p].first < a[p - 1].first) {
 86             swap(a[p], a[p - 1]);
 87             p--;
 88         }
 89     }
 90     pii &operator [] (int i) {
 91         return a[l + i];
 92     }
 93     int size() {
 94         return r - l;
 95     }
 96 };
 97 
 98 
 99 abc g;
100 
101 LL calc(LL x, LL pos) {
102     if (x < pos) return 0;
103     return (x - pos) / pos / 2 + 1;
104 }
105 int main() {
106     //freopen("in.txt", "r", stdin);
107     int n;
108     while (cin >> n) {
109         g.Init();
110         rep0(i, n) {
111             int id, w;
112             sint(id);
113             if (id == 1) {
114                 sint(w);
115                 g.push_back(w);
116             }
117             else {
118                 LL L, R, k;
119                 sint3(L, R, k);
120                 int sz = g.size();
121                 rep0(i, sz) {
122                     LL pos = 1LL << g[i].second, c = calc(R, pos) - calc(L - 1, pos);
123                     if (k <= c) {
124                         pint(g[i].first);
125                         pchr(\n);
126                         break;
127                     }
128                     k -= c;
129                 }
130             }
131         }
132     }
133     return 0;
134 }
View Code

二:

技术分享
  1 #pragma comment(linker, "/STACK:10240000,10240000")
  2 
  3 #include <iostream>
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <map>
  9 #include <queue>
 10 #include <deque>
 11 #include <cmath>
 12 #include <vector>
 13 #include <ctime>
 14 #include <cctype>
 15 #include <set>
 16 
 17 using namespace std;
 18 
 19 #define mem0(a) memset(a, 0, sizeof(a))
 20 #define lson l, m, rt << 1
 21 #define rson m + 1, r, rt << 1 | 1
 22 #define define_m int m = (l + r) >> 1
 23 #define rep0(a, b) for (int a = 0; a < (b); a++)
 24 #define rep1(a, b) for (int a = 1; a <= (b); a++)
 25 #define all(a) (a).begin(), (a).end()
 26 #define lowbit(x) ((x) & (-(x)))
 27 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
 28 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
 29 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
 30 #define pchr(a) putchar(a)
 31 #define pstr(a) printf("%s", a)
 32 #define sint(a) ReadInt(a)
 33 #define sint2(a, b) ReadInt(a);ReadInt(b)
 34 #define sint3(a, b, c) ReadInt(a);ReadInt(b);ReadInt(c)
 35 #define pint(a) WriteInt(a)
 36 
 37 typedef double db;
 38 typedef long long LL;
 39 typedef pair<int, int> pii;
 40 typedef multiset<int> msi;
 41 typedef set<int> si;
 42 typedef vector<int> vi;
 43 typedef map<int, int> mii;
 44 
 45 const int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
 46 const int dy[8] = {1, 0, -1, 0, -1, 1, 1, -1};
 47 const int maxn = 1e3 + 7;
 48 const int maxm = 1e5 + 7;
 49 const int maxv = 1e7 + 7;
 50 const int max_val = 1e6 + 7;
 51 const int MD = 1e9 +7;
 52 const int INF = 1e9 + 7;
 53 const double PI = acos(-1.0);
 54 const double eps = 1e-10;
 55 
 56 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
 57 template<class T>void ReadInt(T &x){char c=getchar();while(!isdigit(c))c=getchar();x=0;while(isdigit(c)){x=x*10+c-0;c=getchar();}}
 58 template<class T>void WriteInt(T i) {int p=0;static int b[20];if(i == 0) b[p++] = 0;else while(i){b[p++]=i%10;i/=10;}for(int j=p-1;j>=0;j--)pchr(0+b[j]);}
 59 
 60 struct abc {
 61     int a[110000];
 62     int l, r;
 63     void Init() { l = r = 0; }
 64     void push_back(int x) {
 65         a[r++] = x;
 66         if (r - l >= 62) {
 67             l++;
 68         }
 69     }
 70     int &operator [] (int i) {
 71         return a[l + i];
 72     }
 73     int size() {
 74         return r - l;
 75     }
 76 };
 77 
 78 abc g;
 79 
 80 pair<int, LL> a[100];
 81 
 82 LL calc(LL x, int id) {
 83     LL start = 1LL << (g.size() - id - 1), t = 1LL << (g.size() - id);
 84     if (x < start) return 0;
 85     return (x - start) / t + 1;
 86 }
 87 
 88 int main() {
 89     //freopen("in.txt", "r", stdin);
 90     int n;
 91     while (cin >> n) {
 92         g.Init();
 93         rep0(i, n) {
 94             int id, w;
 95             sint(id);
 96             if (id == 1) {
 97                 sint(w);
 98                 g.push_back(w);
 99             }
100             else {
101                 LL L, R, k;
102                 sint3(L, R, k);
103                 int total = 0, sz = g.size();
104                 rep0(i, sz) {
105                     LL c = calc(R, i) - calc(L - 1, i);
106                     if (c > 0) a[total++] = make_pair(g[i], c);
107                 }
108                 sort(a, a + total);
109                 int now = 0;
110                 while (1) {
111                     if (k <= a[now].second) {
112                         break;
113                     }
114                     k -= a[now++].second;
115                 }
116                 pint(a[now].first);
117                 pchr(\n);
118             }
119         }
120     }
121     return 0;
122 }
View Code

 

[hdu5204]水题

标签:

原文地址:http://www.cnblogs.com/jklongint/p/4418985.html

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