【HDOJ】4251 The Famous ICPC Team Again
划分树模板题目,主席树也可解。
划分树。
/* 4251 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = 1e5+;
int order[maxn];
int toLeft[][maxn];
int val[][maxn];
int n; void Build(int l, int r, int dep) {
if (l == r) {
toLeft[dep][l] = toLeft[dep][l-] + ;
return ;
} int mid = (l + r) >> ;
int same = mid - l + ; rep(i, l, r+) {
if (val[dep][i] < order[mid])
--same;
} int lpos = l, rpos = mid + ; rep(i, l, r+) {
if (val[dep][i] < order[mid]) {
val[dep+][lpos++] = val[dep][i];
} else if (val[dep][i]==order[mid] && same>) {
val[dep+][lpos++] = val[dep][i];
--same;
} else {
val[dep+][rpos++] = val[dep][i];
}
toLeft[dep][i] = toLeft[dep][l-] + lpos - l;
} Build(l, mid, dep+);
Build(mid+, r, dep+);
} int Query(int l, int r, int k, int L, int R, int dep) {
if (l == r) {
return val[dep][l];
} int mid = (L + R) >> ;
int tmp = toLeft[dep][r] - toLeft[dep][l-]; if (tmp >= k) {
int ll = L + toLeft[dep][l-] - toLeft[dep][L-];
int rr = ll + tmp - ; return Query(ll, rr, k, L, mid, dep+);
} else {
k -= tmp;
int ll = mid+ + l-L - (toLeft[dep][l-] - toLeft[dep][L-]);
int rr = ll + (r-l+-tmp) - ; return Query(ll, rr, k, mid+, R, dep+);
}
} void solve() {
sort(order+, order++n);
Build(, n, ); int q;
int l, r, kth;
int ans; scanf("%d", &q);
while (q--) {
scanf("%d %d", &l, &r);
kth = ((l+r)>>) - l + ;
ans = Query(l, r, kth, , n, );
printf("%d\n", ans);
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t = ; while (scanf("%d", &n)!=EOF) {
rep(i, , n+) {
scanf("%d", &val[][i]);
order[i] = val[][i];
}
printf("Case %d:\n", ++t);
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
主席树。
/* 4251 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
// #define lson l, mid, rt<<1
// #define rson mid+1, r, rt<<1|1 const int maxn = 1e5+;
const int maxm = maxn * ;
int a[maxn], b[maxn];
int T[maxn];
int lson[maxm], rson[maxm], c[maxm];
int n, m, tot; void init() {
m = tot = ;
} int Build(int l, int r) {
int rt = tot++; c[rt] = ;
if (l == r)
return rt; int mid = (l + r) >> ; lson[rt] = Build(l, mid);
rson[rt] = Build(mid+, r);
return rt;
} int Insert(int rt, int p, int delta) {
int nrt = tot++, ret = nrt;
int l = , r = m - , mid; c[nrt] = c[rt] + delta;
while (l < r) {
mid = (l + r) >> ;
if (p <= mid) {
lson[nrt] = tot++;
rson[nrt] = rson[rt];
nrt = lson[nrt];
rt = lson[rt];
r = mid;
} else {
lson[nrt] = lson[rt];
rson[nrt] = tot++;
nrt = rson[nrt];
rt = rson[rt];
l = mid + ;
}
c[nrt] = c[rt] + delta;
} return ret;
} int Query(int lrt, int rrt, int k, int l, int r) {
if (l == r)
return l; int mid = (l + r) >> ;
int tmp = c[lson[rrt]] - c[lson[lrt]]; if (tmp >= k) {
return Query(lson[lrt], lson[rrt], k, l, mid);
} else {
k -= tmp;
return Query(rson[lrt], rson[rrt], k, mid+, r);
}
} void solve() {
init();
sort(b, b+n);
m = unique(b, b+n) - b;
T[] = Build(, m-);
rep(i, , n) {
int id = lower_bound(b, b+m, a[i]) - b;
T[i+] = Insert(T[i], id, );
} int q;
int l, r, kth;
int ans; scanf("%d", &q);
while (q--) {
scanf("%d %d", &l, &r);
kth = ((l+r)>>)-l+;
int id = Query(T[l-], T[r], kth, , m-);
ans = b[id];
printf("%d\n", ans);
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t = ; while (scanf("%d", &n)!=EOF) {
rep(i, , n) {
scanf("%d", &a[i]);
b[i] = a[i];
}
printf("Case %d:\n", ++t);
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【HDOJ】4251 The Famous ICPC Team Again的更多相关文章
- HDOJ 4251 The Famous ICPC Team Again
划分树水题..... The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 3276 ...
- HDU 4251 The Famous ICPC Team Again(划分树)
The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU 4251 The Famous ICPC Team Again 主席树
The Famous ICPC Team Again Problem Description When Mr. B, Mr. G and Mr. M were preparing for the ...
- hdu 4251 The Famous ICPC Team Again划分树入门题
The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 【HDOJ】1310 Team Rankings
STL的应用,基本就是模拟题. /* 1410 */ #include <iostream> #include <string> #include <algorithm& ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【转】lonekight@xmu·ACM/ICPC 回忆录
转自:http://hi.baidu.com/ordeder/item/2a342a7fe7cb9e336dc37c89 2009年09月06日 星期日 21:55 初识ACM最早听说ACM/ICPC ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
随机推荐
- C# Json数据反序列化为Dictionary并根据关键字获取指定值
Json数据: { "dataSet": { "header": { "returnCode": "0", " ...
- C#调用IOS推送
C#调用IOS推送 使用的是 PushSharp 开源库 源码代码如下 点我
- AngularJS(6)-选择框Select
1.在 AngularJS 中我们可以使用 ng-option 指令来创建一个下拉列表,列表项通过对象和数组循环输出 <!DOCTYPE html> <html lang=" ...
- LM算法
最小二乘法的概念 最小二乘法的目标:求误差的最小平方和,对应有两种:线性和非线性. 线性最小二乘的解是closed-form即x=(A^T A)^{-1}A^Tb, 而非线性最小二乘没有closed- ...
- mac 下 sphinx + mysql + php 实现全文搜索(xampp)(3)sphinx 的配置项解析
source:数据的来源,数据是从什么地方来的. index:索引,当有数据源之后,从数据源处构建索引.索引实际上就是相当于一个字典检索.有了整本字典内容以后,才会有字典检索. searchd:提供搜 ...
- mysql 的 存储结构(储存引擎)
1 MyISAM:这种引擎是mysql最早提供的.这种引擎又可以分为静态MyISAM.动态MyISAM 和压缩MyISAM三种: 静态MyISAM:如果数据表中的各数据列的长度都是预先固定好的, ...
- (转)《深入理解java虚拟机》学习笔记7——Java虚拟机类生命周期
C/C++等纯编译语言从源码到最终执行一般要经历:编译.连接和运行三个阶段,连接是在编译期间完成,而java在编译期间仅仅是将源码编译为Java虚拟机可以识别的字节码Class类文件,Java虚拟机对 ...
- Kinetic使用注意点--container
<virtual> new Container(config) 参数: config:包含所有配置项的对象. { x: "横坐标", y: "纵坐标" ...
- IOS平台汉字转拼音方案
iOS/Mac OS X 汉字转拼音 网络流行的汉字转拼音方案是带一个拼音码表,速度快.其实Core Foundation也提供了一种方案,而且还带声调! NSMutableString *ms = ...
- 统一iOS客户端和服务器端认证
最近公司的同事业余时间搞了一个内部的类about.me(https://about.me/)的网站Ocelots,想来是一个很洋气的注意,以后跟客户介绍公司的时候,直接登录该网站,谈到谁的时候,就打开 ...