题意:给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. 11 并发编程-(线程)-信号量&Event&定时器

    1.信号量(本质也是一把锁)Semaphore模块 信号量也是一把锁,可以指定信号量为5,对比互斥锁同一时间只能有一个任务抢到锁去执行, 信号量同一时间可以有5个任务拿到锁去执行, 如果说互斥锁是合租 ...

  2. Eclipse json文件报错

    只要找一个json在线解析,验证你的json文件格式的正确性,错误可以忽略. 如要消除红叉,关闭Json Validation即可,如下操作: Window > Preferences > ...

  3. sqlserver列重命名

    EXEC sp_rename 'tablename.[OldFieldName ]', 'NewFieldName', 'COLUMN'

  4. How to Pronounce T and D between Consonants

    How to Pronounce T and D between Consonants Share Tweet Share Tagged With: Dropped T What happens to ...

  5. C#实现支持单点登录的一个存储用户信息的类

    网上有很多介绍单点登录的文章,但多为架构设计以及概念性文章,而本文将介绍单点登录的具体具体实现 利用哈希表,作为保存登录用户的队列        private static Hashtable m_ ...

  6. oracle表属性

    1. PCTFREE 要形容一个 BLOCK 的运作,我们可以把一个 BLOCK 想成一个水杯.侍者把水倒入放在我们面前的水杯,要多满呢,我们要求他倒 9 分满好了,这时候 PCTFREE 代表着设定 ...

  7. node集成mysql——pool连接池

    安装 mysql npm install mysql or cnpm install mysql 创建db.js,实现mysql操作模块 var mysql = require('mysql'); v ...

  8. pymongo的常用操作

    环境:pymongo3.0.3,python3 以下是我整理的一些关于pymongo的操作,网上很多是用pymongo.Connecion()去连接数据库的,但是我这里连接一直提示没有这个包,如果大家 ...

  9. 让IIS支持PHP的配置步骤

    本文转自:http://marsren.blog.51cto.com/116511/41199/ 在Windows Server 2003的IIS6下配置ISAPI方式的PHP,配置方法是,在IIS的 ...

  10. org.apache.catalina.core.StandardContext.listenerStart Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException

    使用Intelij Idea时,报错如下: org.apache.catalina.core.StandardContext.listenerStart Error configuring appli ...