poj 1990 MooFest(树状数组)
2016-10-19 23:59
447 查看
题意:N头奶牛每头耳背程度v,坐标x。两牛交流需要音量为distance * max(v(i),v(j)),求
所有牛两两交流所需总和?
思路:对耳背进行升序,一头一头的加入牛,这样加入每一头的时候,这头牛的耳背程度
肯定是最大的。开两个树状数组,分别记录距离和牛数量。
每次分别查询左右两边的牛数量和距离和。
代码:
G - MooFest
Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%lld
& %llu
Submit Status
Description
Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a social gathering of cows from around the world. MooFest involves a variety of events including haybale stacking, fence jumping, pin the tail on the farmer, and of course, mooing. When the
cows all stand in line for a particular event, they moo so loudly that the roar is practically deafening. After participating in this event year after year, some of the cows have in fact lost a bit of their hearing.
Each cow i has an associated "hearing" threshold v(i) (in the range 1..20,000). If a cow moos to cow i, she must use a volume of at least v(i) times the distance between the two cows in order to be heard by cow i. If two cows i and j wish to converse, they
must speak at a volume level equal to the distance between them times max(v(i),v(j)).
Suppose each of the N cows is standing in a straight line (each cow at some unique x coordinate in the range 1..20,000), and every pair of cows is carrying on a conversation using the smallest possible volume.
Compute the sum of all the volumes produced by all N(N-1)/2 pairs of mooing cows.
Input
* Line 1: A single integer, N
* Lines 2..N+1: Two integers: the volume threshold and x coordinate for a cow. Line 2 represents the first cow; line 3 represents the second cow; and so on. No two cows will stand at the same location.
Output
* Line 1: A single line with a single integer that is the sum of all the volumes of the conversing cows.
Sample Input
Sample Output
所有牛两两交流所需总和?
思路:对耳背进行升序,一头一头的加入牛,这样加入每一头的时候,这头牛的耳背程度
肯定是最大的。开两个树状数组,分别记录距离和牛数量。
每次分别查询左右两边的牛数量和距离和。
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; const int maxn = 2*1e5+5; ll tree_dis[maxn], tree_num[maxn]; struct node { ll d, v; bool operator < (const node &a) const { return v < a.v; } }a[maxn]; int lowbit(int x) { return x&(-x); } ll query(ll *tree, ll pos) { ll sum = 0; while(pos) { sum += tree[pos]; pos -= lowbit(pos); } return sum; } void update(ll *tree, ll pos, ll val) { while(pos < maxn) { tree[pos] += val; pos += lowbit(pos); } } ll sum(ll *tree, ll from, ll to) { return query(tree, to-1) - query(tree, from-1); } int main(void) { int n; while(cin >> n) { memset(tree_num, 0, sizeof(tree_num)); memset(tree_dis, 0, sizeof(tree_dis)); for(int i = 0; i < n; i++) scanf("%lld%lld", &a[i].v, &a[i].d); sort(a, a+n); ll res = 0; for(int i = 0; i < n; i++) { ll v = a[i].v, d = a[i].d; ll l = sum(tree_num, 1, d); ll r = sum(tree_num, d+1, maxn); res += v*((l*d-sum(tree_dis, 1, d))+(sum(tree_dis, d+1, maxn)-r*d)); update(tree_dis, a[i].d, a[i].d); update(tree_num, a[i].d, 1); } printf("%lld\n", res); } return 0; }
G - MooFest
Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%lld
& %llu
Submit Status
Description
Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a social gathering of cows from around the world. MooFest involves a variety of events including haybale stacking, fence jumping, pin the tail on the farmer, and of course, mooing. When the
cows all stand in line for a particular event, they moo so loudly that the roar is practically deafening. After participating in this event year after year, some of the cows have in fact lost a bit of their hearing.
Each cow i has an associated "hearing" threshold v(i) (in the range 1..20,000). If a cow moos to cow i, she must use a volume of at least v(i) times the distance between the two cows in order to be heard by cow i. If two cows i and j wish to converse, they
must speak at a volume level equal to the distance between them times max(v(i),v(j)).
Suppose each of the N cows is standing in a straight line (each cow at some unique x coordinate in the range 1..20,000), and every pair of cows is carrying on a conversation using the smallest possible volume.
Compute the sum of all the volumes produced by all N(N-1)/2 pairs of mooing cows.
Input
* Line 1: A single integer, N
* Lines 2..N+1: Two integers: the volume threshold and x coordinate for a cow. Line 2 represents the first cow; line 3 represents the second cow; and so on. No two cows will stand at the same location.
Output
* Line 1: A single line with a single integer that is the sum of all the volumes of the conversing cows.
Sample Input
4 3 1 2 5 2 6 4 3
Sample Output
57
相关文章推荐
- [树状数组] poj 1990 MooFest
- POJ - 1990 MooFest (树状数组)
- POJ 1990 MooFest【 树状数组 】
- POJ 1990 MooFest(树状数组)
- POJ 1990 MooFest(树状数组)
- POJ 1990 MooFest(树状数组)
- POJ 1990 MooFest(树状数组)
- poj1990 MooFest && hdu3015 Disharmony Trees (树状数组)
- POJ 1990 MooFest 树状数组
- poj 1990 MooFest(树状数组)
- POJ 1990 MooFest (树状数组)
- POJ 1990——MooFest(2个树状数组)
- poj 1990 MooFest (树状数组)
- POJ 1990 MooFest(树状数组)
- POJ 1990 MooFest(树状数组)
- POJ 1990 MooFest (树状数组)
- poj 1990 MooFest (树状数组)
- POJ 1990 MooFest(想法题&树状数组)
- POJ 1990 MooFest 【树状数组】
- 树状数组 公式推导 poj 1990 MooFest