标签:数据结构-划分树
1 10 1 1 4 2 3 5 6 7 8 9 0 1 3 2
2刚学了划分树,来做这一题,只知道划分树的原理,代码是自己写的,不是通用的写法。G++AC C++超内存。。。#include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; const int maxn = 100000; struct tree{ int l , r; vector<int> element , lft; }a[3*maxn]; vector<int> seq; int n , m; bool cmp(int a , int b){ return a<b;} void build(int l , int r , int k){ a[k].l = l; a[k].r = r; if(l != r){ int mid = (l+r)/2; int lson = 2*k , rson = 2*k+1; a[lson].element.clear(); a[rson].element.clear(); a[lson].element.push_back(0); a[rson].element.push_back(0); a[lson].lft.clear(); a[rson].lft.clear(); a[lson].lft.push_back(0); a[rson].lft.push_back(0); for(int i = 1; i < a[k].element.size(); i++){ if(a[k].element[i] <= seq[mid]){ a[lson].element.push_back(a[k].element[i]); a[k].lft.push_back(a[k].lft[i-1]+1); }else{ a[rson].element.push_back(a[k].element[i]); a[k].lft.push_back(a[k].lft[i-1]); } } build(l , mid , 2*k); build(mid+1 , r , 2*k+1); } } int query(int l , int r , int k , int kth){ if(a[k].l==a[k].r){ return a[k].element[1]; }else{ int tem = a[k].lft[r]-a[k].lft[l-1]; if(tem >= kth){ return query(1+a[k].lft[l-1] , 1+a[k].lft[l-1]+tem-1 , 2*k , kth); }else{ return query(1+(l-1-a[k].lft[l-1]) , (l-1-a[k].lft[l-1])+r-l+1-tem , 2*k+1 , kth-tem); } } } void initial(){ a[1].element.clear(); a[1].element.push_back(0); a[1].lft.clear(); a[1].lft.push_back(0); seq.clear(); } void readcase(){ scanf("%d%d" , &n , &m); seq.push_back(0); for(int i = 0; i < n; i++){ int num; scanf("%d" , &num); if(seq[0] >= num) seq[0] = num-1; seq.push_back(num); a[1].element.push_back(num); } } void computing(){ sort(seq.begin() , seq.end() , cmp); build(1 , n , 1); int s , t , k; while(m--){ scanf("%d%d%d" , &s , &t , &k); printf("%d\n" , query(s , t , 1 , k)); } } int main(){ int t; scanf("%d" , &t); while(t--){ initial(); readcase(); computing(); } return 0; }
hdu 2665 Kth number(划分树),布布扣,bubuko.com
标签:数据结构-划分树
原文地址:http://blog.csdn.net/u011836218/article/details/32177205