-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
301 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using ll = long long int; | ||
|
||
char query(int a, int b, int c, int d) { | ||
std::cout << "? " << a << ' ' << b << ' ' << c << ' ' << d << '\n'; | ||
std::cout.flush(); | ||
|
||
std::string s; | ||
std::cin >> s; | ||
return s[0]; | ||
} | ||
|
||
void answer(int x, int y) { | ||
std::cout << "! " << x << ' ' << y << '\n'; | ||
std::cout.flush(); | ||
} | ||
|
||
void solve() { | ||
int n; | ||
std::cin >> n; | ||
|
||
int max = 0; | ||
for (int i = 1; i < n; ++i) { | ||
if (query(max, max, i, i) == '<') { | ||
max = i; | ||
} | ||
} | ||
|
||
int p = 0; | ||
std::vector<int> v {p}; | ||
for (int i = 1; i < n; ++i) { | ||
char t = query(max, p, max, i); | ||
if (t == '<') { | ||
p = i; | ||
v = {i}; | ||
} else if (t == '=') { | ||
v.push_back(i); | ||
} | ||
} | ||
|
||
int min = v[0]; | ||
for (int i = 1; i < v.size(); ++i) { | ||
if (query(min, min, v[i], v[i]) == '>') { | ||
min = v[i]; | ||
} | ||
} | ||
|
||
answer(max, min); | ||
} | ||
|
||
int main() { | ||
std::ios::sync_with_stdio(false); | ||
std::cin.tie(nullptr); | ||
|
||
int T; | ||
std::cin >> T; | ||
|
||
while (T--) { | ||
solve(); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using ll = long long int; | ||
|
||
const int MAXN = 1e6 + 19; | ||
|
||
int n; | ||
std::string s; | ||
|
||
struct BIT { | ||
ll tr[MAXN]; | ||
ll query(int x) { | ||
x += 1; | ||
|
||
ll res = 0; | ||
for (; x; x -= x & -x) { | ||
res += tr[x]; | ||
} | ||
return res; | ||
} | ||
ll query(int l, int r) { | ||
return query(r) - query(l - 1); | ||
} | ||
void update(int x, int k) { | ||
x += 1; | ||
|
||
for (; x <= n + 10; x += x & -x) { | ||
tr[x] += k; | ||
} | ||
} | ||
} ml, mr; | ||
|
||
int qL[MAXN], Ll, Lr; | ||
int qR[MAXN], Rl, Rr; | ||
|
||
void solve() { | ||
|
||
std::cin >> n; | ||
std::cin >> s; | ||
s = '>' + s + '<'; | ||
|
||
Ll = 1, Lr = 0; | ||
Rl = 1, Rr = 0; | ||
for (int i = 0; i <= n + 1; ++i) { | ||
if (s[i] == '<') { | ||
qR[++Rr] = i; | ||
} | ||
|
||
if (s[i] == '>') { | ||
ml.update(i, i); | ||
} else { | ||
mr.update(i, i); | ||
} | ||
} | ||
|
||
qL[++Lr] = 0; | ||
for (int i = 1; i <= n; ++i) { | ||
if (s[i] == '<') { | ||
++Rl; | ||
} | ||
|
||
int szL = Lr - Ll + 1, szR = Rr - Rl + 1; | ||
ll ans = 0; | ||
if (s[i] == '<') { | ||
if (szL <= szR) { | ||
ll L = ml.query(qL[Ll], qL[Lr]); | ||
ll R = 0; | ||
if (szL >= 2) { | ||
R = mr.query(qR[Rl], qR[Rl + szL - 2]); | ||
} | ||
ans = R * 2 + i - L * 2; | ||
} else { | ||
ll L = 0; | ||
ll R = mr.query(qR[Rl], qR[Rr]); | ||
if (szR >= 1) { | ||
L = ml.query(qL[Lr - szR + 1], qL[Lr]); | ||
} | ||
ans = R * 2 + i - L * 2 - (n + 1); | ||
} | ||
} else { | ||
if (szR <= szL) { | ||
ll L = 0; | ||
ll R = mr.query(qR[Rl], qR[Rr]); | ||
if (szR >= 2) { | ||
L = ml.query(qL[Lr - szR + 2], qL[Lr]); | ||
} | ||
ans = R * 2 - i - L * 2 - (n + 1); | ||
} else { | ||
ll L = ml.query(qL[Ll], qL[Lr]); | ||
ll R = 0; | ||
if (szL >= 1) { | ||
R = mr.query(qR[Rl], qR[Rl + szL - 1]); | ||
} | ||
ans = R * 2 - i - L * 2; | ||
} | ||
} | ||
|
||
std::cout << ans << ' '; | ||
|
||
if (s[i] == '>') { | ||
qL[++Lr] = i; | ||
} | ||
} | ||
std::cout << '\n'; | ||
|
||
for (int i = 1; i <= n + 10; ++i) { | ||
ml.tr[i] = 0; | ||
mr.tr[i] = 0; | ||
} | ||
} | ||
|
||
int main() { | ||
std::ios::sync_with_stdio(false); | ||
std::cin.tie(nullptr); | ||
|
||
int T; | ||
std::cin >> T; | ||
|
||
while (T--) { | ||
solve(); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using ll = long long int; | ||
|
||
const int MAXN = 1e6 + 19; | ||
|
||
bool comp(const std::vector<int> &a, const std::vector<int> &b) { | ||
for (int i = 0; i < a.size(); ++i) { | ||
if (a[i] <= b[i]) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
void solve() { | ||
int n, m; | ||
std::cin >> n >> m; | ||
|
||
std::vector<int> c(n + 1); | ||
for (int i = 1; i <= n; ++i) { | ||
std::cin >> c[i]; | ||
} | ||
|
||
std::vector<std::vector<int>> a(n + 1); | ||
for (int i = 1; i <= n; ++i) { | ||
a[i].resize(m + 1); | ||
for (int j = 1; j <= m; ++j) { | ||
std::cin >> a[i][j]; | ||
} | ||
} | ||
|
||
std::vector<std::vector<std::pair<int, int>>> G(n * 2 + n * m + 1); | ||
auto add = [&](int u, int v, int w) { | ||
G[u].emplace_back(v, w); | ||
}; | ||
auto dijkstra = [&](auto &G, int src, int t) -> ll { | ||
std::vector<ll> dist(G.size(), ll(1e18)); | ||
std::vector<uint8_t> vist(G.size(), false); | ||
dist[src] = 0; | ||
|
||
std::priority_queue<std::pair<ll, int>> q; | ||
q.emplace(0LL, src); | ||
|
||
while (!q.empty()) { | ||
auto [_, node] = q.top(); | ||
q.pop(); | ||
if (vist[node]) { | ||
continue; | ||
} | ||
vist[node] = true; | ||
|
||
for (auto [to, w] : G[node]) { | ||
if (dist[to] > dist[node] + w) { | ||
dist[to] = dist[node] + w; | ||
q.emplace(-dist[to], to); | ||
} | ||
} | ||
} | ||
|
||
return dist[t]; | ||
}; | ||
|
||
for (int i = 1; i <= n; ++i) { | ||
add(i, i + n, c[i]); | ||
} | ||
|
||
for (int b = 1; b <= m; ++b) { | ||
std::vector<int> id; | ||
for (int i = 1; i <= n; ++i) { | ||
id.push_back(i); | ||
} | ||
std::sort(id.begin(), id.end(), [&](int x, int y) { | ||
return a[x][b] < a[y][b]; | ||
}); | ||
|
||
int d = n * 2 + n * (b - 1); | ||
std::vector<int> v; | ||
for (int l = 0, r; l < n; l = r + 1) { | ||
r = l; | ||
while (r + 1 < n && a[id[r]][b] == a[id[r + 1]][b]) { | ||
++r; | ||
} | ||
for (int i = l; i <= r; ++i) { | ||
add(id[l] + d, id[i], 0); | ||
add(id[i] + n, id[l] + d, 0); | ||
} | ||
|
||
v.push_back(id[l]); | ||
} | ||
|
||
for (int i = 0; i + 1 < v.size(); ++i) { | ||
add(v[i] + d, v[i + 1] + d, 0); | ||
add(v[i + 1] + d, v[i] + d, a[v[i + 1]][b] - a[v[i]][b]); | ||
} | ||
} | ||
|
||
std::cout << dijkstra(G, 1 + n, n + n) << '\n'; | ||
} | ||
|
||
int main() { | ||
std::ios::sync_with_stdio(false); | ||
std::cin.tie(nullptr); | ||
|
||
int T; | ||
std::cin >> T; | ||
|
||
while (T--) { | ||
solve(); | ||
} | ||
|
||
return 0; | ||
} |