[BZOJ3262]陌上花开 CDQ+树状数组

2023-12-07 06:58

本文主要是介绍[BZOJ3262]陌上花开 CDQ+树状数组,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

三维的有序 第一维排序 第二维CDQ 第三维树状数组 

注意要把完全相同的两个点合在一起 他们都互相比对方美丽

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#define SF scanf
#define PF printf
#define lowbit(x) ((x) & (-(x)))
using namespace std;
typedef long long LL;
const int MAXN = 100000;
const int MAXK = 200000;
struct Node {int a, b, c, cnt, ans;bool operator < (const Node &t) const {if(a != t.a) return a < t.a;if(b != t.b) return b < t.b;return c < t.c;}
} A[MAXN+10], tmp[MAXN+10];
bool cmp(const Node &a, const Node &b) {if(a.b != b.b) return a.b < b.b;if(a.c != b.c) return a.c < b.c;return a.a < b.a;
}
int n, K, tot, c[MAXK+10], tim[MAXK+10], T, cnt[MAXK+10];
void add(int x, int val) {for( ; x <= K; x += lowbit(x)) {if(tim[x] != T) c[x] = 0;tim[x] = T; c[x] += val;}
}
int query(int x) {int ret = 0;for( ; x; x -= lowbit(x)) if(tim[x] == T)ret += c[x];return ret;
}
void CDQ(int L, int R) {if(L == R) {A[L].ans += A[L].cnt-1;return ;}int mid = (L+R) >> 1, l1, l2;l1 = L; l2 = mid+1;for(int i = L; i <= R; i++) if(A[i].a <= mid) tmp[l1++] = A[i];else tmp[l2++] = A[i];for(int i = L; i <= R; i++) A[i] = tmp[i];CDQ(L, mid);int j = L; T++;for(int i = mid+1; i <= R; i++) {for( ; j <= mid && A[j].b <= A[i].b; j++) add(A[j].c, A[j].cnt);A[i].ans += query(A[i].c);}CDQ(mid+1, R);l1 = L; l2 = mid+1;for(int i = L; i <= R; i++) if(l1 <= mid && (l2 > R || cmp(A[l1], A[l2]))) tmp[i] = A[l1++];else tmp[i] = A[l2++];for(int i = L; i <= R; i++) A[i] = tmp[i];
}
int main() {SF("%d%d", &n, &K);for(int i = 1; i <= n; i++) SF("%d%d%d", &A[i].a, &A[i].b, &A[i].c), A[i].cnt++;sort(A+1, A+1+n);for(int i = 1; i <= n; i++) {if(i == 1 || A[i-1] < A[i]) A[++tot] = A[i];else A[tot].cnt++;}for(int i = 1; i <= tot; i++) A[i].a = i;sort(A+1, A+1+tot, cmp);CDQ(1, tot);for(int i = 1; i <= tot; i++) cnt[A[i].ans] += A[i].cnt;for(int i = 0; i < n; i++) PF("%d\n", cnt[i]);
}


这篇关于[BZOJ3262]陌上花开 CDQ+树状数组的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/464913

相关文章

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

C语言:柔性数组

数组定义 柔性数组 err int arr[0] = {0}; // ERROR 柔性数组 // 常见struct Test{int len;char arr[1024];} // 柔性数组struct Test{int len;char arr[0];}struct Test *t;t = malloc(sizeof(Test) + 11);strcpy(t->arr,

C 语言基础之数组

文章目录 什么是数组数组变量的声明多维数组 什么是数组 数组,顾名思义,就是一组数。 假如班上有 30 个同学,让你编程统计每个人的分数,求最高分、最低分、平均分等。如果不知道数组,你只能这样写代码: int ZhangSan_score = 95;int LiSi_score = 90;......int LiuDong_score = 100;int Zhou

计算数组的斜率,偏移,R2

模拟Excel中的R2的计算。         public bool fnCheckRear_R2(List<double[]> lRear, int iMinRear, int iMaxRear, ref double dR2)         {             bool bResult = true;             int n = 0;             dou

C# double[] 和Matlab数组MWArray[]转换

C# double[] 转换成MWArray[], 直接赋值就行             MWNumericArray[] ma = new MWNumericArray[4];             double[] dT = new double[] { 0 };             double[] dT1 = new double[] { 0,2 };

PHP7扩展开发之数组处理

前言 这次,我们将演示如何在PHP扩展中如何对数组进行处理。要实现的PHP代码如下: <?phpfunction array_concat ($arr, $prefix) {foreach($arr as $key => $val) {if (isset($prefix[$key]) && is_string($val) && is_string($prefix[$key])) {$arr[

Go 数组赋值问题

package mainimport "fmt"type Student struct {Name stringAge int}func main() {data := make(map[string]*Student)list := []Student{{Name:"a",Age:1},{Name:"b",Age:2},{Name:"c",Age:3},}// 错误 都指向了最后一个v// a

码蹄集部分题目(2024OJ赛9.4-9.8;线段树+树状数组)

1🐋🐋配对最小值(王者;树状数组) 时间限制:1秒 占用内存:64M 🐟题目思路 MT3065 配对最小值_哔哩哔哩_bilibili 🐟代码 #include<bits/stdc++.h> using namespace std;const int N=1e5+7;int a[N],b[N],c[N],n,q;struct QUERY{int l,r,id;}que

【第0006页 · 数组】寻找重复数

【前言】本文以及之后的一些题解都会陆续整理到目录中,若想了解全部题解整理,请看这里: 第0006页 · 寻找重复数         今天想讨论的一道题在 LeetCode 上评论也是颇为“不错”。有一说一,是道好题,不过我们还是得先理解了它才算真正的好题。这里我们展示一种使用二进制的做法,希望能帮到你哟! 【寻找重复数】给定一个包含 n + 1 个整数的数组 nums ,其数字都