题意:给n个点,求每一个点的满足 x y z 都小于等于它的其他点的个数。

析:三维的,第一维直接排序就好按下标来,第二维按值来,第三维用数状数组维即可。

代码如下:

cdq 分治:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int maxm = 1e6 + 5;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} struct Node{
int x, y, z, id, num;
bool operator < (const Node &p) const{
return x != p.x ? x < p.x : (y != p.y ? y < p.y : z < p.z);
} bool operator == (const Node &p) const{
return x == p.x && y == p.y && z == p.z;
}
};
Node a[maxn];
int ans[maxn];
int sum[maxn]; inline int lowbit(int x){ return -x&x; } void add(int x, int c){
while(x < maxn){
sum[x] += c;
x += lowbit(x);
}
} int query(int x){
int ans = 0;
while(x){
ans += sum[x];
x -= lowbit(x);
}
return ans;
} inline bool cmp(const Node &lhs, const Node &rhs){
return lhs.y < rhs.y || lhs.y == rhs.y && lhs.z < rhs.z;
} void dfs(int l, int r){
if(l == r){ ans[a[l].id] += a[l].num-1; return ; }
int m = l + r >> 1;
dfs(l, m); dfs(m+1, r);
sort(a+l, a+m+1, cmp);
sort(a+m+1, a+r+1, cmp);
int j = l;
for(int i = m+1; i <= r; ++i){
while(j <= m && a[j].y <= a[i].y) add(a[j].z, a[j].num), ++j;
ans[a[i].id] += query(a[i].z);
}
for(int i = l; i < j; ++i) add(a[i].z, -a[i].num);
} int id[maxn]; int main(){
int T; cin >> T;
while(T--){
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d %d %d", &a[i].x, &a[i].y, &a[i].z);
a[i].id = i;
} sort(a, a + n); ms(ans, 0);
int cnt = 0;
for(int i = 0; i < n; ++i, ++cnt){
a[cnt] = a[i]; a[cnt].num = 1;
id[a[i].id] = cnt;
for(int j = i+1; j < n && a[i] == a[j]; i = j, id[a[j].id] = cnt, ++j)
++a[cnt].num;
a[cnt].id = cnt;
}
dfs(0, cnt-1);
for(int i = 0; i < n; ++i) printf("%d\n", ans[id[i]]);
}
return 0;
}

 树套树:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int maxm = 1e6 + 5;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} struct Node{
Node *ch[2];
int r;
int v, num;
int s;
Node(int v, int n) : v(v), num(n) { ch[0] = ch[1] = 0; r = rand(); s = num; }
bool operator < (const Node &rhs) const{
return r < rhs.r;
}
int cmp(int x) const{
if(x == v) return -1;
return x < v ? 0 : 1;
}
void maintain(){
s = num;
if(ch[0]) s += ch[0]->s;
if(ch[1]) s += ch[1]->s;
}
}; void rotate(Node* &o, int d){
Node *k = o->ch[d^1]; o->ch[d^1] = k->ch[d]; k->ch[d] = o;
o->maintain(); k->maintain(); o = k;
} void insert(Node* &o, int x, int num){
if(o == 0) o = new Node(x, num);
else {
int d = (x < o->v ? 0 : 1);
insert(o->ch[d], x, num);
if(o->ch[d]->r > o->r) rotate(o, d^1);
}
o->maintain();
} void remove(Node* &o){
if(o == 0) return ;
remove(o->ch[0]);
remove(o->ch[1]);
delete o; o = 0;
} int getrank(Node *o, int num){
if(o == 0) return 0;
if(num == o->v){
if(o->ch[0]) return o->ch[0]->s + o->num + getrank(o->ch[1], num);
return o->num + getrank(o->ch[1], num);
}
if(num < o->v) return getrank(o->ch[0], num);
if(o->ch[0]) return o->ch[0]->s + o->num + getrank(o->ch[1], num);
return o->num + getrank(o->ch[1], num);
} Node* root[maxn]; struct node{
int x, y, z, id, num;
bool operator < (const node &p) const{
return x != p.x ? x < p.x : (y != p.y ? y < p.y : z < p.z);
} bool operator == (const node &p) const{
return x == p.x && y == p.y && z == p.z;
}
};
node a[maxn];
int id[maxn], ans[maxn];
inline int lowbit(int x){ return -x&x; } void add(int x, int i){
while(x < maxn){
insert(root[x], a[i].z, a[i].num);
x += lowbit(x);
}
} int query(int x, int c){
int ans = 0;
while(x){
ans += getrank(root[x], c);
x -= lowbit(x);
}
return ans;
} int main(){
int T; cin >> T;
while(T--){
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d %d %d", &a[i].x, &a[i].y, &a[i].z);
a[i].id = i;
} sort(a, a + n);
int cnt = 0;
for(int i = 0; i < n; ++i, ++cnt){
a[cnt] = a[i]; a[cnt].num = 1;
id[a[i].id] = cnt;
for(int j = i+1; j < n && a[i] == a[j]; ++j){
i = j; id[a[j].id] = cnt;
++a[cnt].num;
}
ans[cnt] = query(a[cnt].y, a[cnt].z) + a[cnt].num - 1;
add(a[cnt].y, cnt);
}
for(int i = 0; i < maxn; ++i) remove(root[i]);
for(int i = 0; i < n; ++i) printf("%d\n", ans[id[i]]);
}
return 0;
}

  

HDU 5618 Jam's problem again (cdq分治+BIT 或 树状数组套Treap)的更多相关文章

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

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

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

  3. 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; ...

  4. HDU5618 Jam's problem again CDQ分治

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

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

  6. [APIO2019] [LOJ 3146] 路灯 (cdq分治或树状数组套线段树)

    [APIO2019] [LOJ 3146] 路灯 (cdq分治或树状数组套线段树) 题面 略 分析 首先把一组询问(x,y)看成二维平面上的一个点,我们想办法用数据结构维护这个二维平面(注意根据题意这 ...

  7. HDU 4267 A Simple Problem with Integers 多个树状数组

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

  8. HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...

  9. BZOJ3262/洛谷P3810 陌上花开 分治 三维偏序 树状数组

    原文链接http://www.cnblogs.com/zhouzhendong/p/8672131.html 题目传送门 - BZOJ3262 题目传送门 - 洛谷P3810 题意 有$n$个元素,第 ...

随机推荐

  1. websocket使用

    兼容性介绍 : https://caniuse.com/#search=websockets var websocket = null; //判断当前浏览器是否支持WebSocket if ('Web ...

  2. MVC控制器详解

    原文地址:http://www.cnblogs.com/SeeYouBug/p/6441934.html#3628606 目录 一.理解控制器 1.1.什么是控制器 1.2.控制器的作用 1.3.创建 ...

  3. js中Math.round、parseInt、Math.floor和Math.ceil小数取整小结

    以前经常在代码中看到Math.round.parseInt.Math.floor和Math.ceil这四个函数,虽然知道结果都可以返回一个整数,但是对他们四者的区别还是不太清楚,今天就做一个小结. 一 ...

  4. docker上传镜像

    已经存在镜像 docker tag conductor:ui docker.io/mhcvs2/mhc docker push docker.io/mhcvs2/mhc

  5. 大型运输行业实战_day09_2_站间互售实现

    1.添加站间互售入口 对应的html代码 <button onclick="otherStation()">站间互售</button> 对应的js发送函数 ...

  6. dubbo 多协议和多注册中心

    一.配置dubbo多协议模式 1.默认协议 Dubbo缺省协议采用单一长连接和NIO异步通讯,适合于小数据量大并发的服务调用,以及服务消费者机器数远大于服务提供者机器数的情况.Dubbo缺省协议不适合 ...

  7. Interface, 接口的实现初解

    百度是这么说的: Java接口是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为(功能). 两种含 ...

  8. hdoj1074--Doing Homework (DP 状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 思路: 看着数据很小,15,但是完成的顺序有15!情况,这么大的数据是无法实现的.上网查才知道要 ...

  9. 使用生活实例理解Asp.net运行时

    学习编程语言,掌握面向对象的编程思想尤为重要,一旦理解了面向对象的这种概念,那么好些地方拿到生活中去理解,就容易的多了.书本上的枯燥干涩的语言,对于好多人来说,即难懂,更难长时间牢牢记得.但是编程语言 ...

  10. IBM MQ 学习

    import java.io.IOException; import java.util.HashMap; import java.util.Map; import com.ibm.mq.MQC; i ...