CDQ分治模板题

#include<cstdio>
#include<cctype>
#include<algorithm>
#include<cstring>
using namespace std;
inline int read()
{
int x = 0, flag = 1;
char c;
while(! isgraph(c = getchar()))
if(c == '-')
flag *= - 1;
while(isgraph(c))
x = x * 10 + c - '0', c = getchar();
return x * flag;
}
void println(int x)
{
if(x < 0)
putchar('-');
if(x == 0)
putchar('0');
int ans[10 + (1 << 4)], top = 0;
while(x)
ans[top ++] = x % 10, x /= 10;
for(; top; top --)
putchar(ans[top - 1] + '0');
putchar('\n');
}
const int MAXN = (int)1e5 + (1 << 5);
struct node
{
int x, y, z, ID;
node(int x = 0, int y = 0, int z = 0, int ID = 0): x(x), y(y), z(z), ID(ID){}
}a[MAXN], b[MAXN];
int operator <(node x, node y)
{
if(x.x != y.x)
return x.x < y.x;
if(x.y != y.y)
return x.y < y.y;
return x.z <= y.z;
}
int ans[MAXN];
int cmp(node x, node y)
{
if(x.y != y.y)
return x.y < y.y;
return x.ID < y.ID;
}
int tree[MAXN];
int MAXZ;
void modify(int u, int delta)
{
while(u <= MAXZ)
tree[u] += delta, u += (u & (-u));
}
int query(int u)
{
int ret = 0;
while(u)
ret += tree[u], u -= (u & (- u));
return ret;
}
void CDQ(int L, int R)
{
if(L == R)
return;
int mid = (L + R) >> 1;
int top = 0;
for(int i = L; i <= mid; i ++)
b[top ++] = node(0, a[i].y, a[i].z, 0);
for(int i = mid + 1; i <= R; i ++)
b[top ++] = node(0, a[i].y, a[i].z, a[i].ID);
sort(b, b + top, cmp);
for(int i = 0; i < top; i ++)
{
if(b[i].ID == 0)
modify(b[i].z, 1);
else
ans[b[i].ID] += query(b[i].z);
}
for(int i = 0; i < top; i ++)
if(b[i].ID == 0)
modify(b[i].z, - 1);
CDQ(L, mid);
CDQ(mid + 1, R);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("HDU5618.in", "r", stdin);
freopen("HDU5618.out", "w", stdout);
#endif
int T = read();
while(T --)
{
int n = read();
for(int i = 1; i <= n; i ++)
{
int x = read(), y = read(), z = read();
a[i] = node(x, y, z, i);
MAXZ = max(MAXZ, a[i].z);
}
sort(a + 1, a + n + 1);
memset(ans, 0, sizeof(ans));
memset(tree, 0, sizeof(tree));
int cnt = 0;
for(int i = n; i; i --)
{
if((a[i].x == a[i + 1].x)
&& (a[i].y == a[i + 1].y)
&& (a[i].z == a[i + 1].z))
cnt ++;
else
cnt = 0;
ans[a[i].ID] += cnt;
}
CDQ(1, n);
for(int i = 1; i <= n; i ++)
println(ans[i]);
}
}

HDU5618 Jam's problem again的更多相关文章

  1. HDU5618 Jam's problem again CDQ分治

    Jam's problem again CDQ分治 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5618 题意: \[ 有n 个元素,第 i 个元素有 ...

  2. cdq分治(hdu 5618 Jam's problem again[陌上花开]、CQOI 2011 动态逆序对、hdu 4742 Pinball Game、hdu 4456 Crowd、[HEOI2016/TJOI2016]序列、[NOI2007]货币兑换 )

    hdu 5618 Jam's problem again #include <bits/stdc++.h> #define MAXN 100010 using namespace std; ...

  3. HDU 5618 Jam's problem again(三维偏序,CDQ分治,树状数组,线段树)

    Jam's problem again Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  4. Google Code Jam 资格赛: Problem A. Magic Trick

    Note: To advance to the next rounds, you will need to score 25 points. Solving just this problem wil ...

  5. HDU 5618 Jam's problem again CDQ分治 BC ROUND 70

    题意:给你1e5个点(x,y,z),对于每一个点询问有多少个点(x1,y1,z1)满足x1<=x&&y1<=y&&z1<=z 分析:(官方题解奉上)很 ...

  6. HDU 5618 Jam's problem again

    题意: 三维坐标,对于1个点,找出有多少个点,3个坐标都比该点小! Sample Input 1 4 10 4 7 10 6 6 8 2 5 7 3 10   Sample Output 1 1 0 ...

  7. HDU 5618 Jam's problem again (cdq分治+BIT 或 树状数组套Treap)

    题意:给n个点,求每一个点的满足 x y z 都小于等于它的其他点的个数. 析:三维的,第一维直接排序就好按下标来,第二维按值来,第三维用数状数组维即可. 代码如下: cdq 分治: #pragma ...

  8. HDU 5618:Jam's problem again(CDQ分治+树状数组处理三维偏序)

    http://acm.hdu.edu.cn/showproblem.php?pid=5618 题意:…… 思路:和NEUOJ那题一样的.重新写了遍理解了一下,算作处理三维偏序的模板了. #includ ...

  9. Google Code Jam 2010 Round 1C Problem B. Load Testing

    https://code.google.com/codejam/contest/619102/dashboard#s=p1&a=1 Problem Now that you have won ...

随机推荐

  1. Java-basic-7-面向对象

    继承 在Java中,每个子类只能有一个父类,但可以继承多个接口. 子类继承父类,类定义的时候用extends. 继承接口,用implements. 重写 声明为final的方法不能被重写. 声明为st ...

  2. LeetCode(234) Palindrome Linked List

    题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) t ...

  3. HDU:4185-Oil Skimming

    Oil Skimming Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...

  4. 【转】MySQL innodb_autoinc_lock_mode 详解 ,并发插入时主键冲突的解决方案

    本文转载于 http://www.cnblogs.com/JiangLe/p/6362770.html innodb_autoinc_lock_mode这个参数控制着在向有auto_increment ...

  5. Centos启动时停止在登录界面但不显示登录信息(一直在转圈)

    进入单用户模式  执行 iscsiadm -m node -o delete,然后reboot

  6. HDU 5111 Alexandra and Two Trees 树链剖分 + 主席树

    题意: 给出两棵树,每棵树的节点都有一个权值. 同一棵树上的节点的权值互不相同,不同树上节点的权值可以相同. 要求回答如下询问: \(u_1 \, v_1 \, u_2 \, v_2\):询问第一棵树 ...

  7. c#利用反射实现对类中的常量进行取值和对应常量的注释

    C#利用反射实现对类中的常量进行取值和对应常量的注释 项目示例:https://gitee.com/dhclly/IceDog.GenerateErrorCode 因为业务需要,项目中有大量的错误码, ...

  8. 写iOS SDK注意事项

    转载http://www.devtang.com/blog/2015/01/31/write-sdk-tips/

  9. [转载]ExtJs4 笔记(2) ExtJs对js基本语法扩展支持

    作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/) 本篇主要介绍一下ExtJs对JS基本语法的扩展支持,包括动态加载.类的封装等. 一.动态引 ...

  10. 2018 “百度之星”程序设计大赛 - 初赛(B)

    degree  Accepts: 1581  Submissions: 3494  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 1310 ...