思路:用一个数组记录最近k次的出现位置,然后在其附近更新答案。具体见代码:

 #pragma comment(linker, "/STACK:10240000,10240000")

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define define_m int m = (l + r) >> 1
#define Rep(a, b) for(int a = 0; a < b; a++)
#define lowbit(x) ((x) & (-(x)))
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {} typedef double db;
typedef long long LL;
typedef pair<int, int> pii;
typedef multiset<int> msi;
typedef multiset<int>::iterator msii;
typedef set<int> si;
typedef set<int>::iterator sii;
typedef vector<int> vi; const int dx[] = {, , -, , , , -, -};
const int dy[] = {, -, , , -, , , -};
const int maxn = 1e5 + ;
const int maxm = 1e5 + ;
const int maxv = 1e7 + ;
const int MD = 1e9 +;
const int INF = 1e9 + ;
const double PI = acos(-1.0);
const double eps = 1e-; template<class edge> struct Graph {
vector<vector<edge> > adj;
Graph(int n) { adj.clear(); adj.resize(n + ); }
Graph() { adj.clear(); }
void resize(int n) { adj.resize(n + ); }
void add(int s, edge e){ adj[s].push_back(e); }
void del(int s, edge e) { adj[s].erase(find(iter(adj[s]), e)); }
void clear() { adj.clear(); }
vector<edge>& operator [](int t) { return adj[t]; }
}; template<class T> struct TreeArray {
vector<T> c;
int maxn;
TreeArray(int n) { c.resize(n + ); maxn = n + ; }
TreeArray() { c.clear(); maxn = ; }
void clear() { memset(&c[], , sizeof(T) * maxn); }
void resize(int n) { c.resize(n + ); maxn = n + ; }
void add(int p, T x) { while (p <= maxn) { c[p] += x; p += lowbit(p); } }
T get(int p) { T res = ; while (p) { res += c[p]; p -= lowbit(p); } return res; }
T range(int a, int b) { return get(b) - get(a - ); }
}; bool cmp(const pair<pii, int> &a, const pair<pii, int> &b) {
return a.first.second < b.first.second;
} int a[maxn], b[maxn], c, n, k, L[maxn], R[maxn], vis[maxn], cc, out[maxn]; TreeArray<int> ts;
pair<pii, int> in[maxn];
Graph<int> G; int find(int x) { return lower_bound(b + , b + c + , x) - b; } void DFS(int u) {
vis[u] = ;
L[u] = ++cc;
b[cc] = a[u];
for (int i = ; i < G[u].size(); i++) {
if (!vis[G[u][i]] || !vis[G[u][i]]) {
DFS(G[u][i]);
}
}
R[u] = cc;
} int main() {
//freopen("in.txt", "r", stdin);
int T, cas = , m;
cin >> T;
while (T--) {
scanf("%d%d", &n, &k);
for (int i = ; i <= n; i++) {
scanf("%d", a + i);
}
G.clear();
G.resize(n);
for (int i = , u, v; i < n; i++) {
scanf("%d%d", &u, &v);
G.add(u, v);
G.add(v, u);
}
mem0(vis);
cc = ;
DFS();
memcpy(a, b, sizeof(b));
sort(b + , b + n + );
c = unique(b + , b + n + ) - b - ;
for (int i = ; i <= n; i++) {
a[i] = find(a[i]);
}
cin >> m;
for (int i = , x; i < m; i++) {
scanf("%d", &x);
in[i].first = make_pair(L[x], R[x]);
in[i].second = i;
}
sort(in, in + m, cmp);
G.clear();
G.resize(n);
ts.clear();
ts.resize(n);
mem0(vis); int last = ;
for (int i = ; i < m; i++) {
for (int j = last + ; j <= in[i].first.second; j++) {
G.add(a[j], j);
int sz = G[a[j]].size();
if (sz >= k) {
ts.add(G[a[j]][sz - k], );
if (sz > k) {
ts.add(G[a[j]][sz - k - ], -);
if (sz > k + ) ts.add(G[a[j]][sz - k - ], );
}
}
}
out[in[i].second] = ts.range(in[i].first.first, in[i].first.second);
//cout << out[in[i].second] << " " << in[i].first.first << " " << in[i].first.second << endl; last = in[i].first.second;
}
if (cas) cout << endl;
printf("Case #%d:\n", ++cas);
for (int i = ; i < m; i++) {
printf("%d\n", out[i]);
}
}
return ;
}

最新文章

  1. LATEX数学公式基本语法
  2. Android消息通知(notification)和PendingIntent传值
  3. [ZZ] 景深效果(Depth of Field) , Pass1 将场景渲染到一个RenderTarget,做为清晰版, Pass2: BluredRT , Pass3: WDepth = Depth / Far_Z_Clip
  4. vim 7.4 编译安装
  5. hdu 3460
  6. 最接近原生APP体验的高性能前端框架-MUI
  7. NumPy学习_00 ndarray的创建
  8. Centos7下安装配置elasticsearch 6.3.1
  9. 大牛的IT经验,方法【跟,帮】
  10. LeetCode每天一题之两数之和
  11. JDK动态代理与CGLib动态代理相关问题
  12. 洛谷P4052 [JSOI2007]文本生成器 AC自动机+dp
  13. WordPress主题开发实例:get_term_by()获取指定分类链接
  14. tornado-简单的服务器非阻塞
  15. ASP.NET下使用Combres对JS、CSS合并和压缩
  16. BZOJ 4939 [Ynoi2016]掉进兔子洞(莫队+bitset)
  17. 如何自定义echarts 线性图的选择事件
  18. HashMap与HashTable原理及数据结构
  19. cn_office_professional_plus_2010_x86_515 安装激活方法解决方案64bit
  20. 【bzoj4010】[HNOI2015]菜肴制作 拓扑排序+堆

热门文章

  1. 掌握MySQL连接查询到底什么是驱动表
  2. Linux 常用到的命令
  3. 《Spring In Action》阅读笔记之核心概念
  4. SpringMVC数据传递及乱码问题
  5. qa问答机器人pysparnn问题的召回
  6. pytorch 去除维度为1的维度
  7. HDFS一些基本操作方法
  8. docker(2)
  9. Spring5参考指南:SpringAOP简介
  10. Ubuntu登陆时忘记密码怎么办