HDU 4358 Boring counting(莫队+DFS序+离散化)

本文介绍了一种结合DFS序和莫队算法解决HDU4358问题的方法,通过对子树进行查询,统计特定权重出现次数为K的情况。

Boring counting

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others)
Total Submission(s): 2811    Accepted Submission(s): 827

Problem Description
In this problem we consider a rooted tree with N vertices. The vertices are numbered from 1 to N, and vertex 1 represents the root. There are integer weights on each vectice. Your task is to answer a list of queries, for each query, please tell us among all the vertices in the subtree rooted at vertice u, how many different kinds of weights appear exactly K times?
 

 

Input
The first line of the input contains an integer T( T<= 5 ), indicating the number of test cases.
For each test case, the first line contains two integers N and K, as described above. ( 1<= N <= 10 5, 1 <= K <= N )
Then come N integers in the second line, they are the weights of vertice 1 to N. ( 0 <= weight <= 10 9 )
For next N-1 lines, each line contains two vertices u and v, which is connected in the tree.
Next line is a integer Q, representing the number of queries. (1 <= Q <= 10 5)
For next Q lines, each with an integer u, as the root of the subtree described above.
 

 

Output
For each test case, output "Case #X:" first, X is the test number. Then output Q lines, each with a number -- the answer to each query.

Seperate each test case with an empty line.
 

 

Sample Input
1
3 1
1 2 2
1 2
1 3
3
2
1
3
 

 

Sample Output
Case #1:
1
1
1

 

题目链接:HDU 4358

把DFS序和莫队算法结合了起来,前两发杯具PE,如果用过DFS序配合线段树的话就大概能知道怎么做了,对于一颗子树的询问显然DFS序是很适合的,然后这样就得到了每一个点所管理的区间[L,R],那莫队移动的时候怎么判断是否遇到了某一个原树上的节点呢?显然用先序遍历的方式来得到某一个点管理的区间,那么L这个点必定是子树树根的位置,若这个点管理的位置是[L,R]那么实际上这个子树根点的值就是arr[L],不过由于每一个区间都是满点的,就映射成val[timeorder]=arr[u],其中u是某次dfs时的起点。

除此之外还要判断当前减掉的数字是从k减到k-1还是k+1减到k,加上的同理,最后每一个case之间换一行,结尾不换行Orz……另外最近在玩C++11的匿名函数,在sort里随便用下玩

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1e5+7;
struct edge
{
    int to;
    int pre;
};
struct info
{
    int l,r;
    int id,b;
};
info Q[N];
edge E[N];
int head[N],tot;
int L[N],R[N],order,val[N];
int arr[N],cnt[N],ans[N];
vector<int>vec;

void init()
{
    CLR(head,-1);
    tot=0;
    CLR(L,0);
    CLR(R,0);
    order=0;
    CLR(val,0);
    CLR(cnt,0);
    vec.clear();
}
inline void add(int s,int t)
{
    E[tot].to=t;
    E[tot].pre=head[s];
    head[s]=tot++;
}
void dfs(const int &now,const int &pre)
{
    L[now]=++order;
    val[order]=arr[now];
    for (int i=head[now]; ~i; i=E[i].pre)
    {
        int v=E[i].to;
        //if(v!=pre)
        dfs(v,now);
    }
    R[now]=order;
}
int main(void)
{
    int tcase,n,k,i,a,b,m,rt;
    scanf("%d",&tcase);
    for (int q=1; q<=tcase; ++q)
    {
        init();
        scanf("%d%d",&n,&k);
        for (i=1; i<=n; ++i)
        {
            scanf("%d",&arr[i]);
            vec.push_back(arr[i]);
        }
        sort(vec.begin(),vec.end());
        vec.erase(unique(vec.begin(),vec.end()),vec.end());
        for (i=1; i<=n; ++i)
            arr[i]=lower_bound(vec.begin(),vec.end(),arr[i])-vec.begin();
        for (i=0; i<n-1; ++i)
        {
            scanf("%d%d",&a,&b);
            add(a,b);
        }
        dfs(1,-1);
        scanf("%d",&m);
        int unit=(int)sqrt(1.0*n);
        for (i=0; i<m; ++i)
        {
            scanf("%d",&rt);
            Q[i].l=L[rt];
            Q[i].r=R[rt];
            Q[i].id=i;
            Q[i].b=Q[i].l/unit;
        }
        sort(Q,Q+m,[&](const info &x,const info &y){return (x.b==y.b&&x.r<y.r)||x.b<y.b;});
        int l=1,r=0,temp=0;
        for (i=0; i<m; ++i)
        {
            while (l<Q[i].l)
            {
                --cnt[val[l]];
                if(cnt[val[l]]==k)
                    ++temp;
                else if(cnt[val[l]]==k-1)
                    --temp;
                ++l;
            }
            while (l>Q[i].l)
            {
                --l;
                ++cnt[val[l]];
                if(cnt[val[l]]==k)
                    ++temp;
                else if(cnt[val[l]]==k+1)
                    --temp;
            }
            while (r<Q[i].r)
            {
                ++r;
                ++cnt[val[r]];
                if(cnt[val[r]]==k)
                    ++temp;
                else if(cnt[val[r]]==k+1)
                    --temp;
            }
            while (r>Q[i].r)
            {
                --cnt[val[r]];
                if(cnt[val[r]]==k-1)
                    --temp;
                else if(cnt[val[r]]==k)
                    ++temp;
                --r;
            }
            ans[Q[i].id]=temp;
        }
        printf("Case #%d:\n",q);
        for (i=0; i<m; ++i)
            printf("%d\n",ans[i]);
        if(q!=tcase)
            putchar('\n');
    }
    return 0;
}

转载于:https://www.cnblogs.com/Blackops/p/6040686.html

AI-PPT 一键生成 PPT:用户输入主题关键词,AI-PPT 可快速生成完整 PPT,涵盖标题、正文、段落结构等,还支持对话式生成,用户可在 AI 交互窗口边查看边修改。 文档导入转 PPT:支持导入 Word、Excel、PDF 等多种格式文档,自动解析文档结构,将其转换为结构清晰、排版规范的 PPT,有保持原文和智能优化两种模式。 AI-PPT 对话 实时问答:用户上传 PPT 或 PPTX 文件后,可针对演示内容进行提问,AI 实时提供解答,帮助用户快速理解内容。 多角度内容分析:对 PPT 内容进行多角度分析,提供全面视野,帮助用户更好地把握内容结构和重点。 多语言对话支持:支持多语言对话,打破语言障碍,方便不同语言背景的用户使用。 AI - 绘图 文生图:用户输入文字描述,即可生成符合语义的不同风格图像,如油画、水彩、中国画等,支持中英文双语输入。 图生图:用户上传图片并输入描述,AI - 绘图能够根据参考图和描述生成新的风格化图像,适用于需要特定风格或元素的创作需求。 图像编辑:提供如 AI 超清、AI 扩图、AI 无痕消除等功能,用户可以上传图片进行细节修改和优化,提升图片质量。 AI - 文稿 文案生成:能够根据用户需求生成多种类型的文章,如市场营销文案、技术文档、内部沟通内容等,提升文案质量和创作效率。 文章润色:对已有文章进行改善和优化,包括语言表达、逻辑连贯性、内容流畅度等方面,使文章更符合用户期望和风格。 文章续写:AI 技术理解文本语境,为用户提供新的想法、补充资料或更深层次的见解,帮助用户丰富文档内容。 AI - 医生 智能健康咨询:包括症状自查,用户输入不适症状,AI 结合病史等信息提供疾病可能性分析与初步建议;用药指导,支持查询药品适应症、禁忌症等,并预警潜在冲突;中医辨证,提供体质辨识与调理建议。 医学报告解读:用户上传体检报告
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值