【HDOJ】4605 Magic Ball Game
思路1:
树状数组+离线处理,对所有的w离散化处理,边dfs边使用树状数组更新左右w的情况。
思路2:
主席树,边bfs边建树。结点信息存储cnt,然后在线查询。
树状数组。
/* 4605 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct ques_t {
int w, id; ques_t() {}
ques_t(int w, int id):
w(w), id(id) {} } ques_t; const int maxn = 1e5+;
const int maxm = maxn * ;
int a[maxm];
int W[maxn];
ques_t Q[maxn];
vector<ques_t> vc[maxn];
int ans[maxn][];
int son[maxn][];
int tot[maxm][];
int n, m; int lowest(int x) {
return x & -x;
} void add(int x, int delta, int id) {
while (x <= m) {
tot[x][id] += delta;
x += lowest(x);
}
} int sum(int x, int id) {
int ret = ; while (x) {
ret += tot[x][id];
x -= lowest(x);
} return ret;
} void init() {
memset(son, , sizeof(son));
memset(tot, , sizeof(tot));
rep(i, , n+)
vc[i].clr();
m = ;
} void dfs(int u, int tot) {
int v, sz = SZ(vc[u]);
int llt, lgt, rlt, rgt;
int eq; rep(i, , sz) {
int w = vc[u][i].w;
int id = vc[u][i].id;
llt = sum(w-, );
lgt = sum(m, ) - sum(w, );
rlt = sum(w-, );
rgt = sum(m, ) - sum(w, );
eq = tot - llt - lgt - rlt - rgt;
if (eq) {
ans[id][] = -;
} else {
ans[id][] = (lgt+rgt) + (llt+rlt)*;
ans[id][] = rlt;
}
}
if (son[u][]) {
add(W[u], , );
dfs(son[u][], tot+);
add(W[u], -, ); add(W[u], , );
dfs(son[u][], tot+);
add(W[u], -, );
}
} void solve() {
int q;
int x, w; scanf("%d", &q);
rep(i, , q) {
scanf("%d %d", &Q[i].id, &Q[i].w);
a[m++] = Q[i].w;
} sort(a+, a+m);
m = unique(a+, a+m) - (a+); rep(i, , n+) {
W[i] = lower_bound(a+, a++m, W[i]) - a;
} rep(i, , q) {
int u = Q[i].id;
w = lower_bound(a+, a++m, Q[i].w) - a;
vc[u].pb(ques_t(w, i));
} dfs(, ); rep(i, , q) {
if (ans[i][] == -)
puts("");
else
printf("%d %d\n", ans[i][], ans[i][]);
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t;
int u, v1, v2; scanf("%d", &t);
while (t--) {
init();
scanf("%d", &n);
rep(i, , n+) {
scanf("%d", &W[i]);
a[m++] = W[i];
}
int k;
scanf("%d", &k);
rep(i, , k) {
scanf("%d %d %d", &u, &v1, &v2);
son[u][] = v1;
son[u][] = v2;
}
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
主席树。
/* 4605 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
// #define lson l, mid, rt<<1
// #define rson mid+1, r, rt<<1|1 const int INF = 1e9+;
const int maxn = 1e5+;
const int maxm = maxn * ;
int a[maxn], W[maxn];
int T[maxn];
int lson[maxm], rson[maxm];
int lc[maxm], rc[maxm];
int n, m, tot;
int son[maxn][]; void init() {
tot = m = ;
memset(son, , sizeof(son));
} int Build(int l, int r) {
int rt = tot++; lc[rt] = rc[rt] = ; if (l == r)
return rt; int mid = (l + r) >> ; lson[rt] = Build(l, mid);
rson[rt] = Build(mid+, r); return rt;
} int Insert(int rt, int p, int lval, int rval) {
int nrt = tot++, ret = nrt;
int l = , r = m - , mid; lc[nrt] = lc[rt] + lval;
rc[nrt] = rc[rt] + rval;
while (l < r) {
mid = (l + r) >> ;
if (p <= mid) {
lson[nrt] = tot++;
rson[nrt] = rson[rt];
nrt = lson[nrt];
rt = lson[rt];
r = mid;
} else {
lson[nrt] = lson[rt];
rson[nrt] = tot++;
nrt = rson[nrt];
rt = rson[rt];
l = mid + ;
}
lc[nrt] = lc[rt] + lval;
rc[nrt] = rc[rt] + rval;
} return ret;
} pii Query(int rt, int L, int R, int l, int r) {
if (L==l && R==r)
return mp(lc[rt], rc[rt]); int mid = (l + r) >> ; if (R <= mid) {
return Query(lson[rt], L, R, l, mid);
} else if (L > mid) {
return Query(rson[rt], L, R, mid+, r);
} else {
pii ltmp = Query(lson[rt], L, mid, l, mid);
pii rtmp = Query(rson[rt], mid+, R, mid+, r);
return mp(ltmp.fir+rtmp.fir, ltmp.sec+rtmp.sec);
}
} void solve() {
a[m++] = INF;
a[m++] = ;
sort(a, a+m);
m = unique(a, a+m) - a; T[] = Build(, m-);
queue<int> Q;
int u, v; Q.push();
while (!Q.empty()) {
u = Q.front();
Q.pop();
if (son[u][]) {
int wid = lower_bound(a, a+m, W[u]) - a;
T[son[u][]] = Insert(T[u], wid, , );
T[son[u][]] = Insert(T[u], wid, , );
Q.push(son[u][]);
Q.push(son[u][]);
}
} int q;
int w;
int n2, n7; scanf("%d", &q);
while (q--) {
scanf("%d %d", &v, &w);
int wid = lower_bound(a, a+m, w) - a;
if (a[wid] == w) {
// check hit w
pii eq = Query(T[v], wid, wid, , m-);
if (eq.fir+eq.sec > ) {
puts("");
continue;
}
} pii lt = Query(T[v], , wid-, , m - );
pii gt = Query(T[v], wid, m-, , m - ); n2 = (gt.fir+gt.sec) + (lt.fir+lt.sec) * ;
n7 = lt.sec; printf("%d %d\n", n7, n2);
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t;
int u; scanf("%d", &t);
rep(tt, , t+) {
scanf("%d", &n);
init();
rep(i, , n+) {
scanf("%d", &W[i]);
a[m++] = W[i];
}
int k;
scanf("%d", &k);
rep(i, , k) {
scanf("%d", &u);
scanf("%d %d", &son[u][], &son[u][]);
}
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
数据发生器。
from copy import deepcopy
from random import randint, shuffle
import shutil
import string def GenDataIn():
with open("data.in", "w") as fout:
t = 1
bound = 10**5 / 2
fout.write("%d\n" % (t))
for tt in xrange(t):
m = randint(100, 200)
n = 2 * m + 1
fout.write("%d\n" % (n))
L = []
for i in xrange(n):
w = randint(1, bound)
L.append(w)
fout.write(" ".join(map(str, L)) + "\n")
ust = [1]
vst = range(2, n+1)
fout.write("%d\n" % (m))
for i in xrange(m):
idx = randint(0, len(ust)-1)
u = ust[idx]
ust.remove(u)
V = []
for j in xrange(2):
idx = randint(0, len(vst)-1)
v = vst[idx]
V.append(v)
ust.append(v)
vst.remove(v)
fout.write("%d %d %d\n" % (u, V[0], V[1]))
q = randint(n/2, n)
fout.write("%d\n" % (q))
for i in xrange(q):
x = randint(1, n)
w = randint(1, bound)
fout.write("%d %d\n" % (x, w)) def MovDataIn():
desFileName = "F:\eclipse_prj\workspace\hdoj\data.in"
shutil.copyfile("data.in", desFileName) if __name__ == "__main__":
GenDataIn()
MovDataIn()
【HDOJ】4605 Magic Ball Game的更多相关文章
- hdu 4605 Magic Ball Game
http://acm.hdu.edu.cn/showproblem.php?pid=4605 可以离线求解 把所以可能出现的 magic ball 放在一个数组里(去重),从小到大排列 先不考虑特殊 ...
- HDU 4605 Magic Ball Game(可持续化线段树,树状数组,离散化)
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 【HDOJ】3183 A Magic Lamp
RMQ. /* 3183 */ #include <cstdio> #include <cstring> #include <cstdlib> #define MA ...
- 【HDOJ】1556 Color the ball
简单线段树. #include <stdio.h> #define MAXN 100005 #define lson l, mid, rt<<1 #define rson mi ...
- 【HDOJ】5155 Harry And Magic Box
DP.dp[i][j]可以表示i行j列满足要求的组合个数,考虑dp[i-1][k]满足条件,那么第i行的那k列可以为任意排列(2^k),其余的j-k列必须全为1,因此dp[i][j] += dp[i- ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
随机推荐
- JS的IE和FF兼容性问题汇总
转自:蓝色理想 以下以 IE 代替 Internet Explorer,以 MF 代替 Mozilla FF 一.函数和方法差异 1. getYear()方法 [分析说明]先看一下以下代码: var ...
- .NET架构师技能体系
.NET架构师应该掌握什么样的技术?其实这个问题很简单,去看看招聘.NET架构师的公司的职位要求就知道了.比如:http://www.cnblogs.com/guwei4037/p/5615471.h ...
- 【NetOffice Excel】Excel合并单元格【原】
CSharp操作Excel采用开源的原生.NET程序集NetOffice,格式兼容性更好. 在操作Excel的时候有时候需要合并单元格 using ExcelOffice = NetOffice.Ex ...
- 一款js点击显示和隐藏的例子(pc,移动端通用)
html部分: <div id="box"> <div id="box_title">标题</div> <div id ...
- gson小练习之嵌套复杂数据解析
package com.zf.demo; import java.util.List; import com.google.gson.Gson; public class JGson { /** * ...
- 手把手教你写LKM rookit! 之 第一个lkm程序及模块隐藏(一)
唉,一开始在纠结起个什么名字,感觉名字常常的很装逼,于是起了个这<手把手教你写LKM rookit> 我觉得: 你们觉得:...... 开始之前,我们先来理解一句话:一切的操作都是系统调用 ...
- Oracle中的CR块详解
1.概述 Cr块consistent read块也就是用来维护oracle的读一致性的数据块.当查询某些数据的时候,发现数据块的版本比我们要查询的新,例如session1执行了dml操作并没有提交,s ...
- oracle linux了解基本命令行
1. Linux的版本:核心(kernel)版本和发行(distribution)版本 2. 复制.删除和移动文件的命令 cp [选项] 源文件或目录 目标文件或目录 -R,-r ...
- EXTJS 4.2 资料 控件之Grid 列鼠标悬停提示
columns: [ { header: }, { header: }, { header: , renderer: function (v, ctx, record) { ctx.tdAttr = ...
- linux ssh 安装、安全设置
环境:ubuntu 12.04 一.简单安装设置 1. 安装ssh 服务器 $ sudo apt-get install openssh 2. 查看运行状态 $ service ssh statu ...