「APIO 2019」路灯
显然一个熟练的选手应该能一眼看出我们需要维护点对的答案
显然在断开或连上某一条边的时候只会对左右两边联通的点产生贡献,这个拿\(set\)维护一下就好了
那现在的问题就是怎么维护了
考虑一个非常\(sb\)的问题,我们只想知道一个点对\((x,y)\)从开始到某个时间\(t\)有多少个时间是联通的
如果\(i\)时刻\((x,y)\)突然联通了,那么我们就把答案加上\(t-i+1\),如果\(i\)时刻\((x,y)\)突然断开了,我们就把答案减去\(t-i+1\),正确性显然
于是我们只需要分别维护那些常数和加上了多少个当前时间就可以回答任意时刻的询问了
所以问题变成了矩阵加单点查,显然可以差分之后变成一个三维偏序问题,可以直接大力\(cdq\),当然也可以直接上树套树
代码
#include <bits/stdc++.h>
#define L first
#define R second
#define re register
#define LL long long
#define lb(x) ((x) & (-x))
#define mp std::make_pair
#define set_it std::set<pii>::iterator
inline int read() {
char c = getchar();
int x = 0;
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - 48, c = getchar();
return x;
}
typedef std::pair<int, int> pii;
const int maxn = 3e5 + 5;
const int M = maxn * 200;
std::set<pii> s;
int n, m, B, cnt;
char S[maxn], op[12];
int rt[maxn << 2];
LL g[M], A;
int h[M], l[M], r[M];
int ins(int now, int x, int y, int pos, int v, int w) {
if (!now)
now = ++cnt;
if (x == y) {
g[now] += v, h[now] += w;
return now;
}
int mid = x + y >> 1;
if (pos <= mid)
l[now] = ins(l[now], x, mid, pos, v, w);
else
r[now] = ins(r[now], mid + 1, y, pos, v, w);
g[now] = g[l[now]] + g[r[now]];
h[now] = h[l[now]] + h[r[now]];
return now;
}
void find(int now, int x, int y, int pos) {
if (!now)
return;
if (x == y) {
A += g[now];
B += h[now];
return;
}
int mid = x + y >> 1;
if (pos <= mid)
find(l[now], x, mid, pos);
else
find(r[now], mid + 1, y, pos), A += g[l[now]], B += h[l[now]];
}
void change(int x, int y, int a, int b) {
if (y > n)
return;
for (re int i = x; i <= n; i += lb(i)) rt[i] = ins(rt[i], 1, n, y, a, b);
}
void query(int x, int y) {
for (re int i = x; i; i -= lb(i)) find(rt[i], 1, n, y);
}
inline pii ask(int pos) {
s.insert(mp(pos, n + 1));
set_it it = s.find(mp(pos, n + 1));
--it;
s.erase(mp(pos, n + 1));
return *it;
}
inline void add(int x, int y, int lx, int ry, int t, int v) {
change(x, lx, v * (1 - t), v);
change(x, ry + 1, v * (t - 1), -1 * v);
change(y + 1, lx, v * (t - 1), -1 * v);
change(y + 1, ry + 1, v * (1 - t), v);
}
inline void getAns(int t) {
int x = read(), y = read();
if (x > y)
std::swap(x, y);
A = 0, B = 0;
query(x, y);
printf("%d\n", A + B * t);
}
int main() {
n = read() + 1, m = read();
scanf("%s", S + 1);
for (re int i = 1; i < n; i++) S[i] -= '0';
int t = 1;
for (re int i = 1; i <= n; i++)
if (!S[i])
s.insert(mp(t, i)), t = i + 1;
for (set_it it = s.begin(); it != s.end(); ++it) add((*it).L, (*it).R, (*it).L, (*it).R, 0, 1);
for (re int i = 1; i <= m; i++) {
scanf("%s", op);
if (op[0] == 'q')
getAns(i - 1);
if (op[0] == 't') {
int x = read();
pii ll = ask(x), rr = ask(x + 1);
if (!S[x]) {
add(ll.L, ll.R, rr.L, rr.R, i, 1);
s.erase(ll), s.erase(rr);
s.insert(mp(ll.L, rr.R));
} else {
add(ll.L, x, x + 1, rr.R, i, -1);
s.erase(ll);
s.insert(mp(ll.L, x));
s.insert(mp(x + 1, rr.R));
}
S[x] ^= 1;
}
}
return 0;
}
「APIO 2019」路灯的更多相关文章
- #3146. 「APIO 2019」路灯
#3146. 「APIO 2019」路灯 题目描述 一辆自动驾驶的出租车正在 Innopolis 的街道上行驶.该街道上有 \(n + 1\) 个停车站点,它们将街道划分成了 \(n\) 条路段.每一 ...
- #3145. 「APIO 2019」桥梁
#3145. 「APIO 2019」桥梁 题目描述 圣彼得堡市内所有水路长度总和约 282 千米,市内水域面积占城市面积的 7%.--来自维基百科 圣彼得堡位于由 \(m\) 座桥梁连接而成的 \(n ...
- #3144. 「APIO 2019」奇怪装置
#3144. 「APIO 2019」奇怪装置 题目描述 考古学家发现古代文明留下了一种奇怪的装置.该装置包含两个屏幕,分别显示两个整数 \(x\) 和 \(y\). 经过研究,科学家对该装置得出了一个 ...
- 「APIO 2019」桥梁
题目 三天终于把\(APIO\)做完了 这题还是比较厉害的,如果不知道这是个分块应该就自闭了 考虑一个非常妙的操作,按照操作分块 我们设一个闸值\(S\),把\(S\)个边权修改操作分成一块,把所有的 ...
- 【LOJ #3144】「APIO 2019」奇怪装置
题意: 定义将一个\(t\)如下转换成一个二元组: \[ f(t) = \begin{cases} x = (t + \left\lfloor \frac{t}{B} \right \rfloor) ...
- 「APIO 2019」奇怪装置
题目 考虑推柿子 最开始的想法是如果两个\(t\)在\(mod\ B\)意义下相等,那么只需要比较一下\((t+\left \lfloor \frac{t}{B}\rfloor \right)mod\ ...
- 「WC 2019」数树
「WC 2019」数树 一道涨姿势的EGF好题,官方题解我并没有完全看懂,尝试用指数型生成函数和组合意义的角度推了一波.考场上只得了 44 分也暴露了我在数数的一些基本套路上的不足,后面的 \(\ex ...
- LOJ#3054. 「HNOI 2019」鱼
LOJ#3054. 「HNOI 2019」鱼 https://loj.ac/problem/3054 题意 平面上有n个点,问能组成几个六个点的鱼.(n<=1000) 分析 鱼题,劲啊. 容易想 ...
- 【题解】#6622. 「THUPC 2019」找树 / findtree(Matrix Tree+FWT)
[题解]#6622. 「THUPC 2019」找树 / findtree(Matrix Tree+FWT) 之前做这道题不理解,有一点走火入魔了,甚至想要一本近世代数来看,然后通过人类智慧思考后发现, ...
随机推荐
- 使用idea开发分布式项目中优化tomact的方法
1. idea内存优化 找到idea安装目录,我的是在D:\IDEA\bin目录中 找到idea.exe.vmoptions和idea64.exe.vmoptions文件 这两个文件全部改成如下配置, ...
- NX二次开发-C++ DeleteFile删除文件实例代码
NX9+VS2012 #include<Windows.h> DeleteFile("D:\\1\\test123.prt"); Caesar卢尚宇 2019年7月29 ...
- 原生js - 两种图片懒加载实现原理
目前图片懒加载的方式主要有两种: 1.利用getBoundingClientRectAPI得到当前元素与视窗的距离来判断 2.利用h5的新API IntersectionObserver 来实现 ge ...
- 说说ReactiveCocoa 2
http://www.cocoachina.com/applenews/devnews/2014/0115/7702.html 转自无网不剩的博客 ReactiveCocoa是Github开源 ...
- 知识整理:字符串hash
字符串hash唯一用途是快速判断两字符串是否相等,但存在极小概率假阳性(本来不相等,但算法返回相等). 根本思想是把一个字符串转换为一个整数,要求相同的字符串,对应的这个整数相同,不同的字符串,对应的 ...
- ionic-CSS:ionic checkbox(复选框)
ylbtech-ionic-CSS:ionic checkbox(复选框) 1.返回顶部 1. ionic checkbox(复选框) ionic 里面的 Checkbox 和普通的 Checkbox ...
- 剑指offer——14机器人的运动范围
题目描述 地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子. 例如,当k为18时,机器人能 ...
- C++11多线程程序运行错误
编译没有问题,错误如图: 错误原因: http://stackoverflow.com/questio ... ation-not-permitted gcc4.6以后对于ld自动加上了as-need ...
- scrapy的使用--Rcrapy-Redis
Scrapy-Redis分布式爬虫组件 Scrapy是一个框架,他本身是不支持分布式的.如果我们想要做分布式的爬虫.就需要借助一个组件叫做Scrapy-Redis.这个组件正式利用了Redis可以分布 ...
- 【POJ】3660 Cow Contest
题目链接:http://poj.org/problem?id=3660 题意:n头牛比赛,有m场比赛,两两比赛,前面的就是赢家.问你能确认几头牛的名次. 题解:首先介绍个东西,传递闭包,它可以确定尽可 ...