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 ...
随机推荐
- 使用Intent实现Activity的显式跳转
[正文] 这里以按钮实现活动跳转为例,为实现这个功能,我们需要三个步骤: 1.点击按钮才发生页面跳转,因此,第一步我们先要找到要点击的按钮 如何拿到按钮对象呢?通过资源id,前面我们提到过,在R.id ...
- C#获取文件夹下指定格式的所有文件
C#获取文件夹下指定格式的所有文件的方法,虽然很简单,但还是分享一下吧,用到时可以稍加修改和优化就可以使用. 获取指定目录下所有文件 //最要使用 System.IO.Directory.GetFil ...
- noip 2005 等价表达式
/* 开始想的是 维护a的每个指数的系数 然而不好办 然而还有^10^10^10这种数据 特殊值带入吧 多搞几个素数 接下来就是玄学的事了 给a赋值之后 就是简单地表达式求值 虽然思路简单 但是字符串 ...
- java 线程池用法
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, ...
- 秒味课堂Angular js笔记------指令
1.属性指令 angularjs样式相关指令: ng-class ng-style ng-href ng-src ng-attr-(suffix) ng-bind ng-cloak 没解析完之前标签 ...
- memcache和activemq使用连接,然后close
memcache和activemq使用连接,然后close
- C#获取磁盘列表与信息
方法1:使用Environment //获取当前计算机逻辑磁盘名称列表 String[] drives = Environment.GetLogicalDrives(); Console.WriteL ...
- ImageView的学习
学习安卓时我还是习惯看懂手册,虽然是英文但是可以获得的东西必然也是更多的,否则自己只能停留在拾人牙缝的水平,虽然我是初学,但是还是分享一些自己的学习过程及方法. 从手册中我们看以知道,ImageVie ...
- linux FILE 类型.
stdio.h 头文件中,有以下内容(用内部行号解释): /* The opaque type of streams. This is the definition used elsewhere. * ...
- 启用DHCP中继代理,实现跨子网服务 - Win 2003 Server
伴随着局域网规模的逐步扩大,一个网络常常会被划分成多个不同的子网,以便根据不同子网的工作要求来实现个性化的管理要求.考虑到规模较大的局域网一般会使用DHCP服务器来为各个工作站分配IP地址,不过一旦局 ...