题目:
poj1990
题意:
计算每两头牛的噪音的和。
两头牛的噪音 = max(牛1噪音,牛2噪音) * 两头牛的距离差
思路:
用两个树状数组,一个记录牛的总数,另一个记录牛的总距离。
把牛按 v 值从小到大排序(v相等时,x不影响噪音总和)
再遍历数组,
计算在点 x 的左边有多少头牛,右边有多少头牛,左边的总距离差,右边的总距离差,
ans += v * (左边的总距离差 + 右边的总距离差),
而左边的总距离差 = x * 左边有多少头牛 - 左边的总距离(1 ~ x 内牛的总距离),
右边的总距离差同理。
再逐个加入树状数组。
ps:ans 会爆 int
题目:
hdu3015
题意:
poj1990的换皮,两棵树的不和谐价值 = min(树1高度,树2高度)* 两棵树的距离差
思路:
poj1990的思路 + 离散化(如果用二分查找来计算 x 和 h 的话会TLE)
poj 1990 代码:
#include <iostream>
#include <algorithm>
#define ll long long
#define lowbit(a) ((a) & (-a))
using namespace std;
const int MAXN = 2e4 + 10;
struct node{
int v, x;
friend bool operator < (const node &a, const node &b){
return a.v < b.v;
}
}cow[MAXN];
int tree_cnt[MAXN]; //记录区间 1 ~ x 牛的总数
int tree_dis[MAXN]; //记录区间 1 ~ x 内的牛到 x 的总距离
int final;
void add(int *p, int x, int d){
while (x <= final){
p[x] += d;
x += lowbit(x);
}
}
int sum(int *p, int x){
int cnt = 0;
while (x > 0){
cnt += p[x];
x -= lowbit(x);
}
return cnt;
}
int main(){
// freopen("_in.txt", "r", stdin);
// freopen("_out1.txt", "w", stdout);
int n;
ll l_cnt, r_cnt, l_dis, r_dis, ans = 0; //在点 x 的左边有多少头牛,右边有多少头牛,左边的总距离差,右边的总距离差
scanf("%d", &n);
for (int i = 0; i < n; i++){
scanf("%d%d", &cow[i].v, &cow[i].x);
final = max(final, cow[i].x);
}
sort(cow, cow+n);
for (int i = 0; i < n; i++){
l_cnt = sum(tree_cnt, cow[i].x);
r_cnt = sum(tree_cnt, final) - l_cnt;
l_dis = cow[i].x * l_cnt - sum(tree_dis, cow[i].x);
r_dis = sum(tree_dis, final) - sum(tree_dis, cow[i].x) - cow[i].x * r_cnt;
ans = ans + cow[i].v * (l_dis + r_dis); //ans += v * (左边的总距离差 + 右边的总距离差)
add(tree_cnt, cow[i].x, 1);
add(tree_dis, cow[i].x, cow[i].x);
}
printf("%lld", ans);
return 0;
}
hdu 3015 代码
#include <iostream>
#include <algorithm>
#include <cstring>
#define ll long long
#define lowbit(a) ((a) & (-a))
using namespace std;
const int MAXN = 1e5 + 10;
struct node{
int h, x;
}t[MAXN];
ll tree_cnt[MAXN]; //记录区间 1 ~ x 树的总数
ll tree_dis[MAXN]; //记录区间 1 ~ x 内的树到 x 的总距离
int final;
int cmp0 (const node &a, const node &b){
return a.x < b.x;
}
int cmp1 (const node &a, const node &b){
return a.h > b.h;
}
void add(ll *p, int x, int d){
while (x <= final){
p[x] += d;
x = x + lowbit(x);
}
}
ll sum(ll *p, int x){
ll cnt = 0;
while (x > 0){
cnt += p[x];
x -= lowbit(x);
}
return cnt;
}
int main(){
// freopen("_in.txt", "r", stdin);
// freopen("_out1.txt", "w", stdout);
int n;
ll l_cnt, r_cnt, l_dis, r_dis, ans; //在点 x 的左边(包括点 x 的位置)有多少头树,右边有多少头树,左边的总距离差,右边的总距离差
int temp;
while (~scanf("%d", &n)){
for (int i = 0; i < n; i++)
scanf("%d%d", &t[i].x, &t[i].h);
//离散化 x
sort(t, t+n, cmp0);
temp = t[0].x;
t[0].x = 1;
for (int i = 1; i < n; i++)
if (t[i].x == temp)
t[i].x = t[i-1].x;
else{
temp = t[i].x;
t[i].x = i + 1;
}
final = t[n-1].x;
//离散化 h
sort(t, t+n, cmp1);
temp = t[n-1].h;
t[n-1].h = 1;
for (int i = 1; i < n; i++)
if (t[n-1-i].h == temp)
t[n-1-i].h = t[n-i].h;
else{
temp = t[n-1-i].h;
t[n-1-i].h = i + 1;
}
ans = 0;
memset(tree_cnt, 0, sizeof(ll) * (final + 5));
memset(tree_dis, 0, sizeof(ll) * (final + 5));
for (int i = 0; i < n; i++){
l_cnt = sum(tree_cnt, t[i].x); //如果有树再点 x上,那么 F * S = abs(x-x) * h = 0 * h = 0 ,没有影响
r_cnt = sum(tree_cnt, final) - l_cnt;
l_dis = t[i].x * l_cnt - sum(tree_dis, t[i].x);
r_dis = sum(tree_dis, final) - sum(tree_dis, t[i].x) - t[i].x * r_cnt;
ans = ans + t[i].h * (l_dis + r_dis); //ans += h * (左边的总距离差 + 右边的总距离差)
add(tree_cnt, t[i].x, 1);
add(tree_dis, t[i].x, t[i].x);
}
printf("%lld\n", ans);
}
return 0;
}