double kth(vector<int>& nums1, int l1, vector<int>& nums2, int l2, int n) // n: Median position (zero-based index) { if (l1 >= nums1.size())return nums2[l2 + n]; if (l2 >= nums2.size())return nums1[l1 + n]; if (0 == n)return nums1[l1] < nums2[l2] ? nums1[l1] : nums2[l2]; if (1 == n) { if (nums1[l1] < nums2[l2]) l1++; else l2++; n--; return kth(nums1, l1, nums2, l2, n); } int anchor1 = l1 + n / 2 < nums1.size() ? l1 + n / 2 : nums1.size(); int anchor2 = l2 + n / 2 < nums2.size() ? l2 + n / 2 : nums2.size(); if (nums1[anchor1-1] < nums2[anchor2-1]) { n -= anchor1 - l1; l1 = anchor1; } else { n -= anchor2 - l2; l2 = anchor2; } return kth(nums1, l1, nums2, l2, n); }
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { bool odd = (nums1.size() + nums2.size()) % 2; int mid = (nums1.size() + nums2.size() - 1) / 2; if (odd)return kth(nums1, 0, nums2, 0, mid); else return (kth(nums1, 0, nums2, 0, mid) + kth(nums1, 0, nums2, 0, mid + 1)) / 2.0; }
int kth(node *t,int k) { if(k<=t->l->size()) return kth(t->l,k) ; else if(k>t->l->size()+1) return kth(t->r,k-t->l->size()-1) ; return t->v ; }
int kth(Treap *&p, int k) { int Lsize = p -> left -> size; if(k <= Lsize) return kth(p -> left, k); else if(k == Lsize + 1) return p -> key; else return kth(p -> right, k - Lsize - 1); }
int kth(TreeNode *node, int &cur, int k) { if (!node) return 0; int val = kth(node->left, cur, k); if (cur == k) return val; cur++; if (cur == k) return node->val; return kth(node->right, cur, k); }
double findMedianSortedArrays(int a[], int m, int b[], int n) { int k = (m+n)/2; int m1 = kth(a,m,b,n,k+1); if ((m+n)%2==0) { int m2 = kth(a,m,b,n,k); return ((double)m1+m2)/2.0; } return m1; }
int kth(int a[], int m, int b[], int n, int k) { if (m < n) return kth(b,n,a,m,k); if (n==0) return a[k-1]; if (k==1) return min(a[0],b[0]); int j = min(n,k/2); int i = k-j; if (a[i-1] > b[j-1]) return kth(a,i,b+j,n-j,k-j); return kth(a+i,m-i,b,j,k-i); }
Node * kth(Node* root, int k) { int leftsize = 0; if (root->left != NULL) leftsize = root->left->size; if (k <= leftsize) return kth(root->left, k); if (k == leftsize + 1) return root; return kth(root->right, k - leftsize - 1); }
int kth(Node* o,int k) { // ÕÒµ½µÚK´óµÄÔªËØ if(o == NULL || k <= 0 || k > o->s) return 0; int s = (o->ch[1] == NULL ? 0 : o->ch[1]->s); if(k == s+1) return o->v; else if(k <= s) return kth(o->ch[1],k); else return kth(o->ch[0],k-s-1); }
int kth(int s, int e, int k) { int rn = s + rand() % (e - s), f, i; swap(a + rn, a + s); for(i = s + 1, f = s + 1; i < e; i++) { if(a[i] < a[s]) { swap(a + f, a + i); f++; } } f--; swap(a + f, a + s); if(f == k) return a[f]; else if(f < k) return kth(f + 1, e, k); else return kth(s, f, k); }
/* * param k : description of k * param nums : description of array and index 0 ~ n-1 * return: description of return */ int kthLargestElement(int k, vector<int> nums) { // write your code here int length = nums.size(); if(length == 0 || k > length) return 0; return kth(k, nums, 0, length-1); }
int main() { //srand(time(0)); // Use it, and get "RE" at BZOJ null = new Treap; root = null; scanf("%d%d", &m, &Limit); int delta = 0; while(m--) { char op; int x; scanf(" %c%d", &op, &x); if(op == 'I') { if(x < Limit) continue; insert(root, x - delta); } else if(op == 'A') delta += x; else if(op == 'S') { delta -= x; remove(root, Limit - delta); } else { x = root -> size - x + 1; if(x <= 0) puts("-1"); else printf("%d\n", kth(root, x) + delta); } } printf("%d\n", leave); return 0; }
void main() { char k[30]; gets(k); kth(k); }
void main() { int i, k; srand(time(NULL)); scanf("%d", &n); for(i = 0; i < n; i++) scanf("%d", a + i); scanf("%d", &k); printf("%d\n", kth(0, n, k - 1)); }
int kth(vector<int>& nums, int left, int right, int k) { if (left == right) { return nums[left]; } int pivot = nums[right]; int lp = left, rp = right; while (lp < rp) { while (lp < rp && nums[lp] <= pivot) ++lp; if (lp == rp) break; nums[rp] = nums[lp]; while (lp < rp && nums[rp] > pivot) --rp; if (lp == rp) break; nums[lp] = nums[rp]; } nums[lp] = pivot; if (k == lp - left + 1) return pivot; else if (k < lp - left + 1) return kth(nums, left, lp - 1, k); else return kth(nums, lp + 1, right, k - (lp - left + 1)); }
int kth(int k, vector<int> &nums, int s, int e) { if(s == e) return nums[s]; int p = s+1, q = s+1; while(q <= e) { if(nums[q] > nums[s]) { swap(nums[q], nums[p]); p++; } q++; } p--; swap(nums[p], nums[s]); if(p-s == k-1) return nums[p]; else if(p-s > k-1) return kth(k, nums, s, p); else return kth(k-p+s-1, nums, p+1, e); }
int main() { Node * root = insert(root, new Node(0)); for (int i = 1; i <= 10; ++i) { root = insert(root, new Node(i)); } Node * node = kth(root, 3); printf("%d\n", node->key); return 0; }
ListNode<T>* kth(int k, int &i, ListNode<T> *cur) { if (cur == nullptr) { return nullptr; } ListNode<T> *res = kth(k, i, cur->prev); i += 1; if (i == k) { // Unwiding stack. return cur; } return res; }
void solve() { char ch[10]; int a,b; cnt_query = 0; int sum = 0; while(scanf("%s",ch),'E' != ch[0]) { if('Q' == ch[0]) { scanf("%d%d",&a,&b); op[sum] = Command(0,a,b); } else if('D' == ch[0]) { scanf("%d",&a); op[sum] = Command(1,a,0); exist[a] = false; } else { scanf("%d%d",&a,&b); op[sum] = Command(2,a,wei[a]); wei[a] = b; } sum ++; } init(); long long ret = 0; for(int i = sum - 1;i >= 0; i--) { if(0 == op[i].type) { int x = op[i].a; int k = op[i].b; x = find(x); cnt_query ++; ret += (long long)kth(root[x],k); } else if(1 == op[i].type) { int x = op[i].a; add_edge(edge[x].first,edge[x].second); } else { int x = op[i].a; int v = op[i].b; int rt = find(x); remove(root[rt],wei[x]); insert(root[rt],v); wei[x] = v; } } printf("%.6lf\n",(double) ret / (double)cnt_query); }
void solve () { scanf ("%ld", &n); for (logn = 1; (1 << logn) <= n; logn++); logn--; for (int i = 0; i < n; i++) for (int j = 0; j <= logn; j++) d[i][j] = -1; for (long i = 1, f, t; i < n; i++) { scanf ("%ld%ld", &f, &t); --f; --t; scanf ("%ld", &g[f][t]); g[t][f] = g[f][t]; } dfs(0); while (true) { scanf ("%s", s); k = 0; if (!strcmp("DONE", s)) break; if (!strcmp("KTH", s)) { scanf ("%ld %ld %ld ", &A, &B, &k); printf("%ld\n", kth(A - 1, B - 1, k) + 1); } else { scanf ("%ld%ld", &A, &B); lca(A - 1,B - 1); printf ("%ld\n", res); } } puts(""); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) g[i][j] = 0; for (int i = 0; i < n; i++) dist[i] = u[i] = h[i] = 0; }
inline const T&operator[](int k){ return kth(k); }
struct elem * kth(struct elem *const e, const unsigned int k) { unsigned int ls; return ls = size(e->left), k < ls ? kth(e->left, k) : k == ls ? e : kth(e->right, k - ls - 1); }
Node* kth(Node* n, int k) { int leftsz = n->child[0].size; if (k == leftsz) return n; if (k < leftsz) return kth(n->child[0], k); else return kth(n->child[1], k - leftsz - 1); }
int findKthLargest(vector<int>& nums, int k) { int len = nums.size(); k = len - k + 1; return kth(nums, 0, len - 1, k); }
int kthSmallest(TreeNode* root, int k) { int cur = 0; return kth(root, cur, k); }
T DoublyLinkedList<T>::kthToLastElement(int k) { int i = 0; return kth(k, i, this->_front)->getVal(); }
node* kth(node *&t,int k) { if (k<=t->chsize(0)) return kth(t->ch[0],k); else if(k>t->chsize(0)+1) return kth(t->ch[1],k-(t->chsize(0)+1)); else return t; }
node_type *kth(node_type *p, size_t k) const { if(k >= size(p)){ return nullptr; } if(k < size(p->children[0])){ return kth(p->children[0], k); } if(k == size(p->children[0])){ return p; } return kth(p->children[1], k - 1 - size(p->children[0])); }
void select(int left, int right) { splay(kth(right + 2)); splay(kth(left), root); }