zoukankan      html  css  js  c++  java
  • Manthan, Codefest 16 G. Yash And Trees dfs序+线段树+bitset

    G. Yash And Trees

    题目连接:

    http://www.codeforces.com/contest/633/problem/G

    Description

    Yash loves playing with trees and gets especially excited when they have something to do with prime numbers. On his 20th birthday he was granted with a rooted tree of n nodes to answer queries on. Hearing of prime numbers on trees, Yash gets too intoxicated with excitement and asks you to help out and answer queries on trees for him. Tree is rooted at node 1. Each node i has some value ai associated with it. Also, integer m is given.

    There are queries of two types:

    for given node v and integer value x, increase all ai in the subtree of node v by value x
    for given node v, find the number of prime numbers p less than m, for which there exists a node u in the subtree of v and a non-negative integer value k, such that au = p + m·k.

    Input

    The first of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ 1000) — the number of nodes in the tree and value m from the problem statement, respectively.

    The second line consists of n integers ai (0 ≤ ai ≤ 109) — initial values of the nodes.

    Then follow n - 1 lines that describe the tree. Each of them contains two integers ui and vi (1 ≤ ui, vi ≤ n) — indices of nodes connected by the i-th edge.

    Next line contains a single integer q (1 ≤ q ≤ 100 000) — the number of queries to proceed.

    Each of the last q lines is either 1 v x or 2 v (1 ≤ v ≤ n, 0 ≤ x ≤ 109), giving the query of the first or the second type, respectively. It's guaranteed that there will be at least one query of the second type.

    Output

    For each of the queries of the second type print the number of suitable prime numbers.

    Sample Input

    8 20
    3 7 9 8 4 11 7 3
    1 2
    1 3
    3 4
    4 5
    4 6
    4 7
    5 8
    4
    2 1
    1 1 1
    2 5
    2 4

    Sample Output

    3
    1
    1

    题意

    给你一棵树,然后给你n,m

    每个点有一个点权

    有两个操作

    1 x v 使得x子树里面的所有点的权值加v

    2 x 查询x的子树里面有多少个质数p满足p+k*m,其中p是小于m的素数

    题解:

    这题感觉毫无头绪,看了大佬的博客才发一个神奇东西叫bitset。

    先dfs将子树问题转化成区间问题。

    然后线段树维护整个区间,每个节点挂一个bitset存这个子区间所含质数情况。

    更新操作,就直接让这个bitset左右移动就好了,移动可以拆成两个步骤,一个向右边移动(x%m),一个向左边移动(m-(x%m)),然后两个取并集。

    查询操作,就最后得到那个区间的bitset和素数表&一下就好了

    代码

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define INF 0x7f7f7f7f
    #define N 100050
    #define M 200050
    #define MAXN 1050
    bitset<MAXN>Pri;
    int n,m,k,tot,cnt;
    int last[N],rk[N],kth[N],size[N],w[N];
    struct Edge{int from,to,s;}edges[M];
    struct Tree{int l,r,lazy;bitset<MAXN>sum;}tr[N<<2];
    template<typename T>void read(T&x)
    {
        ll k=0; char c=getchar();
        x=0;
        while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
        if (c==EOF)exit(0);
        while(isdigit(c))x=x*10+c-'0',c=getchar();
        x=k?-x:x;
    }
    void read_char(char &c)
    {while(!isalpha(c=getchar())&&c!=EOF);}
    void AddEdge(int x,int y)
    {
        edges[++tot]=Edge{x,y,last[x]};
        last[x]=tot;
    }
    void get_pri()
    {
        static int f[M];
        for(int i=2;i<m;i++)
        {
            if (f[i])continue;
            Pri[i]=1;
            int j=i;
            while(j+i<m)f[j+i]=1,j=j+i;
        }
    }
    void dfs(int x,int pre)
    {
        kth[++cnt]=x;
        rk[x]=cnt;
        size[x]=1;
        for(int i=last[x];i;i=edges[i].s)
        {
            Edge & e=edges[i];
            if (e.to==pre)continue;
            dfs(e.to,x);
            size[x]+=size[e.to];
        }
    }
    void push_up(int x)
    {
        tr[x].lazy%=m;
        if (tr[x].l!=tr[x].r)tr[x].sum=tr[x<<1].sum|tr[x<<1|1].sum;
        tr[x].sum=(tr[x].sum<<tr[x].lazy)|(tr[x].sum>>(m-tr[x].lazy));
        if (tr[x].l==tr[x].r)tr[x].lazy=0;   
    }
    void push_down(int x)
    {
        tr[x<<1].lazy+=tr[x].lazy;
        tr[x<<1|1].lazy+=tr[x].lazy;
        tr[x].lazy=0;
        push_up(x<<1);
        push_up(x<<1|1);
    }
    void bt(int x,int l,int r)
    {
        tr[x].l=l; tr[x].r=r; tr[x].lazy=0;
        if (l==r)
        {
            tr[x].sum[w[kth[l]]]=1;
            return ;
        }
        int mid=(l+r)>>1;
        bt(x<<1,l,mid);
        bt(x<<1|1,mid+1,r);
        push_up(x);
    }
    void update(int x,int l,int r,int tt)
    {
        if (l<=tr[x].l&&tr[x].r<=r)
        {
            tr[x].lazy+=tt;
            push_up(x);
            return;
        }
        int mid=(tr[x].l+tr[x].r)>>1;
        if (l<=mid)update(x<<1,l,r,tt);
        if (mid<r)update(x<<1|1,l,r,tt);
        push_up(x);
    }
    bitset<MAXN> query(int x,int l,int r)
    {
        if (l<=tr[x].l&&tr[x].r<=r)return tr[x].sum;
        int mid=(tr[x].l+tr[x].r)>>1;
        bitset<MAXN>ans(0);
        push_down(x);
        if (l<=mid)ans|=query(x<<1,l,r);
        if (mid<r)ans|=query(x<<1|1,l,r);
        push_up(x);
        return ans;
    }
    void work()
    {
        read(n); read(m);
        get_pri();
        for(int i=1;i<=n;i++)read(w[i]),w[i]%=m;
        for(int i=1;i<n;i++)
        {
            int x,y;
            read(x); read(y);
            AddEdge(x,y);
            AddEdge(y,x);
        }
        dfs(1,0);
        bt(1,1,n);
        read(k);
        for(int i=1;i<=k;i++)
        {
            int id,x,tt;
            read(id);
            if (id==1){read(x);read(tt);update(1,rk[x],rk[x]+size[x]-1,tt);}
            if (id==2)
            {
                read(x);
                bitset<MAXN>ans=query(1,rk[x],rk[x]+size[x]-1)&Pri;
                printf("%d
    ",(int)(ans.count()));
            }
        }
    }
    int main()
    {
    #ifndef ONLINE_JUDGE
        freopen("aa.in","r",stdin);
    #endif
        work();
    }
    
    
  • 相关阅读:
    Qt使用QCustomplot绘制曲线--修改纵坐标显示宽度
    为WPF项目添加Program.cs
    error: C1083: 无法打开包括文件: “QApplication”: No such file or directory
    clangbackend已停止工作
    无法处理文件 MinimalSimpleBrowserForm.resx,因为它位于 Internet 或受限区域中,或者文件上具有 Web 标记。要想处理这些文件,请删除 Web 标记。
    QByteArray转QString打印
    zend studio中ctrl+鼠标左键无法转到类或函数定义文件的解决方法
    HTML无刷新提交表单
    这个是我得标题:1548241388
    这个是我得标题:1548241357
  • 原文地址:https://www.cnblogs.com/mmmqqdd/p/11241333.html
Copyright © 2011-2022 走看看