题目链接

给n个操作, 第一种是在x, y, z这个点+1. 第二种询问(x1, y1, z1). (x2, y2, z2)之间的总值。

用一次cdq分治可以将三维变两维, 两次的话就变成一维了, 然后最后一维用树状数组维护。 对于每个询问, 相当于将它拆成8个点。

注意第二次cdq分治的时候l可能小于r。 所以这里的return条件是l <= r而不是l == r。 找了好久...

#include <bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define mem(a) memset(a, 0, sizeof(a))
vector <int> b;
const int maxn = 5e4+;
struct node
{
int x1, y1, z1, x2, y2, z2;
int sign, id;
node(){}
node(int x, int y, int z, int sign, int id):x1(x), y1(y), z1(z), sign(sign), id(id){}
}a[maxn], s[maxn*], ss[maxn*], c[maxn*];
bool cmpx(node a, node b)
{
if(a.x1 == b.x1)
return a.id < b.id;
return a.x1 < b.x1;
}
bool cmpy(node a, node b)
{
if(a.y1 == b.y1)
return a.id < b.id;
return a.y1 < b.y1;
}
int len, sum[maxn*], ans[maxn];
int lowbit(int x) {return x&(-x);}
void update(int x, int val) {
while(x <= len) {
sum[x] += val;
x += lowbit(x);
}
}
int query(int x, int ret = )
{
while(x) {
ret += sum[x];
x -= lowbit(x);
}
return ret;
}
void cdqy(int l, int r)
{
if(l >= r)
return ;
int m = l+r>>, cnt = ;
cdqy(l, m);
cdqy(m+, r);
for(int i = l; i <= m; i++) {
if(s[i].sign == ) {
ss[cnt++] = s[i];
}
}
for(int i = m+; i <= r; i++) {
if(s[i].sign != ) {
ss[cnt++] = s[i];
}
}
sort(ss, ss+cnt, cmpy);
for(int i = ; i < cnt; i++) {
if(ss[i].sign == ) {
update(ss[i].z1, );
} else if(ss[i].sign == ) {
ans[ss[i].id] += query(ss[i].z1);
} else {
ans[ss[i].id] -= query(ss[i].z1);
}
}
for(int i = ; i < cnt; i++) {
if(ss[i].sign == ) {
update(ss[i].z1, -);
}
}
}
void cdqx(int l, int r)
{
if(l == r)
return ;
int m = l+r>>, top = ;
cdqx(l, m);
cdqx(m+, r);
for(int i = l; i <= m; i++) {
if(c[i].sign == ) {
s[++top] = c[i];
}
}
for(int i = m+; i <= r; i++) {
if(c[i].sign != ) {
s[++top] = c[i];
}
}
sort(s+, s++top, cmpx);
cdqy(, top);
}
void solve(int n, int num = )
{
sort(b.begin(), b.end());
b.erase(unique(b.begin(), b.end()), b.end());
len = b.size();
for(int i = ; i <= n; i++) {
if(a[i].sign == ) {
c[++num] = a[i];
} else {
c[++num] = node(a[i].x2, a[i].y2, a[i].z2, , a[i].id);
c[++num] = node(a[i].x1-, a[i].y1-, a[i].z1-, , a[i].id);
c[++num] = node(a[i].x2, a[i].y2, a[i].z1-, , a[i].id);
c[++num] = node(a[i].x2, a[i].y1-, a[i].z2, , a[i].id);
c[++num] = node(a[i].x1-, a[i].y2, a[i].z2, , a[i].id);
c[++num] = node(a[i].x1-, a[i].y1-, a[i].z2, , a[i].id);
c[++num] = node(a[i].x1-, a[i].y2, a[i].z1-, , a[i].id);
c[++num] = node(a[i].x2, a[i].y1-, a[i].z1-, , a[i].id);
}
}
for(int i = ; i <= num; i++) {
c[i].z1 = lower_bound(b.begin(), b.end(), c[i].z1)-b.begin()+;
}
cdqx(, num);
}
int main()
{
int t, n;
cin>>t;
while(t--) {
cin>>n;
mem(sum);
mem(ans);
for(int i = ; i <= n; i++) {
scanf("%d%d%d%d", &a[i].sign, &a[i].x1, &a[i].y1, &a[i].z1);
if(a[i].sign == ) {
scanf("%d%d%d", &a[i].x2, &a[i].y2, &a[i].z2);
b.pb(a[i].z2);
b.pb(a[i].z1-);
} else {
b.pb(a[i].z1);
}
a[i].id = i;
}
solve(n);
for(int i = ; i <= n; i++) {
if(a[i].sign == ) {
printf("%d\n", ans[i]);
}
}
}
return ;
}

hdu 5126 stars cdq分治套cdq分治+树状数组的更多相关文章

  1. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. HDU 3887:Counting Offspring(DFS序+树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...

  3. HDU 5654 xiaoxin and his watermelon candy 离线树状数组 区间不同数的个数

    xiaoxin and his watermelon candy 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5654 Description Du ...

  4. HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...

  5. HDU 2227 Find the nondecreasing subsequences dp思想 + 树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=2227 用dp[i]表示以第i个数为结尾的nondecreasing串有多少个. 那么对于每个a[i] 要去找 & ...

  6. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  7. hdu 4715 Difference Between Primes(素数筛选+树状数组哈希剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=4715 [code]: #include <iostream> #include <cstdio ...

  8. HDU 4605 Magic Ball Game (dfs+离线树状数组)

    题意:给你一颗有根树,它的孩子要么只有两个,要么没有,且每个点都有一个权值w. 接着给你一个权值为x的球,它从更节点开始向下掉,有三种情况 x=w[now]:停在此点 x<w[now]:当有孩子 ...

  9. HDU 4267 A Simple Problem with Integers(树状数组区间更新)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  10. Holedox Eating HDU - 4302 2012多校C 二分查找+树状数组/线段树优化

    题意 一个长度$n<=1e5$的数轴,$m<=1e5$个操作 有两种一些操作 $0$  $x$ 在$x$放一个食物 $1$ 一个虫子去吃最近的食物,如果有两个食物一样近,不转变方向的去吃 ...

随机推荐

  1. Building Android Apps 30条建议

    Building Android Apps — 30 things that experience made me learn the hard way There are two kinds of ...

  2. 原生化:AnDevCon 2014 McVeigh 的主题演讲

    作者:Jeff McVeigh(Intel) 基于(至少部分)NDK的原生安卓应用程序占现在前1000 强的 60% 以上.该增长的原因很简单:开发商需要为用户提供超卓的体验(包括灵敏的反应.与丰富的 ...

  3. python编码格式

    python编码总结: 1).首先python有两种格式的字符串,str和unicode,其中unicode相当于字节码那样,可以跨平台使用. str转化为unicode可以通过unicode(),u ...

  4. 一个Java项目的学习

    1. java命令行的启动 首先是gradle build 其次是:java -Dabc.appid=1234 -classpath "a.jar:b.jar"  com.ctri ...

  5. linux之SQL语句简明教程---GROUP BY

    我们现在回到函数上.记得我们用 SUM 这个指令来算出所有的 Sales (营业额)吧!如果我们的需求变成是要算出每一间店 (Store_Name) 的营业额 (Sales),那怎么办呢?在这个情况下 ...

  6. 【FSFA 读书笔记】Ch 2 Computer Foundatinons(1)

    Data Organization 1. 进制转换. 按照正常的书写顺序写一个数字(无论多少进制),其中最左边的列称为“最高有效符号”,最右边的列称为“最低有效符号”. (The right-most ...

  7. poj 1050 To the Max_dp求最大子矩阵和

    题意:求最大子矩阵和 利用dp[i]每次向下更新,构成竖起的单条矩阵,再按不小于零就加起来来更新,构成更大的矩阵 #include <iostream> #include<cstdi ...

  8. linux shell命令行下操作mysql 删除mysql指定数据库下的所有表--亲测成功百分百测试通过--绝对可靠

    1,在shell提示符下查看mysql指定数据库下的表等数据

  9. linux内核交互,设备驱动控制管理接口

    1,ioctl preface--starting point ,format,mount volume,in addition to the above file system -- allows ...

  10. Hive 4、Hive 的安装配置(远端MyMql模式)

    1.remote一体 这种存储方式需要在远端服务器运行一个mysql服务器,并且需要在Hive服务器启动meta服务.这里用mysql的测试服务器,ip位192.168.1.214,新建hive_re ...