bzoj1176: [Balkan2007]Mokia cdq
链接
思路
cdq入门题,拆成4个矩阵,然后cdq。
代码
/**************************************************************
Problem: 1176
User: gryz2016
Language: C++
Result: Accepted
Time:2652 ms
Memory:13012 kb
****************************************************************/
#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) (x&-x)
const int N = 2e5 + 7;
int read() {
int x = 0, f = 1; char s = getchar();
for (; s > '9' || s < '0'; s = getchar()) if (s == '-') f = -1;
for (; s >= '0' && s <= '9'; s = getchar()) x = x * 10 + s - '0';
return x * f;
}
int n, a[N], ans[N];
struct ask {
int opt, x, y, w, id;
ask(int a = 0, int b = 0, int c = 0, int d = 0, int e = 0) {
opt = a, x = b, y = c, w = d, id = e;
}
bool operator < (const ask &b) const {
return x == b.x ? opt < b.opt : x < b.x;
}
} Q[N], tmp[N];
int lsh_y[N << 1];
namespace BIT {
int sum[N], maxn;
void add(int id, int w) {
for (int i = id; i <= maxn; i += lowbit(i)) sum[i] += w;
}
int query(int x) {
int ans = 0;
for (int i = x; i >= 1; i -= lowbit(i)) ans += sum[i];
return ans;
}
}
void cdq(int l, int r) {
if (l == r) return;
int mid = (l + r) >> 1;
cdq(l, mid), cdq(mid + 1, r);
int p = l, q = mid + 1, js = l;
while (p <= mid && q <= r) {
if (Q[p] < Q[q]) {
if (Q[p].opt == 1) BIT::add(Q[p].y, Q[p].w);
tmp[js++] = Q[p++];
} else {
if (Q[q].opt == 2) ans[Q[q].id] += Q[q].w * BIT::query(Q[q].y);
tmp[js++] = Q[q++];
}
}
if (p <= mid) {
for (int i = l; i < p; ++i) if (Q[i].opt == 1) BIT::add(Q[i].y, -Q[i].w);
while (p <= mid) tmp[js++] = Q[p++];
} else {
while (q <= r) {
if (Q[q].opt == 2) ans[Q[q].id] += Q[q].w * BIT::query(Q[q].y);
tmp[js++] = Q[q++];
}
for (int i = l; i <= mid; ++i) if (Q[i].opt == 1) BIT::add(Q[i].y, -Q[i].w);
}
for (int i = l; i <= r; ++i) Q[i] = tmp[i];
}
int main() {
// freopen("a.in", "r", stdin);
int S = read(), W = read(), n = 0, DSR = 0;
while (233) {
int opt = read();
if (opt == 3) break;
if (opt == 1) {
int x = read(), y = read(), w = read();
Q[++n] = ask(opt, x, y, w), lsh_y[++lsh_y[0]] = Q[n].y;
} else {
++DSR;
int a = read(), b = read(), x = read(), y = read();
if (x && y) Q[++n] = ask(opt, x, y, 1, DSR), lsh_y[++lsh_y[0]] = Q[n].y;
if (a - 1 && b - 1) Q[++n] = ask(opt, a - 1, b - 1, 1, DSR), lsh_y[++lsh_y[0]] = Q[n].y;
if (a - 1 && y) Q[++n] = ask(opt, a - 1, y, -1, DSR), lsh_y[++lsh_y[0]] = Q[n].y;
if (x && b - 1) Q[++n] = ask(opt, x, b - 1, -1, DSR), lsh_y[++lsh_y[0]] = Q[n].y;
}
}
sort(lsh_y + 1, lsh_y + 1 + lsh_y[0]);
lsh_y[0] = unique(lsh_y + 1, lsh_y + 1 + lsh_y[0]) - lsh_y - 1;
for (int i = 1; i <= n; ++i) Q[i].y = lower_bound(lsh_y + 1, lsh_y + 1 + lsh_y[0], Q[i].y) - lsh_y;
BIT::maxn = lsh_y[0] + 1;
cdq(1, n);
for (int i = 1; i <= DSR; ++i) printf("%d\n", ans[i]);
return 0;
}
bzoj1176: [Balkan2007]Mokia cdq的更多相关文章
- [BZOJ1176][Balkan2007]Mokia cdq+树状数组
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 3134 Solved: 1395[Submit][S ...
- BZOJ1176: [Balkan2007]Mokia CDQ分治
最近很不对啊=w= 写程序全是bug啊 ans数组开小了竟然一直不知道,小数据没问题大数据拍不过,交上去RE 蛋疼半天 这个主要把每次询问拆成3个询问. #include<cstdio> ...
- BZOJ 1176: [Balkan2007]Mokia( CDQ分治 + 树状数组 )
考虑cdq分治, 对于[l, r)递归[l, m), [m, r); 然后计算[l, m)的操作对[m, r)中询问的影响就可以了. 具体就是差分答案+排序+离散化然后树状数组维护.操作数为M的话时间 ...
- BZOJ 1176[Balkan2007]Mokia(CDQ分治)
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 3381 Solved: 1520[Submit][S ...
- bzoj1176: [Balkan2007]Mokia【cdq分治】
把询问搞成4个,cdq分治. #include <bits/stdc++.h> #define rep(i, a, b) for (int i = a;i <= b; i++) #d ...
- BZOJ1176 [Balkan2007]Mokia 【CDQ分治】
题目 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000. 输入格式 ...
- BZOJ1176 [Balkan2007]Mokia(CDQ)
CDQ裸题,\(x\), \(y\), \(tim\)三维偏序 #include <cstdio> #include <iostream> #include <cstri ...
- 2018.09.16 bzoj1176: [Balkan2007]Mokia(cdq分治)
传送门 调了半天发现是输出优化打错了求心理阴影体积233 这题很简单啊. 一个修改操作x如果对一个询问操作y有贡献那么有. tx<ty,Xx<=Xy,Yx<=Yy" rol ...
- cdq分治入门--BZOJ1176: [Balkan2007]Mokia
对w*w,w<=2000000的矩形,一开始全是0(或一开始全是s),n<=170000个操作,每次操作:矩阵内某点加上一个数,查某一个子矩阵的和,保证修改数<=160000,询问数 ...
随机推荐
- 总结:WPF中ResourceDictionary资源文件的查找和遍历方法
原文:总结:WPF中ResourceDictionary资源文件的查找和遍历方法 一.查找包含制定关键字的资源 ResourceDictionary GetThemeDictionary() ...
- 分布式图片服务器FastDFS
1. 什么是FastDFS FastDFS 是用 c 语言编写的一款开源的分布式文件系统.FastDFS 为互联网量身定制,充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标,使 ...
- 2019 荔枝java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.荔枝等公司offer,岗位是Java后端开发,因为发展原因最终选择去了荔枝,入职一年时间了,也成为了面试官,之 ...
- 2019 途牛旅游网java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.途牛旅游网等公司offer,岗位是Java后端开发,因为发展原因最终选择去了途牛旅游网,入职一年时间了,也成为 ...
- Django:RestFramework之-------权限
4.restframework-权限 4.1权限: 权限在单个视图应用. class MyPermission(object): """认证类""&q ...
- Java 之 Random 类
一.Random 类 random 类的实例用于生成伪随机数. Demo: Random r = new Random(); int i = r.nextInt(); 二.Random 使用步骤 1 ...
- Java 使用properties配置文件加载配置
一般我们不把数据库的配置信息写死在代码中. 写好代码后,编译.调试,成功后只把输出目录中的东西(jar包..class文件.资源文件等)拷贝到服务器上,由运维来管理.服务器上是没有源文件的(.java ...
- 对Sql Server执行计划的浅显了解。
一名大三的小学生,今天开始我的第一篇博客,最近随便做了一个简易的酒店管理系统,对sql执行计划有了初步的了解. 查看上面语句的预估执行计划,在工具栏中有这个按钮 聚集索引扫描被称为Index Scan ...
- Microsoft SQL Server 2012安装说明
Microsoft SQL Server 2012安装说明 环境:Windows8, Windows7, WinVista, Win2003, WinXP Microsoft SQL Server 2 ...
- centos下导入mysql数据库
先进入mysql1.mysql -u root -p2.输入密码3.use 要导入的数据库名(没有就新建一个,使用create database test;命令新建,再use test;,再set n ...