http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1112
用第k大数8s水过的题....如果计算复杂度,理论上是不应该可以通过的...这题正解是树套树,用线段树嵌套平衡树。(树套树版本已写,详见另一篇随笔)
记录一下水过去的代码:
View Code
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #include <cstdlib> 6 7 #define lson l, m, rt << 1 8 #define rson m + 1, r, rt << 1 | 1 9 #define root 0, mx - 1, 1 10 11 using namespace std; 12 13 #define debug 0 14 15 int min2(int _a, int _b){return _a < _b ? _a : _b;} 16 int max2(int _a, int _b){return _a > _b ? _a : _b;} 17 18 const int maxn = 50001; 19 int tmp[maxn], s[maxn]; 20 21 int k_elem(int n, int *a, int k){//二分搜索第k大数 22 int t, key; 23 int i, j, l = 0, r = n - 1; 24 while (l < r){ 25 for (key = a[((i = l - 1) + (j = r + 1)) >> 1]; i < j; ){ 26 for (j--; key < a[j]; j--); 27 for (i++; a[i] < key; i++); 28 if (i < j) t = a[i], a[i] = a[j], a[j] = t; 29 } 30 if (k > j) l = j + 1; 31 else r = j; 32 } 33 return a[k]; 34 } 35 36 int main(){ 37 int T, n, m; 38 int l, r, k; 39 char in[3]; 40 41 scanf("%d", &T); 42 while (T--){ 43 scanf("%d%d", &n, &m); 44 for (int i = 1; i <= n; i++){ 45 scanf("%d", &s[i]); 46 } 47 while (m-- && scanf("%s", in)){ 48 if (in[0] == 'Q'){ 49 scanf("%d%d%d", &l, &r, &k); 50 for (int i = l; i <= r; i++){ 51 tmp[i - l] = s[i]; 52 } 53 printf("%d\n", k_elem(r - l + 1, tmp, k - 1)); 54 } 55 else{ 56 scanf("%d%d", &l, &k); 57 s[l] = k; 58 } 59 } 60 } 61 62 return 0; 63 }
——written by Lyon