UVALive 4329 Ping pong(树状数组求逆序数+顺序数)
来源:互联网 发布:数据错误 循环冗余检查 编辑:程序博客网 时间:2024/06/08 14:00
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2330
题意:对于给定的一个长度为n序列a,对于每个位置i,若左边存在一个数a[l],右边存在一个数a[r],满足a[l] < a[i] < a[r]或a[l] > a[i] > a[r],则(l, r)构成一对,求总共有多少对。
思路:虽然先学的是线段树,但是这类问题也可以用树状数组解决。考虑2个数组c和d,对于位置i,c[i]表示位置i左边有c[i]个数比它小,d[i]表示位置i右边有d[i]个数比它小。则最后的结果就是
代码:
#include <stdio.h>#include <iostream>#include <string.h>#include <math.h>using namespace std;const int N = 2e4 + 10;const int M = 1e5 + 10;int s[M << 2];int c[M], d[M];int a[N];int n;int lowbit(int x) { return x & (-x);}void add(int x) { while (x <= M) { s[x]++; x += lowbit(x); }}int sum(int x) { int res = 0; while (x > 0) { res += s[x]; x -= lowbit(x); } return res;}int main() { int t_case; scanf("%d", &t_case); for (int i_case = 1; i_case <= t_case; i_case++) { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); memset(s, 0, sizeof(s)); for (int i = 1; i <= n; i++) { c[i] = sum(a[i] - 1); add(a[i]); } memset(s, 0, sizeof(s)); for (int i = n; i >= 1; i--) { d[i] = sum(a[i] - 1); add(a[i]); } long long res = 0; for (int i = 2; i < n; i++) res += 1LL * c[i] * (n - i - d[i]) + 1LL * (i - 1 - c[i]) * d[i]; printf("%lld\n", res); } return 0;}
0 0
- UVALive 4329 Ping pong(树状数组求逆序数+顺序数)
- HDU 2492 Ping pong 树状数组求逆序数
- HDU2492 Ping pong 树状数组求逆序数
- hdu 2492 Ping pong 树状数组 求逆序数
- POJ 3928 & HDU 2492 Ping pong(树状数组求逆序数)
- UVALive - 4329 Ping pong 数状数组
- UVALive - 4329 Ping pong (树状数组)
- UVALIVE 4329 Ping pong(树状数组)
- UVALive 4329Ping pong(树状数组)
- uvalive 4329 Ping Pong(树状数组)
- UVALive 6508(树状数组求逆序数)
- uvalive 4329 Ping pong---树状数组
- 4329 - Ping pong uvalive+树状数组
- uvalive 4329 Ping pong 树状数组
- uvalive 4329 Ping pong (树状数组)
- UVALive 4329--Ping pong+树状数组
- UVALive - 4329 Ping pong (树状数组)
- UVALive 4329 Ping pong(树状数组)
- scrollview和listview共存
- C#语音SDKSDK接口开发经验及具体开发实现
- Show full directory path in MAC
- 使用第三方的短信验证码
- iOS7 监听耳机拔出
- UVALive 4329 Ping pong(树状数组求逆序数+顺序数)
- 将Java对象转换成json字符串,然后客户端接收到字符串后处理变成Javascript对象加载到下拉列框。
- xcode7+ios9 访问网络提示错误解决办法
- 类加载器详解
- 使用JavaScript实现回到顶部功能
- Fregment切换动画
- jQuery 笔记
- iOS学习之iOS沙盒(sandbox)机制和文件操作(一)
- Android中添加常驻通知栏