有两个序列A和B,A=(a1,a2,...,ak),B=(b1,b2,...,bk),A和B都按升序排列.对于1<=i,j<=k,求k个最小的(ai+bj).要求算法尽量高效. int *min_k(int *A, int *B, int len1, int len2, int k) { if (A == NULL || B == NULL || k <= 0) return NULL; int i, j; int *tmp = new int[k]; i = len1; j = len
247-线段树的查询 II 对于一个数组,我们可以对其建立一棵 线段树, 每个结点存储一个额外的值 count 来代表这个结点所指代的数组区间内的元素个数. (数组中并不一定每个位置上都有元素) 实现一个 query 的方法,该方法接受三个参数 root, start 和 end, 分别代表线段树的根节点和需要查询的区间,找到数组中在区间[start, end]内的元素个数. 注意事项 It is much easier to understand this problem if you fin
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to N.Each node has an integer weight. We will ask you to perform the following operation: u v k : ask for the kth minimum weight on the path from node u
1523. K-inversions Time limit: 1.0 secondMemory limit: 64 MB Consider a permutation a1, a2, …, an (all ai are different integers in range from 1 to n). Let us call k-inversion a sequence of numbers i1, i2, …, ik such that 1 ≤ i1 < i2 < … < ik ≤ n
对于每个节点维护这个节点到根的权值线段树 对于每个询问(x,y),这条路径上的线段树 tree[x]+tree[y]-tree[lca(x,y)]-tree[fa[lca(x,y)]] #include<cstdio> #include<algorithm> using namespace std; ; ; int n,m,tot,cnt,ind,sz,last; int tmp[maxn],hash[maxn],g[maxn],v[maxn]; int num[maxn],pos
以前似乎做过类似的不过当时完全不会.现在看到就有点思路了,开始还有洋洋得意得觉得自己有不小的进步了,结果思路错了...改了很久后测试数据过了还果断爆空间... 给你一串数字A,然后是两种操作: "1 l r k c":意思是当 l=<i<=r 对(i-a)%k = =0 的每个 Ai 都增加 c (1=<k<=10) "2 i" :意思是求出 Ai 一看就是区间更新和单点查询,其实可以用树状数组做,可是觉得线段树好弄一点,结果成功入坑...