bzoj 1176 CDQ分治
思路:首先我们将问题转换一下,变成问在某个点左下角的权值和,那么每一个询问可以拆成4的这样的询问,然后
进行CDQ 分治,回溯的时候按x轴排序,然后用树状数组维护y的值。
- #include<bits/stdc++.h>
- #define LL long long
- #define fi first
- #define se second
- #define mk make_pair
- #define pii pair<int, int>
- #define x2 skdjflsdg
- #define y2 sdkfjsktge
- using namespace std;
- const int N = 2e6 + ;
- const int M = 1e6 + ;
- const int inf = 0x3f3f3f3f;
- const LL INF = 0x3f3f3f3f3f3f3f3f;
- const int mod = 1e9 +;
- int s, w, cnt, tot, op;
- LL ans[M], a[N];
- struct Qus {
- int x, y, idx, id;
- } qus[M], tmp[M];
- void modify(int x, int v) {
- for(int i = x; i < N; i += i & -i) {
- a[i] += v;
- }
- }
- LL sum(int x) {
- LL ans = ;
- for(int i = x; i; i -= i & -i) {
- ans += a[i];
- }
- return ans;
- }
- void cdq(int l, int r) {
- if(l == r) return;
- int mid = l + r >> ;
- cdq(l, mid); cdq(mid + , r);
- int p = l, q = mid + , cnt = l;
- for(int i = mid + ; i <= r; i++) {
- while(p <= mid && qus[p].x <= qus[i].x) {
- if(qus[p].idx == )
- modify(qus[p].y, qus[p].id);
- p++;
- }
- ans[qus[i].id] += qus[i].idx * sum(qus[i].y);
- }
- for(int i = l; i < p; i++) {
- if(qus[i].idx == ) {
- modify(qus[i].y, -qus[i].id);
- }
- }
- p = l, q = mid + , cnt = l;
- while(p <= mid && q <= r) {
- if(qus[p].x <= qus[q].x) {
- tmp[cnt++] = qus[p++];
- } else {
- tmp[cnt++] = qus[q++];
- }
- }
- while(p <= mid) tmp[cnt++] = qus[p++];
- while(q <= r) tmp[cnt++] = qus[q++];
- for(int i = l; i <= r; i++) qus[i] = tmp[i];
- }
- int main() {
- scanf("%d%d", &s, &w);
- while(scanf("%d", &op) && op < ) {
- if(op == ) {
- int x, y, a; scanf("%d%d%d", &x, &y, &a);
- x += ; y += ;
- qus[++tot].x = x;
- qus[tot].y = y;
- qus[tot].id = a;
- qus[tot].idx = ;
- } else {
- int x1, y1, x2, y2;
- scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
- x1 += , y1 += ;
- x2 += , y2 += ;
- qus[++tot].x = x2;
- qus[tot].y = y2;
- qus[tot].id = ++cnt;
- qus[tot].idx = ;
- qus[++tot].x = x2;
- qus[tot].y = y1 - ;
- qus[tot].id = cnt;
- qus[tot].idx = -;
- qus[++tot].x = x1 - ;
- qus[tot].y = y2;
- qus[tot].id = cnt;
- qus[tot].idx = -;
- qus[++tot].x = x1 - ;
- qus[tot].y = y1 - ;
- qus[tot].id = cnt;
- qus[tot].idx = ;
- ans[cnt] = 1ll * (x2 - x1) * (y2 - y1) * s;
- }
- }
- cdq(, tot);
- for(int i = ; i <= cnt; i++)
- printf("%lld\n", ans[i]);
- return ;
- }
- /*
- */
bzoj 1176 CDQ分治的更多相关文章
- bzoj 1176 cdq分治套树状数组
题面: 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000. Inp ...
- BZOJ 1537 cdq分治
思路: 我只是想写一下cdq-- 二维偏序 一维排序 一维cdq分治 (我忘了归并排序怎么写了,,,) 写了个sort- 复杂度是O(nlog^2n) //By SiriusRen #include ...
- BZOJ 3262 cdq分治 OR 树套树
注意判断 三个条件都一样的-- (CDQ分治 其实并不是很难理解 只是想不到--) CDQ分治: //By SiriusRen #include <cstdio> #include < ...
- bzoj 2683 CDQ分治
题目描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 将格子x,y里的数 ...
- BZOJ - 1935 / 1176 cdq分治 三维偏序
题意:给定n*m的网格,且给出n个(x,y)表示该网格已被占有,q次询问(x1,y1)到(x2,y2)的网格中有多少个被占有,n,m范围1e7,q范围5e5 cdq按x轴排序,树状数组维护y轴 #in ...
- 【BZOJ4237】稻草人(CDQ分治,单调栈)
[BZOJ4237]稻草人(CDQ分治,单调栈) 题面 BZOJ 题解 \(CDQ\)分治好题呀 假设固定一个左下角的点 那么,我们可以找到的右下角长什么样子??? 发现什么? 在右侧是一个单调递减的 ...
- BZOJ 1176: [Balkan2007]Mokia( CDQ分治 + 树状数组 )
考虑cdq分治, 对于[l, r)递归[l, m), [m, r); 然后计算[l, m)的操作对[m, r)中询问的影响就可以了. 具体就是差分答案+排序+离散化然后树状数组维护.操作数为M的话时间 ...
- BZOJ 1176 Mokia CDQ分治+树状数组
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 821[Submit][St ...
- 【BZOJ】1176: [Balkan2007]Mokia(cdq分治)
http://www.lydsy.com/JudgeOnline/problem.php?id=1176 在写这题的时候思维非常逗啊........2333................... 最后 ...
随机推荐
- 怎么用spring cloud service-id 进行调用接口
这里最关键的就是加上@LoadBalanced @SpringBootApplication public class ConsumerMovieApplication { @Bean @LoadBa ...
- [DeeplearningAI笔记]序列模型1.1-1.2序列模型及其数学符号定义
5.1循环序列模型 觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.1什么是序列模型 在进行语音识别时,给定了一个输入音频片段X,并要求输出片段对应的文字记录Y,这个例子中的输入和输出都输 ...
- [LeetCode] 28. Implement strStr() ☆
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【Android】完善Android学习(七:API 4.0.3)
备注:之前Android入门学习的书籍使用的是杨丰盛的<Android应用开发揭秘>,这本书是基于Android 2.2API的,目前Android已经到4.4了,更新了很多的API,也增 ...
- Debian最完美安装flash的教程//适用于所有linux版本
话说不管是新手还是老手,都离不开flash.没有flash的支持,菜鸟们也少了一些把玩linux的动力. flash有很多安装的方法,不过性能相差很大.这里的缘由就不重要了. 下面我介绍在chromi ...
- 庞老师集群.ziw
2017年2月17日, 星期五 庞老师集群 链接:http://pan.baidu.com/s/1mhSw2TE 密码:hzz4 更改子网IP,及网关: null
- C++类四个默认函数&深复制&浅复制
学习C++语言的同学都知道,C++中类是有默认的几个函数的,主要是有四个函数: 四个函数 默认构造函数:A(void),无参构造函数 拷贝(复制)构造函数:A(const A&a).用一个对象 ...
- PHP与Ajax
如何用PHP接收JSON格式数据 1.一般来说,我们直接用$_POST $_REQUEST $_GET这样的超全局变量接收就好了 <?php $obj_temp=$_POST['data']; ...
- Java实现链式存储的二叉树
二叉树的定义: 二叉树(BinaryTree)是n(n≥0)个结点的有限集,它或者是空集(n=0),或者由一个根结点及两棵互不相交的.分别称作这个根的左子树和右子树的二叉树组成. 二叉树的遍历方式主要 ...
- Linux内核同步原语之原子操作【转】
转自:http://blog.csdn.net/npy_lp/article/details/7262388 避免对同一数据的并发访问(通常由中断.对称多处理器.内核抢占等引起)称为同步. ——题记 ...