「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 =& ...
随机推荐
- 使用php-vmstat遇到的麻烦
workerman-vmstat是一个基于workerman的扩展,用于监听服务器应用对内存.cpu消耗的友好的查看功能,具体介绍可以去git上看: https://github.com/wal ...
- 不需要图片,css+svg绘制动态loading加载图标
1.html 部分: <div id="refershDiv" class="refershDiv"> <svg xmlns="ht ...
- SPAN, RSPAN, ERSPAN
该文档摘自:Home > CCIE Routing and Switching Study Group > Discussions 由 Deben 于 2015-2-6 上午6:50 创建 ...
- 原生js登录创建cookie
原生js创建cookie,功能:点击登录按钮时,将用户名.密码存为cookie:页面再次加载时,自动读取cookie中的用户名.密码. <html><head><titl ...
- python splash scrapy
python splash scrapy 1. 前言 slpash是一个渲染引擎,它有自己的api,可以直接访问splash服务的http接口,但也有对应的包python-splash方便调 ...
- Java 9 New Features
Java 9 概述 1. jdk 9 的发布.经过 4 次跳票,历经曲折的 java 9 终于终于在 2017 年 9 月 21 日发布. 2. Java 9 中哪些不得不说的新特性?java 9 提 ...
- string和stringBuffer,stringBuilder的区别
1,String类的内容一旦声明后是不可改变的,改变的只是其内存的指向,而StringBuffer类的对象内容是可以改变的. 2,对于StringBuffer,不能像String那样直接通过赋值的方式 ...
- 【转】网关协议学习:CGI、FastCGI、WSGI、uWSGI
一直对这四者的概念和区别很模糊,现在就特意梳理一下它们的关系与区别. CGI CGI即通用网关接口(Common Gateway Interface),是外部应用程序(CGI程序)与Web服务器之间的 ...
- Fleck WebSocket使用
Fleck WebSocket使用 作为笔记存储. 最近公司有这方面的使用需求.在网上查了一些资料后.得到了想要的结果.以下记录摘抄至网上资料. 1.首先,服务端.项目NuGet直接引用Fleck类库 ...
- Nginx 反向代理报400错误解决方法!
如果后端真是的服务器设置有类似防盗链或者根据http请求头中的host字段来进行路由或判断功能的话,如果反向代理层的nginx不重写请求头中的host字段,将会导致请求失败,报400错误,解决办法: ...