【HDOJ】4605 Magic Ball Game

时间:2022-09-17 19:23:42

思路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的更多相关文章

  1. hdu 4605 Magic Ball Game

    http://acm.hdu.edu.cn/showproblem.php?pid=4605 可以离线求解 把所以可能出现的 magic ball  放在一个数组里(去重),从小到大排列 先不考虑特殊 ...

  2. HDU 4605 Magic Ball Game(可持续化线段树,树状数组,离散化)

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. 【HDOJ】3183 A Magic Lamp

    RMQ. /* 3183 */ #include <cstdio> #include <cstring> #include <cstdlib> #define MA ...

  4. 【HDOJ】1556 Color the ball

    简单线段树. #include <stdio.h> #define MAXN 100005 #define lson l, mid, rt<<1 #define rson mi ...

  5. 【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- ...

  6. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  7. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  8. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  9. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

随机推荐

  1. 【BZOJ】4001&colon; &lbrack;TJOI2015&rsqb;概率论

    题意 求节点数为\(n\)的有根树期望的叶子结点数.(\(n \le 10^9\)) 分析 神题就打表找规律.. 题解 方案数就是卡特兰数,$h_0=1, h_n = \sum_{i=0}^{n-1} ...

  2. Apache禁止目录访问的方法

    在学习ThinkPHP(3.2.3)的时候,公共文件夹.应用目录文件夹等都自带或者自动生成index.html的安全文件.但是ThinkPHP文件夹(核心包)却没有这样的设置.那么ThinkPHP核心 ...

  3. Linux时间子系统之六:高精度定时器(HRTIMER)的原理和实现

    转自:http://blog.csdn.net/droidphone/article/details/8074892 上一篇文章,我介绍了传统的低分辨率定时器的实现原理.而随着内核的不断演进,大牛们已 ...

  4. cisco 路由配置

    Cisco路由配置基础 刚刚接触cisco路由配置,下面是学习的笔记,感觉命令还是多敲才能熟悉 一. 所处状态各类 router> 用户处于用户命令状态,可以查看网络和主机 router# 用户 ...

  5. 如何更新Linux源

    首先需要自己收藏几个可以得到Linux源的站点,比如:http://mirrors.163.com/ (163的镜像站):可以百度搜索[Linux镜像站]: 下面这些镜像站,转自:http://www ...

  6. 连载:面向对象葵花宝典:思想、技巧与实践(33) - ISP原则

     ISP,Interface Segregation Principle,中文翻译为"接口隔离原则". 和DIP原则一样,ISP原则也是大名鼎鼎的Martin大师提出来的,他在19 ...

  7. 【BZOJ 2673】&lbrack;Wf2011&rsqb;Chips Challenge

    题目大意: 传送门 $n*n$的棋盘,有一些位置可以放棋子,有一些已经放了棋子,有一些什么都没有,也不能放,要求放置以后满足:第i行和第i列的棋子数相同,同时每行的棋子数占总数比例小于$\frac{A ...

  8. &lbrack;转&rsqb;usdt omnicore testnet 测试网络

    本文转自:https://www.jianshu.com/p/417c280b8f9f Testnet 模式允许 omni core 运行在比特币测试链上,用于安全测试. 为了在 testnet 上收 ...

  9. Eclipse Indigo 3&period;7&period;0 安装GIT插件提示 requires &amp&semi;&num;39&semi;bundle org&period;eclipse&period;team&period;core&lpar;转&rpar;

    错误提示: Cannot complete the install because one or more required items could not be found.Software bei ...

  10. github上手实践教程

    简介: SSH公私钥的使用 github的使用 git 工具的基本使用 基本步骤: 一.github的使用 1.github账号的创建[官网一步一步创建就行了,这一步骤省略] 2.创建远程仓库: 创建 ...