HDU 5618 Jam's problem again (cdq分治+BIT 或 树状数组套Treap)
题意:给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)的更多相关文章
- HDU 5618 Jam's problem again CDQ分治 BC ROUND 70
题意:给你1e5个点(x,y,z),对于每一个点询问有多少个点(x1,y1,z1)满足x1<=x&&y1<=y&&z1<=z 分析:(官方题解奉上)很 ...
- 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 ...
- 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; ...
- HDU5618 Jam's problem again CDQ分治
Jam's problem again CDQ分治 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5618 题意: \[ 有n 个元素,第 i 个元素有 ...
- 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 ...
- [APIO2019] [LOJ 3146] 路灯 (cdq分治或树状数组套线段树)
[APIO2019] [LOJ 3146] 路灯 (cdq分治或树状数组套线段树) 题面 略 分析 首先把一组询问(x,y)看成二维平面上的一个点,我们想办法用数据结构维护这个二维平面(注意根据题意这 ...
- HDU 4267 A Simple Problem with Integers 多个树状数组
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 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 ...
- BZOJ3262/洛谷P3810 陌上花开 分治 三维偏序 树状数组
原文链接http://www.cnblogs.com/zhouzhendong/p/8672131.html 题目传送门 - BZOJ3262 题目传送门 - 洛谷P3810 题意 有$n$个元素,第 ...
随机推荐
- egret 配置设置
修改index.html的时候,要主要template文件夹下的web文件夹也有个index.html,两者控制的不一样 初始安装新建项目后调试这样的情况.重新安装引擎和下载egret安装包安装,默认 ...
- 控制html元素的隐藏问题
控制元素隐藏的方式,有display:none.visibility:hidden以及不透明度设置. 一.display:none 被隐藏的元素,在页面中不占位,空出的位置会被相邻的元素占用. < ...
- 创建jsp+Servlet+JavaBean+JDBC+MySQL项目的过程
1 根据需求建立Mysql数据,确立数据库的表的字段.属性.主键,外键等.下面我使用的数据库名dev ,表名user,字段 name,设置为主键.用户名不能为空,字段password,密码 2 在E ...
- 如何使用Python画地图数据
http://blog.csdn.net/wen_fei/article/details/78355699
- form表单重置、清空方法记录
myform 是form的id属性值 1.调用reset()方法 function fomrReset() { document.getElementById("myform"). ...
- Python3使用csv模块csv.writer().writerow()保存csv文件,产生空行的问题
问题:csv.writer().writerow()保存的csv文件,打开时每行后都多一行空行 解决方法:在open()内增加一个参数newline='' 即可 问题现象: 1.代码 with ...
- 修改hosts,***
某些网站之所以在国内上不了,是因为dns受到干扰,无法解析出正确的ip地址. 可以在hosts文件中加入网站对应的正确ip地址,进行访问. 1.打开hosts文件, 路径为 C:\Windows\S ...
- 第八章 高级搜索树 (xa3)红黑树:插入
- Eclipse中的SVN操作
--------------------siwuxie095 Eclipse 中的 SVN 操作 (一)发布项目 ...
- Asp.net实现同页面内多图片自动上传并带预览显示
FileUpload控件实现单按钮图片自动上传并带预览显示 1.实现原理: 此方法适合针对有后台生成的图片相关内容,例如购物网站商品展示页面中的封面图片,图片的数量由后台访问数据库,并加载到页面.这种 ...