树状数组 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 
#include 
#include 
#include 
#include 
#include 
 
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; ivalue[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;
}


你可能感兴趣的:(模板,小结,树状数组,离线查询)