HDU 5678 ztr loves trees
来源:互联网 发布:淘宝宝贝描述素材 编辑:程序博客网 时间:2024/04/28 21:59
Problem Description
Super Deity ztr likes trees from childhood,CCTV-children:"The apple on the apple tree.You and I under the apple tree.Play games in front of the apple tree.So many happiness".
One day,qzh visit ztr to ask some questions.To give a tree with a root,each vertex has a value.Each time query the median of a subtree.
ztr said:this is a water problem,do you do it?But qzh show cannot help but want you who is also a Super Deity to help him.Could you help him?
One day,qzh visit ztr to ask some questions.To give a tree with a root,each vertex has a value.Each time query the median of a subtree.
ztr said:this is a water problem,do you do it?But qzh show cannot help but want you who is also a Super Deity to help him.Could you help him?
Input
There are T test cases. The first line of input contains an positive integer T indicating the number of test cases.
For each test case:
Each line contains two positive integer n,m.indicating the number of vetrex and the number of query times.
The next line contains n numbers, the ith number indicating the value of vertex i.
The next n-1 lines,each line contains two numbers u and v,indicating there is a edge form u to v.
The next m lines, each line contains a numbers x.indicating query the median of subtree x.
1<=T<=3,1<=n<=105,1<=m<=106,1<=u<=v<=n,1<=val<=109.
The vetrex 1 is the root of the tree.Guarantee input a tree with a root.
For each test case:
Each line contains two positive integer n,m.indicating the number of vetrex and the number of query times.
The next line contains n numbers, the ith number indicating the value of vertex i.
The next n-1 lines,each line contains two numbers u and v,indicating there is a edge form u to v.
The next m lines, each line contains a numbers x.indicating query the median of subtree x.
The vetrex 1 is the root of the tree.Guarantee input a tree with a root.
Output
For each test case:print a line.To avoid huge output,you should hash each answer first,then print it.
The method to hash:a[i] indicates the ith query result,ans=∑a[i]∗10m−imod1,000,000,007 Round to the nearest tenth
The method to hash:a[i] indicates the ith query result,
Sample Input
15 31 2 3 4 51 22 33 44 5123
Sample Output
339.0先从树形转线形,然后是区间第k大即可。这题数据有问题,val有等于0的,我写的不是离散的所以一直wa,简直有毒。#include<cmath>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef unsigned long long LL;const int mod = 1e9 + 7;const int maxn = 1e5 + 10;int T, n, m, v[maxn], x, y;int ft[maxn], nt[maxn], u[maxn], sz;int now[maxn], tot;double ans[maxn];struct Tree{int f[maxn << 6], L[maxn << 6], R[maxn << 6];int root[maxn], sz;int Node() { f[sz] = L[sz] = R[sz] = 0; return sz++; }void clear() { sz = 1; root[0] = 0; }void add(int bef, int now, int l, int r, int v){L[now] = L[bef]; R[now] = R[bef]; f[now] = f[bef] + 1;if (l == r) return;int m = l + r >> 1;if (v <= m){L[now] = Node();add(L[bef], L[now], l, m, v);}else{R[now] = Node();add(R[bef], R[now], m + 1, r, v);}}void insert(int rt, int v){root[rt] = Node();add(root[rt - 1], root[rt], 0, 1e9, v);}double find(int bef, int now, int l, int r, int v){if (l == r) return l;int m = l + r >> 1;if (f[L[now]] - f[L[bef]] >= v) return find(L[bef], L[now], l, m, v);return find(R[bef], R[now], m + 1, r, v - f[L[now]] + f[L[bef]]);}double get(int r1, int r2){if ((r2 - r1) & 1) return find(root[r1], root[r2], 0, 1e9, r2 - r1 + 1 >> 1);return (find(root[r1], root[r2], 0, 1e9, r2 - r1 >> 1) + find(root[r1], root[r2], 0, 1e9, r2 - r1 + 2 >> 1)) / 2.0;}}solve;void dfs(int x){solve.insert(now[x] = ++tot, v[x]);for (int i = ft[x]; i != -1; i = nt[i]) dfs(u[i]);ans[x] = solve.get(now[x] - 1, tot);}int main(){scanf("%d", &T);while (T--){scanf("%d%d", &n, &m);tot = sz = 0;for (int i = 1; i <= n; i++){scanf("%d", &v[i]);ft[i] = -1;}for (int i = 1; i < n; i++){scanf("%d%d", &x, &y);u[sz] = y; nt[sz] = ft[x]; ft[x] = sz++;}solve.clear(); dfs(1);double res = 0;while (m--){scanf("%d", &x);res = fmod(res * 10 + ans[x], mod);}printf("%.1lf\n", res);}return 0;}
0 0
- HDU 5678 ztr loves trees
- HDU5678-ztr loves trees
- hdu 5678 ztr loves trees(求子树第k大 线段树套主席树)
- hdu 5678 ztr loves trees(dfs序、主席树静态第k大)
- hdu--5675 ztr loves math
- HDU 5675 ztr loves math
- HDU 5675 ztr loves math
- HDU 5677 ztr loves substring
- hdu 5675 ztr loves math
- hdu 5675 ztr loves math
- hdu 5677-ztr loves substring
- hdu 5677 ztr loves substring
- hdu 5678 ztr loves trees (给一颗有根树,树上的每一个节点有一个权值,每次询问某个子树中所有权值的中位数)
- HDU 5676 ztr loves lucky numbers
- HDU 5676 ztr loves lucky numbers
- hdu 5675 ztr loves math【思维】
- Hdu 5676 ztr loves lucky numbers
- HDU 5677 ztr loves substring (dp)
- HDOJ3785 priority_queue AC, set TLE ,vector TLE
- linux内存源码分析 - 内存回收(整体流程)
- 单例模式的七种写法
- nefu 561 方块计算(DFS)
- 我的第一篇博客
- HDU 5678 ztr loves trees
- Android Studio 进阶详细配置
- cmd 下登陆ftp及相关操作
- 第十周项目一 阅读下面的定义,请说出在测试函数中不同情况的调用产生的结果(5 c)
- java类中获得spring中的bean
- 聊聊Socket、TCP/IP、HTTP、FTP及网络编程
- java基础知识总结(七)
- 兔子-you are attempting to install the android sdk inside your android studio installation
- hdu 2089 不要62