CF_#497(Div. 1)

题目传送门

C. Guess two numbers

考虑类似二分的方法
因为可行解区域是一个$L$型
按面积考虑二分位置即可

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
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
int query(LL x, LL y) {
printf("%lld %lld\n", x, y);
fflush(stdout);
int u;
scanf("%d", &u);
if(u == 0) exit(0);
return u;
}
void solve(LL xl, LL xm, LL xr, LL yl, LL ym, LL yr) {
if(xm <= xl) xm = xr, yr = ym;
if(ym <= yl) ym = yr, xr = xm;
long double sa = (long double)(xm - xl) * (ym - yl);
long double sb = (long double)(xm - xl) * (yr - ym);
long double sc = (long double)(xr - xm) * (ym - yl);
LL mx, my;
if(sa + sc > sb && sa + sb > sc) mx = (xl + xm) >> 1, my = (yl + ym) >> 1;
else if(sb > sa + sc) mx = (xl + xm) >> 1, my = ym;
else mx = xm, my = (yl + ym) >> 1;
int d = query(mx, my);
if(d == 1) solve(max(xl, mx + 1), xm, xr, yl, ym, yr);
else if(d == 2) solve(xl, xm, xr, max(yl, my + 1), ym, yr);
else solve(xl, min(mx, xm), xr, yl, min(my, ym), yr);
}
int main() {
#ifdef CX_TEST
freopen("E:\\program--GG\\test_in.txt", "r", stdin);
#endif
LL n;
scanf("%lld", &n);
solve(1, n + 1, n + 1, 1, n + 1, n + 1);
return 0;
}

D. Ants

标准$2-sat$,利用线段树优化建图即可

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
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
vector<int> g[maxn], f[maxn << 2], g2[maxn * 50];
int sz[maxn], fa[maxn], dep[maxn], son[maxn];
int top[maxn], dfn[maxn];
int fl[maxn << 2], fr[maxn << 2];
int cnt = 0, tot;
void dfs1(int u, int father) {
dep[u] = dep[father] + 1;
fa[u] = father;
sz[u] = 1;
son[u] = -1;
for(auto v : g[u]) {
if(v == father) continue;
dfs1(v, u);
sz[u] += sz[v];
if(son[u] == -1 || sz[v] > sz[son[u]]) son[u] = v;
}
}
void dfs2(int u, int t) {
top[u] = t;
dfn[u] = ++cnt;
if(son[u] == -1) return;
dfs2(son[u], t);
for(auto v : g[u]) {
if(v == son[u] || v == fa[u]) continue;
dfs2(v, v);
}
}
void update(int t, int l, int r, int ll, int rr, int x) {
if(ll <= l && r <= rr) {
f[t].push_back(x);
return;
}
int mid = (l + r) >> 1;
if(ll <= mid) update(t << 1, l, mid, ll, rr, x);
if(rr > mid) update(t << 1 | 1, mid + 1, r, ll, rr, x);
}
void fset(int u, int v, int k) {
int x = top[u], y = top[v];
while(x != y) {
int &w = dep[x] > dep[y] ? u : v;
int &z = dep[x] > dep[y] ? x : y;
update(1, 1, cnt, dfn[z], dfn[w], k);
w = fa[z];
z = top[w];
}
if(u == v) return;
if(dfn[u] > dfn[v]) swap(u, v);
update(1, 1, cnt, dfn[u] + 1, dfn[v], k);
}
int rev(int x) {
if(x & 1) return x - 1;
else return x + 1;
}
void add_edge(int x, int y) {
g2[x].push_back(y);
g2[rev(y)].push_back(rev(x));
}
void build(int t, int l, int r) {
fl[t] = tot++;
int pre = fl[t];
for(auto e : f[t]) {
add_edge(pre << 1, rev(e));
add_edge(pre << 1, tot << 1);
add_edge(e, tot << 1);
pre = tot++;
}
fr[t] = pre;
if(t > 1) add_edge(fr[t >> 1] << 1, fl[t] << 1);
if(l == r) return;
int mid = (l + r) >> 1;
build(t << 1, l, mid);
build(t << 1 | 1, mid + 1, r);
}
int pre[maxn * 50], low[maxn * 50], scc[maxn * 50];
int dfs_t, scc_cnt;
stack<int> ST;
void dfs(int u) {
pre[u] = low[u] = ++dfs_t;
ST.push(u);
for(auto v : g2[u]) {
if(!pre[v]) {
dfs(v);
low[u] = min(low[u], low[v]);
} else if(!scc[v]) {
low[u] = min(low[u], pre[v]);
}
}
if(low[u] == pre[u]) {
scc_cnt++;
while(!ST.empty()) {
int x = ST.top();
ST.pop();
scc[x] = scc_cnt;
if(x == u) break;
}
}
}
vector<int>g3[maxn * 50];
int l[maxn * 50], q[maxn * 50], pc[maxn * 50];
void Shrink_Point(int n) {
for(int u = 0; u < n; u++) {
for(auto v : g2[u]) {
if(scc[u] != scc[v]) {
g3[scc[v]].push_back(scc[u]);
l[scc[u]]++;
}
}
}
}
void dfsc(int u) {
if(pc[u]) return;
pc[u] = 2;
for(auto v : g3[u]) dfsc(v);
}
bool find_scc(int n) {
dfs_t = scc_cnt = 0;
for(int i = 0; i < n; i++) {
if(!pre[i]) dfs(i);
}
for(int i = 0; i < n; i += 2) {
if(scc[i] == scc[i + 1]) return false;
}
Shrink_Point(n);
int mm = 0;
for(int i = 1; i <= scc_cnt; i++) {
if(l[i] == 0) q[mm++] = i;
}
for(int i = 0; i < mm; i++) {
for(auto v : g3[q[i]]) {
l[v]--;
if(l[v] == 0) q[mm++] = v;
}
}
for(int i = 0; i < n; i += 2) {
l[scc[i]] = scc[i + 1];
l[scc[i + 1]] = scc[i];
}
for(int i = 0; i < mm; i++) {
if(pc[q[i]]) continue;
pc[q[i]] = 1;
dfsc(l[q[i]]);
}
return true;
}
int main() {
#ifdef CX_TEST
freopen("E:\\program--GG\\test_in.txt", "r", stdin);
#endif
int n, m, i, u, v;
scanf("%d", &n);
for(i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs1(1, 0);
dfs2(1, 1);
scanf("%d", &m);
for(i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
fset(u, v, i << 1);
scanf("%d%d", &u, &v);
fset(u, v, i << 1 | 1);
}
tot = m;
build(1, 1, cnt);
if(!find_scc(tot * 2)) puts("NO");
else {
puts("YES");
for(i = 0; i < m; i++) printf("%d\n", pc[scc[i << 1]]);
}
return 0;
}