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 ...
随机推荐
- 告别LVS:使用keepalived+nginx实现负载均衡代理多个https
需求1:CDN小节点使用尽可能少的资源实现高可用和负载均衡需求2:需要支持10多个HTTPS站点的反向代理后端环境:nginx在前端做url_hash,后端缓存服务器使用squid和lighttpd分 ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- Windows下用C语言获取进程cpu使用率,内存使用,IO情况
#ifndef PROCESS_STAT_H #define PROCESS_STAT_H #ifdef __cplusplus extern “C” { #endif typedef l ...
- cocos2d-x CCAction:动作(转)
透明度变化的功能挺不错. 瞬时动作 瞬时动作不需要时间,立即完成 [cpp] //放置,=setPosition() pRole->runAction(CCPlace::create ...
- Linux驱动开发cdev驱动分层设计
#ifndef MYDEV_H #define MYDEV_H #define DYNAMIC_MINOR 256 struct mydev{ const char *name; const stru ...
- [转] Linux写时拷贝技术(copy-on-write)
PS:http://blog.csdn.net/zxh821112/article/details/8969541 进程间是相互独立的,其实完全可以看成A.B两个进程各自有一份单独的liba.so和l ...
- 《UNIX网络编程》之select IO
select 函数的原理 select 管理者 用select来管理多个IO 一旦其中的一个或者多个IO检测到我们所感兴趣的事件, select 函数返回,返回值为检测到的事件个数 然后,遍历事件,进 ...
- GridView下DropDownList 的选择方法onselectedindexchanged 实现方法
在GridView下面绑定好了下拉框,我们常常会遇到一个问题, 选择方法怎么实现呢,用js总是难的去算是在GridView的第几行第几个元素,因为服务器的id和客户端的id经常变化让js根本无从找起, ...
- Js参数RSA加密传输,jsencrypt.js的使用
注意几点: 1.参数传递的+号处理,在传输时会把+变成空格,不处理后端就报错了. 1.前段代码 <!DOCTYPE html> <html> <head> < ...
- pydev出现Project interpreter not specified(eclipse+pydev)
出现上述错误的原因是因为没有为Pydev指定python.exe位置 修改步骤依次是 1.从Eclipse的菜单 Window --> Preferences 打开首选项配置: 2. ...