A题 楼兰图腾

链接:https://ac.nowcoder.com/acm/contest/1032/A

树状数组 + 逆序对

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) (x & -x)
typedef long long ll;
const int maxn = 2e5 + 10;
int a[maxn], c[maxn], l[maxn], r[maxn], n;
void update(int p, int val) {
while (p <= n) {
c[p] += val;
p += lowbit(p);
}
} int ask(int x) {
int res = 0;
while (x > 0) {
res += c[x];
x -= lowbit(x);
}
return res;
} int main() {
//freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false), cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
//求正序,前面有几个比a[i]大
for (int i = 1; i <= n; i++) {
update(a[i], 1);
l[i] = ask(a[i] - 1);
}
memset(c, 0, sizeof c);
//树状数组清零求逆序,后面有几个比a[i]大
for (int i = n; i >= 1; i--) {
update(a[i], 1);
r[i] = ask(a[i] - 1);
}
ll ans = 0;
//依次枚举每个点作为中间点,以该点位中心的 ‘v’ 个数显然是 left[i] * right[i]
for (int i = 2; i <= n - 1; i++) {
ans += 1ll * (i - l[i] - 1) * (n - i - r[i]);
}
cout << ans << " ";
ans = 0;
//同理枚举 '^'的个数
for (int i = 2; i <= n - 1; i++) {
ans += 1ll * l[i] * r[i];
}
cout << ans << endl;
return 0;
}

B题 A Tiny Problem with intergers

链接:https://ac.nowcoder.com/acm/contest/1032/B

树状数组 区间修改 + 单点查询

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) (x & -x)
typedef long long ll;
const int maxn = 1e5 + 10;
int tr[maxn], n, q, a, pre;
void add(int i, int v) {
while (i <= n) {
tr[i] += v;
i += lowbit(i);
}
}
int getsum(int x) {
int res = 0;
while (x) {
res += tr[x];
x -= lowbit(x);
}
return res;
}
int main() {
//freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; ++i)cin >> a, add(i, a - pre), pre = a;
char c; int u, v, a;
while (q--) {
cin >> c >> u;
if (c == 'C') {
cin >> v >> a; add(u, a), add(v + 1, - a);
}
else
cout << getsum(u) << endl;
}
}

C题 A Simple Problem with Integers

链接:https://ac.nowcoder.com/acm/contest/1032/C

树状数组 区间修改 + 区间查询 / (线段树 or 分块)

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) (x & -x)
typedef long long ll;
const int maxn = 1e5 + 10;
ll n, q, tr1[maxn], tr2[maxn], a, pre;
void add(int x, int v) {
for (int i = x; i <= n; i += lowbit(i)) tr1[i] += v, tr2[i] += 1ll * v * (x - 1);
}
ll getsum(int x) {
long long sum = 0;
for (int i = x; i; i -= lowbit(i)) sum += tr1[i] * x - tr2[i];
return sum;
}
int main() {
//freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> a, add(i, a - pre), pre = a;
char c; int u, v, a;
while (q--) {
cin >> c >> u >> v;
if (c == 'C') {
cin >> a; add(u, a), add(v + 1, - a);
}
else
cout << getsum(v) - getsum(u - 1) << endl;
}
}

D题 Lost Cows

链接:https://ac.nowcoder.com/acm/contest/1032/D

树状数组 + 二分(or倍增)

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
long long tr[maxn];
void add(int x, int v){
if (x == 0) return;
while (x < maxn) {
tr[x] += v;
x += x & -x;
}
}
long long query(int x){
long long rec = 0;
while (x) {
rec += tr[x];
x -= x & -x;
}
return rec;
}
int a[maxn], ans[maxn];
int main(){
freopen("in.txt", "r", stdin);
int n; scanf("%d", &n);
for (int i = 2; i <= n; i++)
scanf("%d", &a[i]);
//二分,查询前mid个数有多少1比较,更新区间
for (int i = n; i; i--) {
int l = 1, r = n + 1, t;
while (l <= r) {
int mid = l + r >> 1;
if (query(mid) + a[i] < mid) t = mid, r = mid - 1;
else l = mid + 1;
}
ans[i] = t;
add(t, 1);
}
for (int i = 1; i <= n; i++)
printf("%d\n", ans[i]);
}

0x42 数据结构进阶-树状数组的更多相关文章

  1. Codeforces Round #248 (Div. 2) B称号 【数据结构:树状数组】

    主题链接:http://codeforces.com/contest/433/problem/B 题目大意:给n(1 ≤ n ≤ 105)个数据(1 ≤ vi ≤ 109),当中有m(1 ≤ m ≤  ...

  2. [ACM_数据结构] POJ2352 [树状数组稍微变形]

    Description Astronomers often examine star maps where stars are represented by points on a plane and ...

  3. 数据结构(树状数组):HEOI2012 采花

    [题目描述] 萧薰儿是古国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一排的,以便 ...

  4. 【poj 3167】Cow Patterns(字符串--KMP匹配+数据结构--树状数组)

    题意:给2个数字序列 a 和 b ,问按从小到达排序后,a中的哪些子串与b的名次匹配. a 的长度 N≤100,000,b的长度 M≤25,000,数字的大小 K≤25. 解法:[思考]1.X 暴力. ...

  5. 牛客练习赛22-E.简单数据结构1(扩展欧拉定理降幂 +树状数组)

    链接:E.简单数据结构1 题意: 给一个长为n的序列,m次操作,每次操作: 1.区间加 2.对于区间,查询 ,一直到- 请注意每次的模数不同.   题解:扩展欧拉定理降幂 对一个数p取log(p)次的 ...

  6. ACM数据结构-树状数组

    模板: int n; int tree[LEN]; int lowbit(int x){ return x&-x; } void update(int i,int d){//index,del ...

  7. NOIp 数据结构专题总结 (2):分块、树状数组、线段树

    系列索引: NOIp 数据结构专题总结 (1) NOIp 数据结构专题总结 (2) 分块 阅:<「分块」数列分块入门 1-9 by hzwer> 树状数组 Binary Indexed T ...

  8. 【洛谷 p3374】模板-树状数组 1(数据结构)

    题目:已知一个数列,你需要进行下面两种操作:1.将某一个数加上x:2.求出某区间每一个数的和. 解法:树状数组求前缀和. #include<cstdio> #include<cstd ...

  9. 【洛谷 p3368】模板-树状数组 2(数据结构)

    题目:已知一个数列,你需要进行下面两种操作:1.将某区间每一个数数加上x:2.求出某一个数的和. 解法:树状数组+前缀和优化.数组中每位存和前一位的数的差,这样区间修改只用改两位,单点询问就是求前缀和 ...

  10. 数据结构--树状数组(黑龙江省第八届大学生程序设计竞赛--post office)

    例题来源: 题目: 1468: Post office 题目描述 There are N(N<=1000) villages along a straight road, numbered fr ...

随机推荐

  1. Go切片是值传递还是引用传递?

    Go没有引用传递和引用类型!!! 很多人有个误区,认为涉及Go切片的参数是引用传递,或者经常听到Go切片是引用类型这种说法,今天我们就来说一下方面的问题. 什么是值传递? 将实参的值传递给形参,形参是 ...

  2. 【UniApp】-uni-app-OptionAPI应用生命周期和页面生命周期

    前言 好,经过上个章节的介绍完毕之后,了解了一下 uni-app-修改组件主题和样式 那么了解完了uni-app-修改组件主题和样式之后,这篇文章来给大家介绍一下 uni-app-OptionAPI应 ...

  3. [ABC270Ex] add 1

    Problem Statement You are given a tuple of $N$ non-negative integers $A=(A_1,A_2,\ldots,A_N)$ such t ...

  4. Winform 好看控件库推荐:MaterialSkin.2

    MaterialSkin.2 控件包是在 MaterialSkin 及基础上二次开发而来的,在原控件基础上修复了一些Bug,丰富了主题以及动画效果,效果非常好. MaterialSkin.2 现在处于 ...

  5. K8s 里多容器 Pod 的健康检查探针工作机制分析

    目录 1. 开篇 2. 聊啥 3. 结论(TL;DR) 4. 测试过程 4.1 准备测试用镜像 4.2 准备 Deployment YAML 4.3 准备 Service YAML 4.4 准备第二个 ...

  6. Celery周期性任务定义beat

    通过celery beat可以使用周期性任务的定义. https://docs.celeryq.dev/en/stable/userguide/periodic-tasks.html 周期性任务bea ...

  7. 腾讯云服务器安装MySQL5.7基于xshell

    腾讯云服务器安装MySQL5.7基于xshell 下载MySQL57安装包 wget https://dev.mysql.com/get/mysql57-community-release-el7-1 ...

  8. 从零玩转Websocket实时通讯服务之前后端分离版本-websocket

    title: 从零玩转Websocket实时通讯服务之前后端分离版本 date: 2021-10-25 00:47:12.945 updated: 2021-12-26 17:43:10.496 ur ...

  9. Python笔记三之闭包与装饰器

    本文首发于公众号:Hunter后端 原文链接:Python笔记三之闭包与装饰器 这一篇笔记介绍 Python 里面的装饰器. 在介绍装饰器前,首先提出这样一个需求,我想统计某个函数的执行时间,假设这个 ...

  10. 创建傀儡进程svchost.exe并注入DLL文件(Shellcode)

    本文主要利用 SetThreadContext 修改进程中的线程上下文来实现Dll注入(ShellCode). 实现原理 首先,使用 CreateProcess 函数创建svchost.exe进程,并 ...