当前位置: 首页 > news >正文

AT_abc442_d 学习笔记

大冤种。

第一眼拿到题目:这不一眼前缀和吗?

结果没看时限&没分析时间复杂度的我:前缀和肯定会 T 飞,需要优化。

怎么优化呢?这不一眼线段树吗,于是我写了 \(100\) 多行线段树。

这里的 \(1\) 操作是让我们交换 \(a_x\)\(a_{x+1}\) 的值,那么这里的 update 函数我们可以这么写:

void update(ll d, ll pl, ll pr, ll p){if (pl == pr){tree[p] = a[pl];return;}pushdown(p, pl, pr);ll mid = (pl + pr) >> 1;if (d <= mid)if (d + 1 <= mid)//都在左子树update(d, pl, mid, ls(p));else //一左一右update(d, pl, mid, ls(p)), update(d + 1, mid + 1, pr, rs(p));else //都在右子树update(d, mid + 1, pr, rs(p));pushup(p);
}

\(2\) 操作就是个区间和(前缀和)板子,不多讲了,直接上冤种代码。

/*********************************************************** Author        : dingziyang888* Website       : https://www.luogu.com.cn/problem/* Created Time  :* FileName      :* Warning!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!* 1.MLE?* 2.array size enough?* 3.long long?* 4.overflow long long?* 5.multiple task cleaned?* 6.freopen?* 7.TLE?* *******************************************************/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <climits>
#define DEBUG
#define Ofile(s) freopen(s".in", "r", stdin), freopen (s".out", "w", stdout)
#define Cfile(s) fclose(stdin), fclose(stdout)
#define fast ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;using ll = long long;
using ull = unsigned long long;
using lb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;constexpr int mod = 998244353;
constexpr int maxk = 200005;ll n, q, L, R, x, opt;ll a[maxk], tree[maxk << 2], tag[maxk << 2];ll ls(ll p){return p << 1;
}ll rs(ll p){return p << 1 | 1;
}void pushup(int p){tree[p] = tree[ls(p)] + tree[rs(p)];
}void build(ll p, ll pl, ll pr){tag[p] = 0;if (pl == pr){tree[p] = a[pl];return;}ll mid = (pl + pr) >> 1;build(ls(p), pl, mid);build(rs(p), mid + 1, pr);pushup(p);
}void addtag(ll p, ll pl, ll pr, ll d){tag[p] += d;tree[p] += d * (pr - pl + 1);
}void pushdown(ll p, ll pl, ll pr){if (tag[p]){ll mid = (pl + pr) >> 1;addtag(ls(p), pl, mid, tag[p]);addtag(rs(p), mid + 1, pr, tag[p]);tag[p] = 0;}
}ll query(ll L, ll R, ll p, ll pl, ll pr){if (L <= pl && pr <= R)return tree[p];pushdown(p, pl, pr);ll mid = (pl + pr) >> 1, res = 0;if (L <= mid)res += query(L, R, ls(p), pl, mid);if (mid < R)res += query(L, R, rs(p), mid + 1, pr);return res;
}void update(ll d, ll pl, ll pr, ll p){if (pl == pr){tree[p] = a[pl];return;}pushdown(p, pl, pr);ll mid = (pl + pr) >> 1;if (d <= mid)if (d + 1 <= mid)update(d, pl, mid, ls(p));else update(d, pl, mid, ls(p)), update(d + 1, mid + 1, pr, rs(p));else update(d, mid + 1, pr, rs(p));pushup(p);
}int main() {fast;cin >> n >> q;for (int i = 1; i <= n; i++)cin >> a[i];build(1, 1, n);while (q--){cin >> opt;if (opt == 1){cin >> x;swap(a[x], a[x + 1]);update(x, 1, n, 1);update(x + 1, 1, n, 1);}else {cin >> L >> R;cout << query(L, R, 1, 1, n) << endl;}}return 0;
}
http://www.jsqmd.com/news/334260/

相关文章:

  • 建议收藏:大模型技术全景图:概念、训练与应用全解析
  • 2026年五大导视系统标识标牌设计制作公司口碑推荐及解析 - 深度智识库
  • 用HTTPX + Pytest + Pydantic + 契约测试做接口自动化
  • 实用指南:网络安全培训
  • k6是什么
  • 性能测试工具的原理与架构解析
  • Playwright + Pytest + Allure的组合做web ui测试
  • 小白也能看懂系列——安全编码(2)
  • P14170 二分图最大匹配期望 学习笔记
  • Selenium + Pytest + Allure的组合做web ui测试
  • 2026年高端全屋定制厂家推荐,不容错过的五大品牌 - 睿易优选
  • 基于PLC工厂的锅炉水位自动控制系统的设计与实现
  • 2026最新陕西婚恋平台五大甄选:深耕本土精准脱单,红娘沈大妈 Real 真心婚恋领跑西北 - 深度智识库
  • Cypress
  • Linux自学教材02
  • Claude Code | Rules 最佳配置案例(中文)
  • Oracle数据库操作基础2
  • 2026年版|大模型企业运营落地全流程(小白/程序员必收藏,从入门到进阶)
  • 基于PLC的电梯控制系统的设计
  • 复现论文《Fair Semi-distributed Resource Allocation Scheme over Relay-Enhanced OFDMA Networks》的代码实现
  • 20260202
  • 收藏!一文掌握大语言模型原理及其医疗领域应用挑战
  • 【收藏备用|2026年版】未来10年,什么领域的职业发展潜力最大?
  • https://blog.csdn.net/2401_84760322/article/details/149808483?spm=1001.2014.3001.5502
  • 基于逆变器风电和储能设备的过电流继电器最优协调研究复现
  • 基于PLC的钢板定长剪切自动控制系统设计
  • 基于Java的旅游资源网站平台设计与实现(11874)
  • 【5G通信】基于matlab 5G毫米波UDN中带有位置感知波束成形的链路级模型干涉评估【含Matlab源码 15044期】
  • 基于PLC的风电控制系统
  • 基于Java的商店会员系统(11875)