「BOI2007」Mokia
「BOI2007」Mokia
传送门
把查询拆成四部分然后容斥计算答案(二维前缀和)
然后 \(\text{CDQ}\) 分治算答案。
参考代码:
#include <algorithm>
#include <cstdio>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while ('0' > c || c > '9') f |= c == '-', c = getchar();
while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
}
const int _ = 2e5 + 5, __ = 2e6 + 5;
int W, tr[__], num;
struct node { int opt, id, x, y, v, ans; } t[_], tt[_];
inline bool cmp(const node& a, const node& b) { return a.id < b.id; }
inline void update(int x, int v)
{ for (rg int i = x; i <= W; i += i & -i) tr[i] += v; }
inline int query(int x)
{ int res = 0; for (rg int i = x; i >= 1; i -= i & -i) res += tr[i]; return res; }
inline void CDQ(int l, int r) {
if (l == r) return ;
int mid = (l + r) >> 1;
CDQ(l, mid), CDQ(mid + 1, r);
int i = l, j = mid + 1, p = l;
while (i <= mid && j <= r) {
if (t[i].x <= t[j].x) { if (t[i].opt == 0) update(t[i].y, t[i].v); tt[p++] = t[i++]; }
else { if (t[j].opt == 1) t[j].ans += query(t[j].y); tt[p++] = t[j++]; }
}
while (i <= mid) { if (t[i].opt == 0) update(t[i].y, t[i].v); tt[p++] = t[i++]; }
while (j <= r) { if (t[j].opt == 1) t[j].ans += query(t[j].y); tt[p++] = t[j++]; }
for (rg int i = l; i <= mid; ++i) if (t[i].opt == 0) update(t[i].y, -t[i].v);
for (rg int i = l; i <= r; ++i) t[i] = tt[i];
}
int main() {
int opt; read(opt), read(W);
while (1) {
read(opt); if (opt == 3) break ;
if (opt == 1) {
int x, y, v; read(x), read(y), read(v);
t[++num] = (node) { 0, num, x, y, v, 0 };
} else {
int xl, yl, xr, yr;
read(xl), read(yl), read(xr), read(yr);
t[++num] = (node) { 1, num, xr, yr, 0, 0 };
t[++num] = (node) { 1, num, xl - 1, yr, 0, 0 };
t[++num] = (node) { 1, num, xr, yl - 1, 0, 0 };
t[++num] = (node) { 1, num, xl - 1, yl - 1, 0, 0 };
}
}
CDQ(1, num), sort(t + 1, t + num + 1, cmp);
for (rg int i = 1; i <= num; ++i)
if (t[i].opt == 1)
printf("%d\n", t[i].ans - t[i + 1].ans - t[i + 2].ans + t[i + 3].ans), i += 3;
return 0;
}
「BOI2007」Mokia的更多相关文章
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- 「译」JUnit 5 系列:扩展模型(Extension Model)
原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...
- JavaScript OOP 之「创建对象」
工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...
- 「C++」理解智能指针
维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...
- 「JavaScript」四种跨域方式详解
超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...
- 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management
写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...
- 「2014-3-18」multi-pattern string match using aho-corasick
我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...
- 「2014-3-17」C pointer again …
记录一个比较基础的东东-- C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...
- 「2014-3-13」Javascript Engine, Java VM, Python interpreter, PyPy – a glance
提要: url anchor (ajax) => javascript engine (1~4 articles) => java VM vs. python interpreter =& ...
随机推荐
- Bugku-CTF加密篇之奇怪的密码(突然天上一道雷电 gndk€rlqhmtkwwp}z )
奇怪的密码 突然天上一道雷电 gndk€rlqhmtkwwp}z
- ACM进阶之路
第一阶段:练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码, 因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打 出来. 3.大数(高精度)加减乘除 ...
- NIO的理解
一.缓冲区(Buffer):在java NIO中负责数据的存取,实际上就是数组,用于存储不用数据类型的数据,根据数据类型不同(boolean除外),提供了相应类型的缓冲区(ByteBuffer,Cha ...
- element 确认框 confirm 的写法
this.confirm('内容', '标题', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'success', callbac ...
- 【译】高级T-SQL进阶系列 (七)【下篇】:使用排序函数对数据进行排序
此文为翻译,由于本人水平有限,疏漏在所难免,欢迎探讨指正. 原文链接:传送门. 使用NTILE函数的示例 NTILE函数将一组记录分割为几个组.其返回的分组数是由一个整形表达式指定的.如下你会找到NT ...
- P1428
AC: #include <stdio.h> #define rep(i, a, b) for(int i = a; i <= b; i++) #define min(a, b) ( ...
- Cocos2dLua3.17.2集成FairyGUI之 lua绑定 (二)
上一章中将fairyGUI集成到C++工程,由于本人使用的是cocoslua,还需要将C++的绑定到lua中使用,本章记录一下过程,由于是过了一段时间,有些步骤忘记了,大概记录一下,诸位大大做个临时参 ...
- Centos7下载和安装教程
https://blog.csdn.net/qq_42570879/article/details/82853708 阿里下载64bit镜像:http://mirrors.aliyun.com/cen ...
- 转载--php 7.2 安装 mcrypt 扩展
在 php 官网下载 mcrypt 包,php 扩展官网 # wget http://pecl.php.net/get/mcrypt-1.0.1.tgz # tar xf mcrypt-1.0.1.t ...
- Java入门笔记 04-异常处理
一. 异常概述: 1. 异常体系结构图: java.lang.Throwable |-----java.lang.Error:一般不编写针对性的代码进行处理. |- ...