QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#133677 | #4940. Token Distance | Team_name# | WA | 75ms | 14520kb | C++20 | 5.0kb | 2023-08-02 12:50:36 | 2023-08-02 12:50:39 |
Judging History
answer
#include <algorithm>
#include <iostream>
#include <numeric>
#include <set>
using PII = std::pair<int, int>;
constexpr int NN = 1E5;
constexpr int SEG = 4 * NN, inf = 1E9;
int N;
int T[NN + 1];
int dT[NN + 1];
std::set<int, decltype([](int i, int j) { return T[i] < T[j] || T[i] == T[j] && i < j; })> s;
int prev[NN + 1];
void get_prev(int X)
{
if (auto it = s.lower_bound(X); it != s.begin() && T[*--it] == T[X])
prev[X] = *it;
else
prev[X] = 0;
}
void chk_min(int& a, int b) { if (a > b) a = b; }
void chk_max(int& a, int b) { if (a < b) a = b; }
namespace seg0
{
int min[SEG], max[SEG];
int p;
int l_, r_;
void update(int u)
{
min[u] = std::min(min[u << 1], min[u << 1 | 1]);
max[u] = std::max(max[u << 1], max[u << 1 | 1]);
}
void build(int u, int l, int r)
{
if (l == r - 1)
{
min[u] = T[l];
max[u] = T[l];
return;
}
int m = (l + r) / 2;
build(u << 1, l, m);
build(u << 1 | 1, m, r);
update(u);
}
void modify(int u, int l, int r)
{
if (l == r - 1)
{
min[u] = T[l];
max[u] = T[l];
return;
}
if (int m = (l + r) / 2; p < m)
modify(u << 1, l, m);
else
modify(u << 1 | 1, m, r);
update(u);
}
void modify(int p_)
{
p = p_;
modify(1, 1, N + 1);
}
PII query(int u, int l, int r)
{
if (l_ <= l && r <= r_)
return {min[u], max[u]};
int m = (l + r) / 2;
int res_min = inf, res_max = 0;
if (l_ < m)
{
auto [ret_min, ret_max] = query(u << 1, l, m);
chk_min(res_min, ret_min);
chk_max(res_max, ret_max);
}
if (m < r_)
{
auto [ret_min, ret_max] = query(u << 1 | 1, m, r);
chk_min(res_min, ret_min);
chk_max(res_max, ret_max);
}
return {res_min, res_max};
}
PII query(int l, int r)
{
l_ = l, r_ = r + 1;
return query(1, 1, N + 1);
}
}
namespace seg1
{
int GCD[SEG];
int p;
int l_, r_;
void update(int u)
{
GCD[u] = std::gcd(GCD[u << 1], GCD[u << 1 | 1]);
}
void build(int u, int l, int r)
{
if (l == r - 1)
{
GCD[u] = std::abs(dT[l]);
return;
}
int m = (l + r) / 2;
build(u << 1, l, m);
build(u << 1 | 1, m, r);
update(u);
}
void modify(int u, int l, int r)
{
if (l == r - 1)
{
GCD[u] = std::abs(dT[l]);
return;
}
if (int m = (l + r) / 2; p < m)
modify(u << 1, l, m);
else
modify(u << 1 | 1, m, r);
update(u);
}
void modify(int p_)
{
p = p_;
modify(1, 2, N + 1);
}
int query(int u, int l, int r)
{
if (l_ <= l && r <= r_)
return GCD[u];
int m = (l + r) / 2;
int res = 0;
if (l_ < m)
res = std::gcd(res, query(u << 1, l, m));
if (m < r_)
res = std::gcd(res, query(u << 1 | 1, m, r));
return res;
}
int query(int l, int r)
{
l_ = l, r_ = r + 1;
return query(1, 2, N + 1);
}
}
namespace seg2
{
int max[SEG];
int p;
int l_, r_;
void update(int u)
{
max[u] = std::max(max[u << 1], max[u << 1 | 1]);
}
void build(int u, int l, int r)
{
if (l == r - 1)
{
max[u] = prev[l];
return;
}
int m = (l + r) / 2;
build(u << 1, l, m);
build(u << 1 | 1, m, r);
update(u);
}
void modify(int u, int l, int r)
{
if (l == r - 1)
{
max[u] = prev[l];
return;
}
if (int m = (l + r) / 2; p < m)
modify(u << 1, l, m);
else
modify(u << 1 | 1, m, r);
update(u);
}
void modify(int p_)
{
p = p_;
modify(1, 1, N + 1);
}
int query(int u, int l, int r)
{
if (l_ <= l && r <= r_)
return max[u];
int m = (l + r) / 2;
int res = 0;
if (l_ < m)
res = std::max(res, query(u << 1, l, m));
if (m < r_)
res = std::max(res, query(u << 1 | 1, m, r));
return res;
}
int query(int l, int r)
{
l_ = l, r_ = r + 1;
return query(1, 1, N + 1);
}
}
int main()
{
std::ios_base::sync_with_stdio(false), std::cin.tie(nullptr);
int Q; std::cin >> N >> Q;
for (int i = 1; i <= N; ++i)
std::cin >> T[i];
seg0::build(1, 1, N + 1);
for (int i = 1; i <= N; ++i)
dT[i] = T[i] - T[i - 1];
seg1::build(1, 2, N + 1);
for (int i = 1; i <= N; ++i)
s.insert(i);
for (int i = 1; i <= N; ++i)
get_prev(i);
seg2::build(1, 1, N + 1);
while (Q--)
{
int op; std::cin >> op;
if (op == 1)
{
int X, Y; std::cin >> X >> Y;
int todo = 0;
if (auto it = s.upper_bound(X); it != s.end() && T[X] == T[*it])
todo = *it;
s.erase(X);
T[X] = Y;
s.insert(X);
get_prev(X);
seg2::modify(X);
if (todo)
{
get_prev(todo);
seg2::modify(todo);
}
seg0::modify(X);
dT[X] = T[X] - T[X - 1];
seg1::modify(X);
if (X < N)
{
dT[X + 1] = T[X + 1] - T[X];
seg1::modify(X + 1);
}
}
else
{
int L, R; std::cin >> L >> R;
auto [min, max] = seg0::query(L, R);
int GCD = seg1::query(L + 1, R);
bool distinct = seg2::query(L, R) < L;
std::cerr << min << " " << max << " " << GCD << " " << distinct << std::endl;
if (min == max)
std::cout << "YES\n";
else
if (distinct && max - min == (R - L) * GCD)
std::cout << "YES\n";
else
std::cout << "NO\n";
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9648kb
input:
5 7 1 1 1 10 1 2 1 3 2 1 5 1 5 4 1 3 7 2 2 4 2 2 5 2 4 5
output:
YES NO NO YES YES
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 4ms
memory: 9636kb
input:
2 1 0 1000000000 2 1 2
output:
YES
result:
ok single line: 'YES'
Test #3:
score: -100
Wrong Answer
time: 75ms
memory: 14520kb
input:
81473 13549 972586683 972586964 972587245 972587526 972587807 972588088 972588369 972588650 972588931 972589212 972589493 972589774 972590055 972590336 972590617 972590898 972591179 972591460 972591741 972592022 972592303 972592584 972592865 972593146 972593427 972593708 972593989 972594270 97259455...
output:
YES YES YES YES NO YES YES YES YES YES YES YES YES YES NO YES YES YES YES YES YES YES YES YES YES YES NO YES YES YES NO YES YES YES YES YES NO YES NO YES YES YES YES YES YES YES NO YES NO YES YES YES YES YES NO YES NO YES NO YES YES YES YES YES YES YES YES YES YES YES YES YES NO YES YES YES YES YES ...
result:
wrong answer 25th lines differ - expected: 'NO', found: 'YES'