HDU - 5877 Weak Pair(dfs序+树状数组)

A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.
Notice that the square root operation should be rounded down to integer.
Input
The input contains several test cases, terminated by EOF.
For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 263.
The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.
Output
For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.

  • 题意:一颗树上,有n个节点,给出每个节点的权值。另外给出一个值k,问有多少对节点满足:
    a[u]×a[v]≤k,u是v节点的祖先(u≠v)。
  • 思路:我们要求所有弱路径,我们可以DFS遍历,遍历到一个点,就看看线段树里面符合a[u]<=k/a[v]的点的个数有多少。查询完之后再把这个点加进去。然后DFS后把加进去的重新减掉,遍历其他分支。

参考题解

#include <map>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define LowBit(x)   (x&(-x))
typedef long long LL;
const int maxn = 1e5+5;
int sum[maxn<<2], point_weight[maxn], book[maxn];
vector<int> tree[maxn], vec;
map<int, int> vis;
int cnt;
LL n, k, ans = 0;

void update(int pos, int val)
{
    while(pos < maxn)
    {
        sum[pos] += val;
        pos += LowBit(pos);
    }
}

int GetSum(int pos)
{
    int result = 0;
    while(pos > 0)
    {
        result += sum[pos];
        pos -= LowBit(pos);
    }
    return result;
}

void DFS(int root)
{
    int len = tree[root].size();
    int pos = upper_bound(vec.begin(), vec.end(), k/point_weight[root])-vec.begin();
    int pos_this = upper_bound(vec.begin(), vec.end(), point_weight[root])-vec.begin();
    ans += GetSum(pos);
    update(pos_this, 1);
    for(int i = 0; i < len; i++)
        DFS(tree[root][i]);
    update(pos_this, -1);
}

inline void init()
{
    cnt = 1;
    ans = 0;
    vis.clear();
    vec.clear();
    for(int i = 0; i < maxn; i++)
    {
        tree[i].clear();
        book[i] = sum[i] = 0;
    }
}

int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        init();
        scanf("%lld%lld", &n, &k);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &point_weight[i]);
            if(vis[point_weight[i]])    continue ;
            vec.push_back(point_weight[i]);
            vis[point_weight[i]] = 1;
        }
        sort(vec.begin(), vec.end());
        for(int i = 1; i < n; i++)
        {
            int parent, child;
            scanf("%d%d", &parent, &child);
            tree[parent].push_back(child);
            book[child] = 1;
        }
        for(int i = 1; i <= n; i++) //所有的孩子节点都跳过,只对根进行DFS
        {
            if(book[i]) continue ;
            DFS(i);
            break ;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40788897/article/details/100077144
今日推荐