HDOJ4605 Magic Ball Game
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDOJ4605 Magic Ball Game相关的知识,希望对你有一定的参考价值。
思路1:
树状数组+离线处理,对所有的w离散化处理,边dfs边使用树状数组更新左右w的情况。
思路2:
主席树,边bfs边建树。结点信息存储cnt,然后在线查询。
树状数组。
1 /* 4605 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector> 10 #include <deque> 11 #include <algorithm> 12 #include <cstdio> 13 #include <cmath> 14 #include <ctime> 15 #include <cstring> 16 #include <climits> 17 #include <cctype> 18 #include <cassert> 19 #include <functional> 20 #include <iterator> 21 #include <iomanip> 22 using namespace std; 23 //#pragma comment(linker,"/STACK:102400000,1024000") 24 25 #define sti set<int> 26 #define stpii set<pair<int, int> > 27 #define mpii map<int,int> 28 #define vi vector<int> 29 #define pii pair<int,int> 30 #define vpii vector<pair<int,int> > 31 #define rep(i, a, n) for (int i=a;i<n;++i) 32 #define per(i, a, n) for (int i=n-1;i>=a;--i) 33 #define clr clear 34 #define pb push_back 35 #define mp make_pair 36 #define fir first 37 #define sec second 38 #define all(x) (x).begin(),(x).end() 39 #define SZ(x) ((int)(x).size()) 40 #define lson l, mid, rt<<1 41 #define rson mid+1, r, rt<<1|1 42 43 typedef struct ques_t { 44 int w, id; 45 46 ques_t() {} 47 ques_t(int w, int id): 48 w(w), id(id) {} 49 50 } ques_t; 51 52 const int maxn = 1e5+5; 53 const int maxm = maxn * 2; 54 int a[maxm]; 55 int W[maxn]; 56 ques_t Q[maxn]; 57 vector<ques_t> vc[maxn]; 58 int ans[maxn][2]; 59 int son[maxn][2]; 60 int tot[maxm][2]; 61 int n, m; 62 63 int lowest(int x) { 64 return x & -x; 65 } 66 67 void add(int x, int delta, int id) { 68 while (x <= m) { 69 tot[x][id] += delta; 70 x += lowest(x); 71 } 72 } 73 74 int sum(int x, int id) { 75 int ret = 0; 76 77 while (x) { 78 ret += tot[x][id]; 79 x -= lowest(x); 80 } 81 82 return ret; 83 } 84 85 void init() { 86 memset(son, 0, sizeof(son)); 87 memset(tot, 0, sizeof(tot)); 88 rep(i, 1, n+1) 89 vc[i].clr(); 90 m = 1; 91 } 92 93 void dfs(int u, int tot) { 94 int v, sz = SZ(vc[u]); 95 int llt, lgt, rlt, rgt; 96 int eq; 97 98 rep(i, 0, sz) { 99 int w = vc[u][i].w; 100 int id = vc[u][i].id; 101 llt = sum(w-1, 0); 102 lgt = sum(m, 0) - sum(w, 0); 103 rlt = sum(w-1, 1); 104 rgt = sum(m, 1) - sum(w, 1); 105 eq = tot - llt - lgt - rlt - rgt; 106 if (eq) { 107 ans[id][0] = -1; 108 } else { 109 ans[id][0] = (lgt+rgt) + (llt+rlt)*3; 110 ans[id][1] = rlt; 111 } 112 } 113 if (son[u][0]) { 114 add(W[u], 1, 0); 115 dfs(son[u][0], tot+1); 116 add(W[u], -1, 0); 117 118 add(W[u], 1, 1); 119 dfs(son[u][1], tot+1); 120 add(W[u], -1, 1); 121 } 122 } 123 124 void solve() { 125 int q; 126 int x, w; 127 128 scanf("%d", &q); 129 rep(i, 0, q) { 130 scanf("%d %d", &Q[i].id, &Q[i].w); 131 a[m++] = Q[i].w; 132 } 133 134 sort(a+1, a+m); 135 m = unique(a+1, a+m) - (a+1); 136 137 rep(i, 1, n+1) { 138 W[i] = lower_bound(a+1, a+1+m, W[i]) - a; 139 } 140 141 rep(i, 0, q) { 142 int u = Q[i].id; 143 w = lower_bound(a+1, a+1+m, Q[i].w) - a; 144 vc[u].pb(ques_t(w, i)); 145 } 146 147 dfs(1, 0); 148 149 rep(i, 0, q) { 150 if (ans[i][0] == -1) 151 puts("0"); 152 else 153 printf("%d %d\n", ans[i][1], ans[i][0]); 154 } 155 } 156 157 int main() { 158 ios::sync_with_stdio(false); 159 #ifndef ONLINE_JUDGE 160 freopen("data.in", "r", stdin); 161 freopen("data.out", "w", stdout); 162 #endif 163 164 int t; 165 int u, v1, v2; 166 167 scanf("%d", &t); 168 while (t--) { 169 init(); 170 scanf("%d", &n); 171 rep(i, 1, n+1) { 172 scanf("%d", &W[i]); 173 a[m++] = W[i]; 174 } 175 int k; 176 scanf("%d", &k); 177 rep(i, 0, k) { 178 scanf("%d %d %d", &u, &v1, &v2); 179 son[u][0] = v1; 180 son[u][1] = v2; 181 } 182 solve(); 183 } 184 185 #ifndef ONLINE_JUDGE 186 printf("time = %d.\n", (int)clock()); 187 #endif 188 189 return 0; 190 }
主席树。
1 /* 4605 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector> 10 #include <deque> 11 #include <algorithm> 12 #include <cstdio> 13 #include <cmath> 14 #include <ctime> 15 #include <cstring> 16 #include <climits> 17 #include <cctype> 18 #include <cassert> 19 #include <functional> 20 #include <iterator> 21 #include <iomanip> 22 using namespace std; 23 //#pragma comment(linker,"/STACK:102400000,1024000") 24 25 #define sti set<int> 26 #define stpii set<pair<int, int> > 27 #define mpii map<int,int> 28 #define vi vector<int> 29 #define pii pair<int,int> 30 #define vpii vector<pair<int,int> > 31 #define rep(i, a, n) for (int i=a;i<n;++i) 32 #define per(i, a, n) for (int i=n-1;i>=a;--i) 33 #define clr clear 34 #define pb push_back 35 #define mp make_pair 36 #define fir first 37 #define sec second 38 #define all(x) (x).begin(),(x).end() 39 #define SZ(x) ((int)(x).size()) 40 // #define lson l, mid, rt<<1 41 // #define rson mid+1, r, rt<<1|1 42 43 const int INF = 1e9+5; 44 const int maxn = 1e5+5; 45 const int maxm = maxn * 30; 46 int a[maxn], W[maxn]; 47 int T[maxn]; 48 int lson[maxm], rson[maxm]; 49 int lc[maxm], rc[maxm]; 50 int n, m, tot; 51 int son[maxn][2]; 52 53 void init() { 54 tot = m = 0; 55 memset(son, 0, sizeof(son)); 56 } 57 58 int Build(int l, int r) { 59 int rt = tot++; 60 61 lc[rt] = rc[rt] = 0; 62 63 if (l == r) 64 return rt; 65 66 int mid = (l + r) >> 1; 67 68 lson[rt] = Build(l, mid); 69 rson[rt] = Build(mid+1, r); 70 71 return rt; 72 } 73 74 int Insert(int rt, int p, int lval, int rval) { 75 int nrt = tot++, ret = nrt; 76 int l = 0, r = m - 1, mid; 77 78 lc[nrt] = lc[rt] + lval; 79 rc[nrt] = rc[rt] + rval; 80 while (l < r) { 81 mid = (l + r) >> 1; 82 if (p <= mid) { 83 lson[nrt] = tot++; 84 rson[nrt] = rson[rt]; 85 nrt = lson[nrt]; 86 rt = lson[rt]; 87 r = mid; 88 } else { 89 lson[nrt] = lson[rt]; 90 rson[nrt] = tot++; 91 nrt = rson[nrt]; 92 rt = rson[rt]; 93 l = mid + 1; 94 } 95 lc[nrt] = lc[rt] + lval; 96 rc[nrt] = rc[rt] + rval; 97 } 98 99 return ret; 100 } 101 102 pii Query(int rt, int L, int R, int l, int r) { 103 if (L==l && R==r) 104 return mp(lc[rt], rc[rt]); 105 106 int mid = (l + r) >> 1; 107 108 if (R <= mid) { 109 return Query(lson[rt], L, R, l, mid); 110 } else if (L > mid) { 111 return Query(rson[rt], L, R, mid+1, r); 112 } else { 113 pii ltmp = Query(lson[rt], L, mid, l, mid); 114 pii rtmp = Query(rson[rt], mid+1, R, mid+1, r); 115 return mp(ltmp.fir+rtmp.fir, ltmp.sec+rtmp.sec); 116 } 117 } 118 119 void solve() { 120 a[m++] = INF; 121 a[m++] = 0; 122 sort(a, a+m); 123 m = unique(a, a+m) - a; 124 125 T[1] = Build(0, m-1); 126 queue<int> Q; 127 int u, v; 128 129 Q.push(1); 130 while (!Q.empty()) { 131 u = Q.front(); 132 Q.pop(); 133 if (son[u][0]) { 134 int wid = lower_bound(a, a+m, W[u]) - a; 135 T[son[u][0]] = Insert(T[u], wid, 1, 0); 136 T[son[u][1]] = Insert(T[u], wid, 0, 1); 137 Q.push(son[u][0]); 138 Q.push(son[u][1]); 139 } 140 } 141 142 int q; 143 int w; 144 int n2, n7; 145 146 scanf("%d", &q); 147 while (q--) { 148 scanf("%d %d", &v, &w); 149 int wid = lower_bound(a, a+m, w) - a; 150 if (a[wid] == w) { 151 // check hit w 152 pii eq = Query(T[v], wid, wid, 0, m-1); 153 if (eq.fir+eq.sec > 0) { 154 puts("0"); 155 continue; 156 } 157 } 158 159 pii lt = Query(T[v], 0, wid-1, 0, m - 1); 160 pii gt = Query(T[v], wid, m-1, 0, m - 1); 161 162 n2 = (gt.fir+gt.sec) + (lt.fir+lt.sec) * 3; 163 n7 = lt.sec; 164 165 printf("%d %d\n", n7, n2); 166 } 167 } 168 169 int main() { 170 ios::sync_with_stdio(false); 171 #ifndef ONLINE_JUDGE 172 freopen("data.in", "r", stdin); 173 freopen("data.out", "w", stdout); 174 #endif 175 176 int t; 177 int u; 178 179 scanf("%d", &t); 180 rep(tt, 1, t+1) { 181 scanf("%d", &n); 182 init(); 183 rep(i, 1, n+1) { 184 scanf("%d", &W[i]); 185 a[m++] = W[i]; 186 } 187 int k; 188 scanf("%d", &k); 189 rep(i, 0, k) { 190 scanf("%d", &u); 191 scanf("%d %d", &son[u][0], &son[u][1]); 192 } 193 solve(); 194 } 195 196 #ifndef ONLINE_JUDGE 197 printf("time = %d.\n", (int)clock()); 198 #endif 199 200 return 0; 201 }
数据发生器。
1 from copy import deepcopy 2 from random import randint, shuffle 3 import shutil 4 import string 5 6 7 def GenDataIn(): 8 with open("data.in", "w") as fout: 9 t = 1 10 bound = 10**5 / 2 11 fout.write("%d\n" % (t)) 12 for tt in xrange(t): 13 m = randint(100, 200) 14 n = 2 * m + 1 15 fout.write("%d\n" % (n)) 16 L = [] 17 for i in xrange(n): 18 w = randint(1, bound) 19 L.append(w) 20 fout.write(" ".join(map(str, L)) + "\n") 21 ust = [1] 22 vst = range(2, n+1) 23 fout.write("%d\n" % (m)) 24 for i in xrange(m): 25 idx = randint(0, len(ust)-1) 26 u = ust[idx] 27 ust.remove(u) 28 V = [] 29 for j in xrange(2): 30 idx = randint(0, len(vst)-1) 31 v = vst[idx] 32 V.append(v) 33 ust.append(v) 34 vst.remove(v) 35 fout.write("%d %d %d\n" % (u, V[0], V[1])) 36 q = randint(n/2, n) 37 fout.write("%d\n" % (q)) 38 for i in xrange(q): 39 x = randint(1, n) 40 w = randint(1, bound) 41 fout.write("%d %d\n" % (x, w)) 42 43 44 45 def MovDataIn(): 46 desFileName = "F:\eclipse_prj\workspace\hdoj\data.in" 47 shutil.copyfile("data.in", desFileName) 48 49 50 if __name__ == "__main__": 51 GenDataIn() 52 MovDataIn()
以上是关于HDOJ4605 Magic Ball Game的主要内容,如果未能解决你的问题,请参考以下文章
hdu 4605 Magic Ball Game (在线主席树/离线树状数组)