HDU 2492 (树状数组)
2016-04-16 02:24
316 查看
题目链接:点击打开链接
题意:求出数列递增或者递减的三元组个数。
用树状数组维护每个数左边和右边有几个数比自己大/小即可。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
#define maxn 21111
struct node {
int num;
int pos;
} p[maxn];
bool cmp1 (const node &a, const node &b) {
return a.num < b.num;
}
bool cmp2 (const node &a, const node &b) {
return a.num > b.num;
}
int l[maxn], r[maxn];//左边比他小的 右边比他小的
int ll[maxn], rr[maxn];//左边比他大的 右边比他大的
int c[maxn], n;
int lowbit (int x) {
return x&(-x);
}
void add (int pos) {
for (int i = pos; i <= n; i += lowbit (i)) {
c[i]++;
}
}
int sum (int pos) {
int ans = 0;
for (int i = pos; i >= 1; i -= lowbit (i)) {
ans += c[i];
}
return ans;
}
int main () {
int t;
scanf ("%d", &t);
while (t--) {
scanf ("%d", &n);
for (int i = 1; i <= n; i++) {
scanf ("%d", &p[i].num);
p[i].pos = i;
}
sort (p+1, p+1+n, cmp1);
memset (c, 0, sizeof c);
for (int i = 1; i <= n; i++) {
l[p[i].pos] = sum (p[i].pos);
r[p[i].pos] = sum (n)-l[p[i].pos];
add (p[i].pos);
}
sort (p+1, p+1+n, cmp2);
memset (c, 0, sizeof c);
for (int i = 1; i <= n; i++) {
ll[p[i].pos] = sum (p[i].pos);
rr[p[i].pos] = sum (n)-ll[p[i].pos];
add (p[i].pos);
}
long long ans = 0;
for (int i = 1; i <= n; i++) {
ans += 1LL*l[i]*rr[i] + 1LL*r[i]*ll[i];
}
cout << ans << endl;
}
return 0;
}
题意:求出数列递增或者递减的三元组个数。
用树状数组维护每个数左边和右边有几个数比自己大/小即可。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
#define maxn 21111
struct node {
int num;
int pos;
} p[maxn];
bool cmp1 (const node &a, const node &b) {
return a.num < b.num;
}
bool cmp2 (const node &a, const node &b) {
return a.num > b.num;
}
int l[maxn], r[maxn];//左边比他小的 右边比他小的
int ll[maxn], rr[maxn];//左边比他大的 右边比他大的
int c[maxn], n;
int lowbit (int x) {
return x&(-x);
}
void add (int pos) {
for (int i = pos; i <= n; i += lowbit (i)) {
c[i]++;
}
}
int sum (int pos) {
int ans = 0;
for (int i = pos; i >= 1; i -= lowbit (i)) {
ans += c[i];
}
return ans;
}
int main () {
int t;
scanf ("%d", &t);
while (t--) {
scanf ("%d", &n);
for (int i = 1; i <= n; i++) {
scanf ("%d", &p[i].num);
p[i].pos = i;
}
sort (p+1, p+1+n, cmp1);
memset (c, 0, sizeof c);
for (int i = 1; i <= n; i++) {
l[p[i].pos] = sum (p[i].pos);
r[p[i].pos] = sum (n)-l[p[i].pos];
add (p[i].pos);
}
sort (p+1, p+1+n, cmp2);
memset (c, 0, sizeof c);
for (int i = 1; i <= n; i++) {
ll[p[i].pos] = sum (p[i].pos);
rr[p[i].pos] = sum (n)-ll[p[i].pos];
add (p[i].pos);
}
long long ans = 0;
for (int i = 1; i <= n; i++) {
ans += 1LL*l[i]*rr[i] + 1LL*r[i]*ll[i];
}
cout << ans << endl;
}
return 0;
}
相关文章推荐
- php中使用exec,system等函数调用系统命令
- 1.闭包
- 179. Largest Number
- 10005---全文检索引擎Solr系列—–全文检索基本原理
- iOS - 仿射变换
- apache benchmark(ab)对Nodejs服压测POST请求时出现: apr_poll: The timeout specified has expired (70007) 问题的解决办法
- GCdepth散点图绘制
- nginx1.8.1(稳定版本) nginx.conf 配置文件详解二
- 【10】mysql编程
- nginx1.8.1(稳定版本) ngixn.conf 配置文件详解一
- C++ 静态数组([])和动态数组(new)的区别
- React入门教程 - 组件生命周期
- MongoDB入门教程二[MongoDB Shell 简介与使用]
- js单元测试sinonjs库
- 前端自动化测试基础-sinon篇章
- Morgan IT 笔试面试汇总
- 沉浸式状态栏设置方法
- 1.四大组件值之Activiyt深度分析
- 关于CSS伪类选择器
- 闭包 什么是闭包