对莫队算法做一个总结
然后介绍一些常用的数据结构
比如splay-tree, kd-tree等等

莫队算法综合题

WC2013糖果公园

需要注意的是,dfs\text{dfs} 序,莫队数组必须开 maxn×2\text{maxn} \times 2 大小

WC2013

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

const int maxn = 200000 + 10;

// == Graph structure ==
int head[maxn], nxt[maxn], ver[maxn];
int tot = 0;

void initG() {
Set(head, 0);
Set(nxt, 0);
Set(ver, 0);
tot = 0;
}
void add(int x, int y) {
ver[++tot] = y;
nxt[tot] = head[x];
head[x] = tot;
}
// == Graph structure finished ==

// == get input ==
int V[maxn], W[maxn], C[maxn];
int cnt[maxn];
int n, m, q;

void init() {
Set(cnt, 0);
}
void getinp() {
scanf("%d%d%d", &n, &m, &q);
_rep(i, 1, m) scanf("%d", &V[i]);
_rep(i, 1, n) scanf("%d", &W[i]);
_for(i, 1, n) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}

_rep(i, 1, n) scanf("%d", &C[i]);
}
// == input finished ==

// == get dfs order and lca ==
int fst[maxn], lst[maxn], dep[maxn], ord[maxn];
int f[maxn][30];
const int H = 25;
int dfn = 0;

void initdfs() {
Set(dep, 0);
Set(f, 0);
dep[1] = 1;
dfn = 0;
}

void dfs(int x) {
assert(dep[1] == 1);

ord[++dfn] = x;
fst[x] = dfn;

for(int i = head[x]; i; i = nxt[i]) {
int y = ver[i];
if(dep[y]) continue;

dep[y] = dep[x] + 1;
f[y][0] = x;
_rep(k, 1, H) f[y][k] = f[f[y][k - 1]][k - 1];
dfs(y);
}

ord[++dfn] = x;
lst[x] = dfn;
}

int getlca(int x, int y) {
if(dep[x] > dep[y]) swap(x, y);
_forDown(i, H, 0) if(dep[f[y][i]] >= dep[x]) y = f[y][i];

if(x == y) return x;

_forDown(i, H, 0) if(f[y][i] != f[x][i]) {
x = f[x][i], y = f[y][i];
}

return f[x][0];
}
// == dfs order and lca finished ==

// == get qry and change ==
class Qry {
public:
int l, r, id, time, lca;
};
Qry qry[maxn];

class Ch {
public:
int val, pos;
};
Ch ch[maxn];

int qn = 0, cn = 0;
void getinp2() {
_rep(i, 1, q) {
int op, x, y;
scanf("%d%d%d", &op, &x, &y);
if(op) {
int lca = getlca(x, y);
qry[++qn].time = cn;
qry[qn].id = qn;

if(fst[x] > fst[y]) swap(x, y);
if(x == lca) {
qry[qn].l = fst[x];
qry[qn].r = fst[y];
}
else {
qry[qn].l = lst[x];
qry[qn].r = fst[y];
qry[qn].lca = lca;
}
}
else {
ch[++cn].pos = x;
ch[cn].val = y;
}
}
}
// == qry and change finished ==

// == block ==
int belong[maxn];
int sz, t;

void block() {
sz = sqrt(dfn);
t = dfn / sz;

_rep(i, 1, t) _rep(k, (i - 1) * sz + 1, i * sz) belong[k] = i;

if(t * sz < dfn) {
t++;
_rep(k, (t - 1) * sz + 1, dfn) belong[k] = t;
}
}

int cmp(const Qry& a, const Qry& b) {
if(belong[a.l] ^ belong[b.l]) return belong[a.l] < belong[b.l];
if(belong[a.l] ^ belong[b.r]) return belong[a.r] < belong[b.r];
return a.time < b.time;
}
// == block finsihed ==

// == Mo algorithm ==
int visMo[maxn];
void push(int x, llong& ans) {
ans += (llong)1 * V[C[x]] * W[++cnt[C[x]]];
}
void del(int x, llong& ans) {
ans -= (llong)1 * V[C[x]] * W[cnt[C[x]]--];
}

void work(int x, llong& ans) {
visMo[x] ? del(x, ans) : push(x, ans);
visMo[x] ^= 1;
}

void apply(int t, llong& ans) {
if(visMo[ch[t].pos]) {
work(ch[t].pos, ans);
swap(ch[t].val, C[ch[t].pos]);
assert(visMo[ch[t].pos] == 0);
work(ch[t].pos, ans);
}
else swap(ch[t].val, C[ch[t].pos]);
}

llong ANS[maxn];

void solve() {
sort(qry + 1, qry + 1 + qn, cmp);
int l = 1, r = 0, ptime = 0;
llong ans = 0;

_rep(i, 1, qn) {
int ql = qry[i].l, qr = qry[i].r, qt = qry[i].time, qlca = qry[i].lca;

while (l < ql) work(ord[l++], ans);
while (l > ql) work(ord[--l], ans);
while (r < qr) work(ord[++r], ans);
while (r > qr) work(ord[r--], ans);

while (ptime < qt) apply(++ptime, ans);
while (ptime > qt) apply(ptime--, ans);

if(qlca) work(qlca, ans);
ANS[qry[i].id] = ans;
if(qlca) work(qlca, ans);
}
}
// == Mo algorithm finished ==

int main() {
freopen("input.txt", "r", stdin);
initG();
init();

getinp();

initdfs();
dfs(1);


getinp2();
block();
solve();

_rep(i, 1, qn) printf("%lld\n", ANS[i]);
}

数据结构优化计算几何

GYM100729I

GYM100729I

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const int inf = 0x3f3f3f3f;
const int maxn = 15;
int ns, nw, np, r;

// == Point structure ==
class Point {
public:
int x, y;
Point(int x = 0, int y = 0) : x(x), y(y) {}

Point operator+ (const Point& rhs) const {
return Point(x + rhs.x, y + rhs.y);
}

Point operator- (const Point& rhs) const {
return Point(x - rhs.x, y - rhs.y);
}

bool operator== (const Point& rhs) const {
return x == rhs.x && y == rhs.y;
}

bool operator< (const Point& rhs) const {
return x < rhs.x || (x == rhs.x && y < rhs.y);
}
};

typedef Point Vector;

llong Cross(const Vector& A, const Vector& B) {
return (llong)A.x * B.y - (llong)A.y * B.x;
}
llong Length2(const Vector& A) {
return (llong)A.x * A.x + (llong)A.y * A.y;
}

Point readPoint() {
int x, y;
scanf("%d%d", &x, &y);
return Point(x, y);
}
// == Point structure finished ==

// == Segment Structure ==
class Line {
public:
Point a, b;
};

Line walls[maxn];

bool segmentProperIntersection(const Point& a, const Point& b, const Point& c, const Point& d) {
if(max(a.x, b.x) < min(c.x, d.x)
|| max(a.y, b.y) < min(c.y, d.y)
|| min(a.x, b.x) > max(c.x, d.x)
|| min(a.y, b.y) > max(c.y, d.y))
return 0;

llong v1 = Cross(b - a, c - a), v2 = Cross(b - a, d - a);
llong v3 = Cross(d - c, a - c), v4 = Cross(d - c, b - c);

return v1 * v2 <= 0 && v3 * v4 <= 0;
}
// == Segment Structure finished ==


set<Point> sen;
vector<Point> ANS;
// == solve the problem ==
int check(Point p, Vector v) {
Point q = p + v;
if(!sen.count(q)) return 0;

int cnt = 0;
_for(i, 0, nw) {
if(segmentProperIntersection(walls[i].a, walls[i].b, p, q)) cnt++;
}

return cnt <= r && Length2(v) <= (llong)(r - cnt) * (r - cnt);
}

void solve(const Point& p) {
_rep(dx, -r, r) _rep(dy, -r, r) {
if(Length2(Vector(dx, dy)) > r * r) continue;
if(check(p, Vector(dx, dy))) ANS.push_back(p + Vector(dx, dy));
}
}
// == solve finsihed ==

void init() {
sen.clear();
}

int main() {
freopen("input.txt", "r", stdin);
int T;
scanf("%d", &T);

while (T--) {
scanf("%d%d%d%d", &ns, &r, &nw, &np);
init();

_for(i, 0, ns) {
sen.insert(readPoint());
}

_for(i, 0, nw) {
walls[i].a = readPoint();
walls[i].b = readPoint();
}

// == input finished ==

// == then query for the products ==
while (np--) {
ANS.clear();
Point p = readPoint();

solve(p);

printf("%d", (int)ANS.size());
sort(ANS.begin(), ANS.end());
for(auto e : ANS) printf(" (%d,%d)", e.x, e.y);
printf("\n");
}
}
}