当前位置:网站首页>CF1677E Tokitsukaze and Beautiful Subsegments
CF1677E Tokitsukaze and Beautiful Subsegments
2022-07-30 13:41:00 【Heart is cold month】
CF1677E Tokitsukaze and Beautiful Subsegments
好题.
对于区间最大值,Consider monotonic stack maintenance,记一个数 a i a_i ai The first number on the left and right that is larger than him is L i L_i Li 和 R i R_i Ri.
则对于区间 [ l , r ] [l,r] [l,r] 满足 L i < l ≤ r < R i L_i<l\leq r<R_i Li<l≤r<Ri,有 max k = l r a k = a i \max_{k=l}^{r}a_k=a_i maxk=lrak=ai.
对于每个 a i a_i ai,We can try to determine the range of the left endpoint of the interval [ L , R ] [L,R] [L,R],初始 L = L i + 1 L=L_i+1 L=Li+1, R = L i R=L_i R=Li.
接着,枚举 a i a_i ai 的因数 x , y x,y x,y,判断是否 L i < p o s x , p o s y ≤ i L_i<pos_x,pos_y \leq i Li<posx,posy≤i,i.e. in range,然后更新 R = min ( p o s x , p o s y ) R=\min(pos_x,pos_y) R=min(posx,posy),总时间复杂度 O ( ∑ a i ) \mathcal O(\sum\sqrt{a_i}) O(∑ai).
Then enumerate the right endpoints of the interval,Calculate the product of a i a_i ai 的数的位置,判断是否在范围内,更新 R R R,记录.
This is recorded ( L , R , r ) (L,R,r) (L,R,r),表示右端点为 r r r,左端点为 [ L , R ] [L,R] [L,R] The interval is beautiful.
考虑扫描线,将询问离线,右端点升序,Put the right endpoint as i i i All triples of are added to the segment tree,i.e. interval plus 1 1 1.
时间复杂度 O ( n 2 log n ) \mathcal O(n^2\log n) O(n2logn).
考虑优化,每次枚举 [ i , R i ] [i,R_i] [i,Ri] 和 [ L i , i ] [L_i,i] [Li,i] Medium length is small,记录三元组,This way the time complexity is amortized O ( n log 2 n ) \mathcal O(n\log^2n) O(nlog2n).
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
#define ha putchar(' ')
#define he putchar('\n')
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return x * f;
}
inline void write(ll x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9)
write(x / 10);
putchar(x % 10 + 48);
}
const int _ = 1e6 + 10;
int n, q, s[_], t, a[_], pos[_], L[_], R[_], sz[_ << 2], tag[_ << 2], tr[_ << 2], as[_];
struct abc {
int l, r, id;
} qr[_];
bool cmp(abc a, abc b) {
return a.r < b.r;
}
bool cmp2(abc a, abc b) {
return a.l > b.l;
}
vector<pair<int, int>> d[_], d2[_];
void build(int o, int l, int r) {
sz[o] = (r - l + 1), tr[o] = tag[o] = 0;
if (l == r) return;
int mid = (l + r) >> 1;
build(o << 1, l, mid), build(o << 1 | 1, mid + 1, r);
}
void pushdown(int o) {
if (tag[o]) {
tr[o << 1] += sz[o << 1] * tag[o];
tr[o << 1 | 1] += sz[o << 1 | 1] * tag[o];
tag[o << 1] += tag[o];
tag[o << 1 | 1] += tag[o];
tag[o] = 0;
}
}
void upd(int o, int l, int r, int L, int R, int k) {
if (L <= l && r <= R) {
tr[o] += sz[o] * k, tag[o] += k;
return;
}
pushdown(o);
int mid = (l + r) >> 1;
if (L <= mid) upd(o << 1, l, mid, L, R, k);
if (R > mid) upd(o << 1 | 1, mid + 1, r, L, R, k);
tr[o] = tr[o << 1] + tr[o << 1 | 1];
}
int qry(int o, int l, int r, int L, int R) {
if (L <= l && r <= R) return tr[o];
pushdown(o);
int mid = (l + r) >> 1, res = 0;
if (L <= mid) res = qry(o << 1, l, mid, L, R);
if (R > mid) res += qry(o << 1 | 1, mid + 1, r, L, R);
tr[o] = tr[o << 1] + tr[o << 1 | 1];
return res;
}
signed main() {
n = read(), q = read();
for (int i = 1; i <= n; ++i) a[i] = read(), pos[a[i]] = i;
t = 0;
for (int i = 1; i <= n; ++i) {
while (t > 0 && a[s[t]] < a[i]) t--;
L[i] = s[t];
s[++t] = i;
}
t = 0, s[0] = n + 1;
for (int i = n; i >= 1; --i) {
while (t > 0 && a[s[t]] < a[i]) t--;
R[i] = s[t];
s[++t] = i;
}
for (int i = 1; i <= q; ++i)
qr[i].l = read(), qr[i].r = read(), qr[i].id = i;
for (int i = 1; i <= n; ++i) {
int l, r;
if (R[i] - i <= i - L[i]) {
r = L[i];
for (int j = 1; j * j <= a[i]; ++j) {
if (a[i] % j != 0) continue;
int fx = pos[j], fy = pos[a[i] / j];
if (fx == fy) continue;
if (fx > fy) swap(fx, fy);
if (fx > L[i] && fy <= i) r = max(r, fx);
}
for (int j = i; j < R[i]; ++j) {
if (a[i] % a[j] == 0) {
int nw = a[i] / a[j];
if (pos[nw] == j) continue;
if (pos[nw] > L[i] && pos[nw] < j) {
r = max(r, pos[nw]);
r = min(r, i);
}
}
if (L[i] < r) d[j].push_back({
L[i] + 1, r});
}
} else {
l = R[i];
for (int j = 1; j * j <= a[i]; ++j) {
if (a[i] % j != 0) continue;
int fx = pos[j], fy = pos[a[i] / j];
if (fx == fy) continue;
if (fx > fy) swap(fx, fy);
if (fy < R[i] && fx >= i) l = min(l, fy);
}
for (int j = i; j > L[i]; --j) {
if (a[i] % a[j] == 0) {
int nw = a[i] / a[j];
if (pos[nw] == j) continue;
if (pos[nw] > j && pos[nw] < R[i]) {
l = min(l, pos[nw]);
l = max(l, i);
}
}
if (l < R[i]) d2[j].push_back({
l, R[i] - 1});
}
}
}
sort(qr + 1, qr + q + 1, cmp);
build(1, 1, n);
int k = 1;
for (int i = 1; i <= n; ++i) {
for (auto v : d[i]) upd(1, 1, n, v.first, v.second, 1);
for (; qr[k].r == i && k <= q; ++k)
as[qr[k].id] += qry(1, 1, n, qr[k].l, qr[k].r);
}
sort(qr + 1, qr + q + 1, cmp2);
build(1, 1, n);
k = 1;
for (int i = n; i >= 1; --i) {
for (auto v : d2[i]) upd(1, 1, n, v.first, v.second, 1);
for (; qr[k].l == i && k <= q; ++k)
as[qr[k].id] += qry(1, 1, n, qr[k].l, qr[k].r);
}
for (int i = 1; i <= q; ++i) write(as[i]), he;
return 0;
}
边栏推荐
- ES6 Set与Map是什么,如何使用
- 打破原则引入SQL,MongoDB到底想要干啥???
- AT4108 [ARC094D] Normalization
- Markdown 3 - 流程图表
- libudev manual
- The way of programmers' cultivation: do one's own responsibilities, be clear in reality - lead to the highest realm of pragmatism
- 20220729 Securities, Finance
- Jenkins自动化部署项目
- qq udp tcp机制
- SQL 26 calculation under 25 years of age or older and the number of users
猜你喜欢
Jenkins自动化部署项目
第十五天笔记
svg波浪动画js特效代码
自从外包干了四年,基本废了...
戴墨镜的卡通太阳SVG动画js特效
el-table中el-table-column下的操作切换class样式
电池包托盘有进水风险,存在安全隐患,紧急召回52928辆唐DM
SyntaxError: EOL while scanning string literal
The way of programmers' cultivation: do one's own responsibilities, be clear in reality - lead to the highest realm of pragmatism
OFDM 十六讲 3- OFDM Waveforms
随机推荐
matlab画图,仅显示部分图例
Markdown 3 - 流程图表
腾讯称电竞人才缺口200万;华为鸿蒙3.0正式发布;乐视推行每周工作4天半?...丨黑马头条...
【软考软件评测师】自动化测试章节下篇
el-table中el-table-column下的操作切换class样式
树形dp小总结(换根,基环树,杂七杂八的dp)
R语言使用方差分析ANOVA比较回归模型的差异、anova函数比较两个模型并报告它们是否存在显著差异(两个模型的数据相同,一个模型使用的预测特征包含另外一个模型的特征)
strlen跟sizeof区别
Mac Brew 安装PHP
OFDM 十六讲 3- OFDM Waveforms
UPC2022暑期个人训练赛第19场(B,P)
深度操作系统DeepinOS安装步骤和MySQL安装测试
【23考研】408代码题参考模板——链表
电池包托盘有进水风险,存在安全隐患,紧急召回52928辆唐DM
【软考软件评测师】基于规则说明的测试技术上篇
What are the hard-core upgrades and applications that cannot be missed in Greenplum 6.0?
机器学习——特征选择
shell script flow control statement
Parallelized Quick Sort Ideas
What is the level of Ali P7?