树状数组 boj 399. Who Is Joyful boj 395. Tree

下面是树状数组一维和二维的模板链接

点击打开链接

代表例题1  boj 399 Who Is Joyful 

时间限制 3000 ms  内存限制 65536 KB

题目描述

There are several little buddies standing in a line. We say someone is a joyful little buddy, if there exists a little buddy whose height is smaller than him in the front of him and there exists a little buddy whose height is bigger than him in the back of him. Now there are N little buddies, numbered from 1 to N. Their heights h are known. Give you two numbers A and B. Query that in the closed interval whose two ends are A and B, if the joyfulness of the little buddies in the interval have nothing to do with the little buddies out of the interval, how many little buddies are joyful in total in the interval.

输入格式

In the first line there is an integer T (T<=50), indicates the number of test cases. For each case, the first line contains one integer N (1<=N<=1e5). The second line contains N integers hi (1<=hi<=1e9, 1<=i<=N), indicate the heights of the little buddies from the beginning to the end. The third line contains an integer Q (1<=Q<=1e5), indicates the number of queries. And in the following Q lines, each line contains two integers A and B (1<=A, B<=N).

输出格式

For each case, output the case number as shown, and then print the answers to the queries, every query in a single line.

输入样例

2
6
5 2 3 1 4 5
1
2 5
8
1 4 5 5 1 1 3 2
2
3 6
1 3

输出样例

Case 1:
1
Case 2:
0
1
这题直接学习了C学长的代码,和smile同学的解析与代码,这题非常的复杂,结合了离线查询,单调队列,以及树状数组

推荐看smile的代码,讲解非常详细


代表例题二  

时间限制 7000 ms  内存限制 65536 KB

题目描述

Given a rooted tree with values assigned on each node, you're required to answer how many nodes on the path from the root to  u , that have a value strictly greater than  value[u] . Node 1 is the root.

输入格式

An integer  T(T20)  will exist in the first line, indicating the number of test cases. For each test case, the first line will be the number of tree nodes  N(1N100000) . The following  (N1)  lines, each with a pair of integers  (u,v) , describe the edges of the tree. The values of nodes  value[i](1value[i]109,1iN)  will be put down on the next line.

输出格式

Output the answer for each node from node  1  to node  N , one per line.

输入样例

1
4
1 2
1 3
4 2
4 1 5 0

输出样例

0
1
0 
2
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <algorithm>
#include <cstring>
 
using namespace std;
struct
{
    int to, next;
}a[200005];
 
int head[100005];
int n;
int tot;
int c[100005];
bool vis[100005];
int ant[100005];
int maxn;
struct data
{
    int key, note;
} value[100005];
int mark[100005];
 
bool cmp(data a1, data a2)
{
    return a1.key < a2.key;
}
 
void add(int u, int v)
{
    a[tot].to = v;
    a[tot].next = head[u];
    head[u] = tot++;
}
 
int lowbit(int n)
{
    return n&(-n);
}
 
void update(int pot, int num)
{
    while (pot<=n)
    {
        c[pot] += num;
        pot += lowbit(pot);
    }
}
 
int sum(int pot)
{
    int ans = 0;
    while (pot>0)
    {
        ans += c[pot];
        pot -= lowbit(pot);
    }
    return ans;
}
 
void dfs(int pot)
{
    ant[pot] = maxn - sum(mark[pot]);
    update(mark[pot], 1);
    maxn++;
    vis[pot] = true;
    for (int j=head[pot]; j!=-1; j=a[j].next)
    {
        int vot = a[j].to;
        if (vis[vot]==false)
            dfs(vot);
        else continue;
    }
    update(mark[pot], -1);
    maxn--;
}
 
int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d", &n);
        int u, v;
        tot = 0;
        memset(head, -1, sizeof head);
        for (int i=0; i<n-1; i++)
        {
            scanf("%d %d", &u, &v);
            add(u, v);
            add(v, u);
        }
 
        for (int i=0; i<n; i++)
        {
            scanf("%d", &value[i].key);
            value[i].note = i+1;
            vis[i+1] = false;
        }
        sort(value, value+n, cmp);
        int temp = 1;
        mark[value[0].note] = temp;
        for (int i=1; i<n; i++)
        {
            if (value[i].key>value[i-1].key)
                mark[value[i].note] = ++temp;
            else mark[value[i].note] = temp;
        }
        for (int i=0; i<=temp; i++)
            c[i] = 0;
        maxn = 0;
        dfs(1);
        for (int i=1; i<=n; i++)
        {
            printf("%d\n", ant[i]);
        }
 
 
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值