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. Persona5

    65536K   Persona5 is a famous video game. In the game, you are going to build relationship with your ...

  2. emacs写cnblog博客

    emacs的版本 org-mode版本   参考链接: 用Emacs管理博客园博客   用emacs org-mode写cnblogs博客 用emacs org-mode写博客 & 发布到博客 ...

  3. 2015多校训练第二场 hdu5305

    把这题想复杂了,一直在考虑怎么快速的判断将选的边和已选的边无冲突,后来经人提醒发现这根本没必要,反正数据也不大开两个数组爆搜就OK了,搜索之前要先排除两种没必要搜的情况,这很容易想到,爆搜的时候注意几 ...

  4. 数据挖掘算法——Apriori

    在上一篇数据挖掘入门算法整理中提到,Apriori算法是关联规则算法中使用最为广泛的算法,这次我们就来学习下该算法的基本知识. 一.算法概述 Apriori 算法是一种最有影响力的挖掘布尔关联规则的频 ...

  5. Python中的魔法函数__repr__和__str__的实质性区别

    str 和 repr 方法:是自定义类的字符串描述,这两种都是比较 Pythonic 的方式去控制对象转化为字符串的方式. 调用这两个方法,返回的都是字符串.但是这两个方法又有一些区别 ** 1 两种 ...

  6. bzoj3039 joyoi1939 玉蟾宫 悬线法

    悬线法 #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...

  7. 24、AES RSA加密处理记录

    一.加密过程解释 前提:发送方为A,接受方为B牢记:RSA为非对称加密,AES为对称加密.对称加密,属于传统的加密技术,加密和解密的秘钥都是相同的,AES的秘钥长度有128.192.256三种.非对称 ...

  8. SpringBoot 项目打包部署Resin遇到的问题

    1)javax/validation/ParameterNameProvider 找不到. 解决:A) resin/lib 目录下删掉原来的,validation-api 更新为 validation ...

  9. IDEA界面创建Scala的Maven项目

    1. 创建Maven工程,勾选右侧的Create from archetype选项,然后选中下方的scala-archetype-simple选项,如图所示:2. 填写相应的GroupId.Artif ...

  10. python - work5 - 类与对象

    # -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: work_20181119.py@ide: PyCharm Communit ...