• codeforces 633G. Yash And Trees dfs序+线段树+bitset


    题目链接

    G. Yash And Trees
    time limit per test
    4 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    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 aiassociated with it. Also, integer m is given.

    There are queries of two types:

    1. for given node v and integer value x, increase all ai in the subtree of node v by value x
    2. 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 mfrom 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.

    Examples
    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
    output
    3
    1
    1
    input
    5 10
    8 7 5 1 0
    1 2
    2 3
    1 5
    2 4
    3
    1 1 0
    1 1 2
    2 2
    output
    2


    题目给的第二个操作, Ai = p+k*m就相当于问Ai%m是不是素数, 想到这个就好做了....每一个节点一个bitset, 然后查询完之后结果与一个素数表进行&操作, 看还剩下几个值, 答案就是几。
    #include <iostream>
    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #include <set>
    #include <string>
    #include <queue>
    #include <stack>
    #include <bitset>
    using namespace std;
    #define pb(x) push_back(x)
    #define ll long long
    #define mk(x, y) make_pair(x, y)
    #define lson l, m, rt<<1
    #define mem(a) memset(a, 0, sizeof(a))
    #define rson m+1, r, rt<<1|1
    #define mem1(a) memset(a, -1, sizeof(a))
    #define mem2(a) memset(a, 0x3f, sizeof(a))
    #define rep(i, n, a) for(int i = a; i<n; i++)
    #define fi first
    #define se second
    typedef pair<int, int> pll;
    const double PI = acos(-1.0);
    const int inf = 1061109567;
    const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
    const int maxn = 1e5+2;
    int in[maxn], out[maxn], dfs_clock, mod, add[maxn<<2];
    bitset <1000> sum[maxn<<2], prime;
    int head[maxn*2], num, a[maxn], b[maxn], vx[1005];
    struct node
    {
        int to, nextt;
    }e[maxn*2];
    void addd(int u, int v) {
        e[num].to = v, e[num].nextt = head[u], head[u] = num++;
    }
    void init() {
        num = 0;
        mem1(head);
    }
    void dfs(int u, int fa) {
        in[u] = ++dfs_clock;
        for(int i = head[u]; ~i; i = e[i].nextt) {
            int v = e[i].to;
            if(v == fa)
                continue;
            dfs(v, u);
        }
        out[u] = dfs_clock;
    }
    inline void pushUp(int rt) {
        sum[rt] = sum[rt<<1]|sum[rt<<1|1];
    }
    inline void change(int rt, int val) {
        sum[rt] = (sum[rt]<<val)|(sum[rt]>>(mod-val));
    }
    void pushDown(int rt) {
        if(add[rt]) {
            change((rt<<1), add[rt]);
            change((rt<<1|1), add[rt]);
            add[rt<<1] = (add[rt]+add[rt<<1])%mod;
            add[rt<<1|1] = (add[rt]+add[rt<<1|1])%mod;
            add[rt] = 0;
        }
    }
    void build(int l, int r, int rt) {
        if(l == r) {
            sum[rt][b[l]] = 1;
            return ;
        }
        int m = l+r>>1;
        build(lson);
        build(rson);
        pushUp(rt);
    }
    void update(int L, int R, int val, int l, int r, int rt) {
        if(L<=l&&R>=r) {
            change(rt, val);
            add[rt] = (val+add[rt])%mod;
            return ;
        }
        pushDown(rt);
        int m = l+r>>1;
        if(L<=m)
            update(L, R, val, lson);
        if(R>m)
            update(L, R, val, rson);
        pushUp(rt);
    }
    bitset<1000> query(int L, int R, int l, int r, int rt) {
        if(L<=l&&R>=r) {
            return sum[rt];
        }
        pushDown(rt);
        bitset<1000> ret;
        ret.reset();
        int m = l+r>>1;
        if(L<=m)
            ret |= query(L, R, lson);
        if(R>m)
            ret |= query(L, R, rson);
        return ret;
    }
    int main()
    {
        int n, u, v, q, x, y, z;
        scanf("%d%d", &n, &mod);
        init();
        for(int i = 1; i<=n; i++) {
            scanf("%d", &a[i]);
            a[i] %= mod;
        }
        for(int i = 1; i<n; i++) {
            scanf("%d%d", &u, &v);
            addd(u, v);
            addd(v, u);
        }
        for(int i = 2; i<mod; i++)
        {
            if(vx[i])
                continue;
            prime[i] = 1;
            for(int j = i; j<mod; j+=i)
                vx[j]=1;
        }
        dfs(1, -1);
        for(int i = 1; i<=n; i++) {
            b[in[i]] = a[i];
        }
        build(1, n, 1);
        scanf("%d", &q);
        while(q--) {
            scanf("%d%d", &x, &y);
            if(x == 1) {
                scanf("%d", &z);
                z %= mod;
                update(in[y], out[y], z, 1, n, 1);
            } else {
                bitset<1000> tmp = query(in[y], out[y], 1, n, 1);
                tmp &= prime;
                printf("%d
    ", tmp.count());
            }
        }
        return 0;
    }
  • 相关阅读:
    关于大文件下载
    关于小文件下载
    小文件下载
    AppStore 中的app怎么样生成二维码,来提供下载
    重学STM32---(十)之CAN通信(二)
    重学STM32---(九)之CAN通信(一)
    将博客搬至CSDN
    绑定socket描述符到一个网络设备
    通用 Makefile(及makefile中的notdir,wildcard和patsubst)
    vsftpd 编译安装 及 隐藏版本号
  • 原文地址:https://www.cnblogs.com/yohaha/p/5237741.html
Copyright © 2020-2023  润新知