HDU5140---Hun Gui Wei Company (主席树)
主席树太强大了,,如果仅仅用来求第k大就太屈才了。。貌似和HDU4605差不多,那个是在图上根据点的顺序建立主席树,这个是根据年龄大小 或者等级高低建立主席树。
题意 大致就是一个二维区间的求和,但是数量级很大,显然不能直接求。
一个想法是可以二维线段树,但是这样显然会MLE。
另外一个还是主席树,以age或者level顺序建立主席树,我是以age建立的。
然后对应的查找就是 查找 tree[LA-1] 、tree[HA]之间的线段树 等级在LL HL之间的salary的和。
题目强制要求在线算,只需要把 k更新后询问的LL HL LA HA 离散化就可以了。
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
const int maxn = 1e5+;
int tot,tree[maxn],c[maxn*],lson[maxn*],rson[maxn*];
ll sum[maxn*];
int build (int l, int r)
{
int root = tot++;
c[root] = sum[root] = ;
if (l != r)
{
int mid = (l + r) >> ;
lson[root] = build(l,mid);
rson[root] = build(mid+,r);
}
return root;
}
int MAX;
int update (int root,int pos,int val,ll sa)
{
int newroot = tot++;
int tmp = newroot;
int l = , r = MAX;
c[newroot] = c[root] + val;
sum[newroot] = sum[root] + sa;
while (l < r)
{
int mid = (l + r) >> ;
if (pos <= mid)
{
r = mid;
rson[newroot] = rson[root];
root = lson[root];
lson[newroot] = tot++;
newroot = lson[newroot];
}
else
{
l = mid + ;
lson[newroot] = lson[root];
root = rson[root];
rson[newroot] = tot++;
newroot = rson[newroot];
}
c[newroot] = c[root] + val;
sum[newroot] = sum[root] + sa;
}
return tmp;
}
ll query(int root1,int root2,int l,int r,int ua,int ub)
{
if (ua > ub)
return ;
if (ua <= l && ub >= r)
{
return sum[root2] - sum[root1];
}
int mid = (l + r) >> ;
ll t1 = , t2 = ;
if (ua <= mid)
t1 = query(lson[root1],lson[root2],l,mid,ua,ub);
if (ub > mid)
t2 = query(rson[root1],rson[root2],mid+,r,ua,ub);
return t1 + t2;
}
int lev[maxn],idx1,idx2,age[maxn]; struct worker
{
int s,l,a;
bool operator < (const worker &rhs)const
{
return a < rhs.a;
}
} per[maxn];
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int n;
while (~scanf ("%d",&n))
{
tot = ;
memset(sum,,sizeof(sum));
MAX = n;
int max_age = ,min_age = inf;
for (int i = ; i <= n; i++)
{
scanf ("%d%d%d",&per[i].s, &per[i].l, &per[i].a);
age[i-] = per[i].a;
lev[i-] = per[i].l;
max_age = max(max_age,age[i-]);
min_age = min(min_age,age[i-]);
}
sort(per+, per++n);
sort(age, age+n);
sort(lev, lev+n);
idx1 = n;
idx2 = unique(lev,lev+n) - lev; tree[] = build(,n);
for (int i = ; i <= n; i++)
{
int tmp = lower_bound(lev,lev+idx2,per[i].l) - lev + ;
tree[i] = update(tree[i-], tmp, , (ll)per[i].s);
}
int Q;
ll k = ;
scanf ("%d",&Q);
for (int i = ; i < Q; i++)
{
ll LL,HL,LA,HA;
scanf ("%I64d%I64d%I64d%I64d",&LL,&HL,&LA,&HA);
LL += k, HL -= k;
LA += k, HA -= k;
if (LL > HL)
swap(LL, HL);
if (LA > HA)
swap(LA, HA);
LA = max(LA, (ll));
HA = min(HA, (ll));
int idx_la = lower_bound(age,age+idx1,LA) - age + ;
int idx_ha = lower_bound(age,age+idx1,HA) - age + ;
if (age[idx_ha-] == HA)
{
while (age[idx_ha-] == HA && idx_ha <= n)
idx_ha ++;
idx_ha--;
}
else
idx_ha--;
LL = max(LL,(ll));
HL = min(HL,(ll));
int i1 = lower_bound(lev,lev+idx2,LL) - lev + ;
int i2 = lower_bound(lev,lev+idx2,HL) - lev + ;
if (lev[i2-] > HL)
i2--;
k = query(tree[idx_la-],tree[idx_ha],,MAX,i1,i2);
printf("%I64d\n",k);
}
}
return ;
} /*
5
1 2 2
2 3 3
3 4 4
4 5 6
6 5 8
1
2 3 2 8 3
5 6 4
2 8 6
4 8 6
1
6 8 4 6
*/
HDU5140---Hun Gui Wei Company (主席树)的更多相关文章
- POJ2104 K-th Number(主席树)
题目 Source http://poj.org/problem?id=2104 Description You are working for Macrohard company in data s ...
- POJ2104 K-th Number[主席树]【学习笔记】
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 51440 Accepted: 17594 Ca ...
- POJ 2104 K-th Number(主席树——附讲解)
Description You are working for Macrohard company in data structures department. After failing your ...
- 【BZOJ】1146: [CTSC2008]网络管理Network(树链剖分+线段树套平衡树+二分 / dfs序+树状数组+主席树)
http://www.lydsy.com/JudgeOnline/problem.php?id=1146 第一种做法(时间太感人): 第二种做法(rank5,好开心) ================ ...
- 【POJ】2104 K-th Number(区间k大+主席树)
http://poj.org/problem?id=2104 裸题不说.主席树水过. #include <cstdio> #include <iostream> #includ ...
- POJ 2104&HDU 2665 Kth number(主席树入门+离散化)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 50247 Accepted: 17101 Ca ...
- ZOJ 2112 Dynamic Rankings(主席树の动态kth)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2112 The Company Dynamic Rankings ...
- [poj2104] K-th Number (主席树)
主席树 Description You are working for Macrohard company in data structures department. After failing y ...
- poj 2104 K-th Number(主席树)
Description You are working for Macrohard company in data structures department. After failing your ...
随机推荐
- poj2096 Collecting Bugs(概率dp)
Collecting Bugs Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 1792 Accepted: 832 C ...
- Cobar是提供关系型数据库(MySQL)分布式服务的中间件
简介 Cobar是提供关系型数据库(MySQL)分布式服务的中间件,它可以让传统的数据库得到良好的线性扩展,并看上去还是一个数据库,对应用保持透明. 产品在阿里巴巴稳定运行3年以上. 接管了3000+ ...
- JStorm 是一个分布式实时计算引擎
alibaba/jstorm JStorm 是一个分布式实时计算引擎. JStorm 是一个类似Hadoop MapReduce的系统, 用户按照指定的接口实现一个任务,然后将这个任务递交给JStor ...
- cache 的设计与实现--转载
本文整理自一下两篇博客:http://my.oschina.net/ScottYang/blog/298727http://my.oschina.net/u/866190/blog/188712 Ca ...
- Configuring the JA-SIG CAS Client --官方
1. for Java using Spring Configuration of the CAS Client for Java via Spring IoC will depend heavily ...
- MaterialDialog的用法:
MaterialDialog的用法:/** * * @author smiling * @date 2016/10 */ Github:https://github.com/drakeet/Mater ...
- HDU 5139 Formula 卡内存
题目就是求这个 n达到10^7,测试数据组数为10^5 为了防止TLE,一开始把每个n对应的值先求出来,但发现竟然开不了10^7的数组(MLE),然后就意识到这是第一道卡内存的题目... 只能离线做, ...
- Shell基本的命令
ubuntu 中文乱码 如果使用的是 PuTTY,可以通过修改 font, character set 设置来解决. Window -> Appearance -> Font settin ...
- 通过URL推送POST数据
由于到了一家新公司重新开始接触MVC和其他的一些东西.所以的重新拾起许多东西. 前一段时间让我写一个和第三方公司推送对接的方法.通过对方提供的URL把数据post推送出去. 我把url到了web.co ...
- OD: SEHOP
SEHOP,Structed Exception Handling Overwrite Protection,一种比 SafeSEH 更严厉的保护机制.Windows Vista SP1 开始支持 S ...