[Ox-off Challenge 32E] to reverse the tree

topic

Data range is very strange, ask for the number of reverse \ (k \ Leq 30000 \) , we should be able to find out all the circumstances

For two trees found \ (X, Y \) , if the \ (X \) is the \ (Y \) of ancestors, then the large absolute value symbol point determines whether the formation of reverse

If \ (a_x> a_y \) , not negated \ (a_x \) , then no matter \ (a_y \) negated or not, will certainly form the reverse order, because \ (a_x> a_y> -a_y \) ; whereas if negated \ (a_x \) , then no matter \ (a_y \) negated or not, certainly not form a reverse order, because \ (a_y> -a_y> -a_x \ )

So we ordered from small to large in accordance with the absolute value, then the order of addition of the tree, a contribution to the point of the answer is that the sub-tree is less than the number of its internal point plus the path to the root is greater than the number of its points ; when we added a point \ (X \) , if the non-inverted, then the path to the root is clearly not a bit larger than its inside and its sub-tree is less than all, of the number is formed on the inside of its reverse subtree the number of points; if negated, then the path to the root is greater than its point of all, not a little smaller than the inner sub-tree it is reverse to the number of points on the path to the root form of the number of

So a tree cross + Fenwick tree seeking a bit, then engage in a backpack to divert enough, backpacks need to \ (\ rm bitset \) optimization

Complexity \ (O (n \ log ^ 2n + \ frac {nk} {w}) \)

Code

#include<bits/stdc++.h>
#define re register
#define LL long long
#define lb(i) (i&-i)
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
inline int read() {
    char c=getchar();int x=0;while(c<'0'||c>'9') c=getchar();
    while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-48,c=getchar();return x;
}
const int maxn=1e5+5;
struct E{int v,nxt;}e[maxn<<1];
struct pt{int x,id;}a[maxn];
std::bitset<300001> dp;
int n,num,Q,c[maxn],__;
int sum[maxn],dfn[maxn],top[maxn],fa[maxn],head[maxn],deep[maxn],son[maxn];
inline int cmp(pt A,pt B) {return A.x<B.x;}
inline void add(int x,int y) {
    e[++num].v=y;e[num].nxt=head[x];head[x]=num;
}
inline void ins(int x) {
    for(re int i=x;i<=n;i+=lb(i)) c[i]++;
}
inline int ask(int x) {
    int now=0;
    for(re int i=x;i;i-=lb(i)) now+=c[i];
    return now;
}
inline int calc(int l,int r) {return ask(r)-ask(l-1);}
inline int get(int x) {
    int now=0;
    while(top[x]) now+=calc(dfn[top[x]],dfn[x]),x=fa[top[x]];
    return now;
}
void dfs1(int x) {
    sum[x]=1;
    for(re int i=head[x];i;i=e[i].nxt) {
        if(deep[e[i].v]) continue;
        deep[e[i].v]=deep[x]+1,fa[e[i].v]=x;
        dfs1(e[i].v),sum[x]+=sum[e[i].v];
        if(sum[e[i].v]>sum[son[x]]) son[x]=e[i].v;
    }
}
void dfs2(int x,int topf) {
    top[x]=topf,dfn[x]=++__;
    if(!son[x]) return;
    dfs2(son[x],topf);
    for(re int i=head[x];i;i=e[i].nxt)
    if(!top[e[i].v]) dfs2(e[i].v,e[i].v);
}
int main() {
    n=read();
    for(re int i=1;i<=n;i++) a[i].x=read(),a[i].id=i;
    for(re int x,y,i=1;i<n;i++) x=read(),y=read(),add(x,y),add(y,x);
    deep[1]=1,dfs1(1),dfs2(1,1);
    std::sort(a+1,a+n+1,cmp);
    dp[0]=1;
    for(re int i=1;i<=n;i++) {
        int x=calc(dfn[a[i].id],dfn[a[i].id]+sum[a[i].id]-1),y=get(a[i].id);
        dp=(dp<<x)|(dp<<y);ins(dfn[a[i].id]);
    }
    Q=read();while(Q--) puts(dp[read()]?"Orz":"QAQ");
    return 0;
}

Guess you like

Origin www.cnblogs.com/asuldb/p/11566025.html