思路:用一个数组记录最近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 ;
}

[hdu4358]树状数组的更多相关文章

  1. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  2. bzoj1878--离线+树状数组

    这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...

  3. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  4. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  5. BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]

    3529: [Sdoi2014]数表 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1399  Solved: 694[Submit][Status] ...

  6. BZOJ 3289: Mato的文件管理[莫队算法 树状数组]

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2399  Solved: 988[Submit][Status][Di ...

  7. 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组

    E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  8. 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序

    3881: [Coci2015]Divljak Time Limit: 20 Sec  Memory Limit: 768 MBSubmit: 508  Solved: 158[Submit][Sta ...

  9. 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)

    题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...

随机推荐

  1. JS Math&Date的方法 (上)

    数学对象&时间对象 本篇文章主要介绍Math 和 Date  的常用方法! 一 :Math & Date         Math 数学对象 - 处理数学计算和数学类          ...

  2. Postman:Pre-request Script

    Pre-request Script:前置处理,会在发出请求前执行,主要用在生成一些动态参数. 例如:api接口都会有签名校验,这个校验在我们api测试的时候很不方便,这里可以利用 postman 前 ...

  3. [YII2] 去除自带js,加载自己的JS,然后ajax(json)传值接值!

    本想用YII2自带的JS,可是用着效果不好,想从新加载,找了好多终于实现啦!还有ajax(json)传值接值! 首先直接了当的就把YII2自带的js去掉! 把下面代码加入到/config/main.p ...

  4. springmvc配置数据源方式

    1 阿里巴巴的druid数据源 <!-- 配置数据源 使用的是Druid数据源 -->-<bean destroy-method="close" init-met ...

  5. search(7)- elastic4s-search-filter模式

    现在我们可以开始探讨ES的核心环节:搜索search了.search又分filter,query两种模式.filter模式即筛选模式:将符合筛选条件的记录作为结果找出来.query模式则分两个步骤:先 ...

  6. GitHub 热点速览 Vol.17:在?各家视频会员要不要?

    作者:HelloGitHub-小鱼干 摘要:经济实用,用作上周的 GitHub 热点的横批再合适不过.先不说 GitHub Trending 上不止一个的会员共享项目,免你找好友刷脸要会员,这项目实在 ...

  7. 前端基础进阶(七)-前端工程师最容易出错的问题-this关键字

    我们在学习JavaScript的时候,因为对一些概念不是很清楚,但是又会通过一些简洁的方式把它给记下来,那么这样自己记下来的概念和真正的概念产生了很强的偏差. 当然,还有一些以为这个是对的,还会把它发 ...

  8. Golang快速入门:从菜鸟变大佬

    最近写了不少Go代码,但是写着写着,还是容易忘,尤其是再写点Python代码后.所以找了一篇不错的Golang基础教程,翻译一下,时常看看. 原文链接: 「Learning Go - from zer ...

  9. 关于暴力破解的一些学习笔记(pikachu)

    这几天的笔记都懒得发博客都写在本地了,随缘搬上来 什么是暴力破解 就是在攻击者不知道目标账号密码情况下的,对目标系统的常识性登陆 一般会采用一些工具+特定的字典 来实现高效的连续的尝试性登陆 一个有效 ...

  10. c语言 字符串大小写转换

    https://www.programmingsimplified.com/c/program/c-program-change-case https://docs.microsoft.com/en- ...