[hdu4358]树状数组
思路:用一个数组记录最近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]树状数组的更多相关文章
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- bzoj1878--离线+树状数组
这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]
3529: [Sdoi2014]数表 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1399 Solved: 694[Submit][Status] ...
- BZOJ 3289: Mato的文件管理[莫队算法 树状数组]
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 2399 Solved: 988[Submit][Status][Di ...
- 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组
E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...
- 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序
3881: [Coci2015]Divljak Time Limit: 20 Sec Memory Limit: 768 MBSubmit: 508 Solved: 158[Submit][Sta ...
- 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)
题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...
随机推荐
- Linux相关操作
ssh配置秘钥 连接远程服务器时:需要用户持有“公钥/私钥对”,远程服务器持有公钥,本地持有私钥. 客户端向服务器发出请求.服务器收到请求之后,先在用户的主目录下找到该用户的公钥,然后对比用户发送过来 ...
- asp.net core webapi 配置跨域处理
在Startup.cs文件中的ConfigureServices方法中加入如下代码: //配置跨域处理 services.AddCors(options => { options.AddPoli ...
- response没有实现跳转,而是提示浏览器下载文件
问题简述: web项目中,response没能实现重定向跳转网页,而是通知浏览器下载文件. 代码如下: response.getWriter().write("<h1 style='c ...
- redis: 配置文件详解(十一)
#通用配置 bind 127.0.0.1 #绑定可访问的ip 默认本机访问,如果bind选项为空的话,那会接受所有来自于可用网络接口的连接,也可以绑定指定ip访问 protected-mode yes ...
- php 常量的使用
我们来看下直接的例子 <?php //定义常量 //常量不可被删除 //常量一旦被定义,就无法重新置换 //常量一旦定义,就不能对他第二次定义,否则会报错! define('MYCONSTANT ...
- php内置函数call_user_func()
<?php //call_user_func(callback,name,age) //第一个参数callback作为回掉函数使用,其余的参数是他的参数 function now($a,$b) ...
- 超详细步骤---Linux下的最新Git版本安装
原文地址:https://blog.csdn.net/u010887744/article/details/53957613 [标注大头] 1.查看当前git版本:git --version 查看最新 ...
- 2019-2020-1 20199303《Linux内核原理与分析》第六周作业
系统调用的三层机制 首先是为系统增加新的命令 运行脚本自动生成文件系统 其中有一个显示时间的功能 编辑test.c文件,增加一个hello函数用来显示学号,再次使用make roofts自动编译,调用 ...
- DiskPart.exe and managing Virtual Hard Disks (VHDs) in Windows 7
coreygoOctober 7, 2009 In Windows 7, new commands have been added in DiskPart to allow for the creat ...
- JDK 14的新特性:instanceof模式匹配
JDK 14的新特性:instanceof模式匹配 JDK14在2020年的3月正式发布了.可惜的是正式特性只包含了最新的Switch表达式,而Records,patterns,text blocks ...