QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#133654 | #4940. Token Distance | salvator_noster# | WA | 31ms | 14812kb | C++14 | 5.9kb | 2023-08-02 12:27:26 | 2023-08-02 12:27:31 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 100000 + 5;
int n, q;
int li[SIZE];
int pre[SIZE], nxt[SIZE];
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
struct SegmentTree {
static const int SGSZ = SIZE << 2;
int maxi[SGSZ], mini[SGSZ], gcdi[SGSZ];
int maxi2[SGSZ];
void pushUp(int p) {
int lc = p << 1, rc = lc | 1;
maxi[p] = max(maxi[lc], maxi[rc]);
mini[p] = min(mini[lc], mini[rc]);
gcdi[p] = gcd(gcdi[lc], gcdi[rc]);
maxi2[p] = max(maxi2[lc], maxi2[rc]);
return;
}
void build(int p, int nle, int nri) {
if (nle == nri) {
maxi[p] = mini[p] = li[nle];
gcdi[p] = (nle == n ? 0 : abs(li[nle + 1] - li[nle]));
maxi2[p] = pre[nle];
return;
}
int mid = (nle + nri) >> 1, lc = p << 1, rc = lc | 1;
build(lc, nle, mid);
build(rc, mid + 1, nri);
pushUp(p);
return;
}
void mdfNum(int p, int nle, int nri, int id) {
if (nle == nri) {
maxi[p] = mini[p] = li[id];
maxi2[p] = pre[id];
return;
}
int mid = (nle + nri) >> 1, lc = p << 1, rc = lc | 1;
if (id <= mid) {
mdfNum(lc, nle, mid, id);
} else {
mdfNum(rc, mid + 1, nri, id);
}
pushUp(p);
return;
}
void mdfDelta(int p, int nle, int nri, int id, int num) {
if (nle == nri) {
gcdi[p] = num;
return;
}
int mid = (nle + nri) >> 1, lc = p << 1, rc = lc | 1;
if (id <= mid) {
mdfDelta(lc, nle, mid, id, num);
} else {
mdfDelta(rc, mid + 1, nri, id, num);
}
pushUp(p);
return;
}
int qryMax(int p, int nle, int nri, int le, int ri) {
if (nle == le && nri == ri) {
return maxi[p];
}
int mid = (nle + nri) >> 1, lc = p << 1, rc = lc | 1;
if (ri <= mid) {
return qryMax(lc, nle, mid, le, ri);
} else if (le > mid) {
return qryMax(rc, mid + 1, nri, le, ri);
} else {
int lans = qryMax(lc, nle, mid, le, mid);
int rans = qryMax(rc, mid + 1, nri, mid + 1, ri);
return max(lans, rans);
}
}
int qryMin(int p, int nle, int nri, int le, int ri) {
if (nle == le && nri == ri) {
return mini[p];
}
int mid = (nle + nri) >> 1, lc = p << 1, rc = lc | 1;
if (ri <= mid) {
return qryMin(lc, nle, mid, le, ri);
} else if (le > mid) {
return qryMin(rc, mid + 1, nri, le, ri);
} else {
int lans = qryMin(lc, nle, mid, le, mid);
int rans = qryMin(rc, mid + 1, nri, mid + 1, ri);
return min(lans, rans);
}
}
int qryGcd(int p, int nle, int nri, int le, int ri) {
if (nle == le && nri == ri) {
return gcdi[p];
}
int mid = (nle + nri) >> 1, lc = p << 1, rc = lc | 1;
if (ri <= mid) {
return qryGcd(lc, nle, mid, le, ri);
} else if (le > mid) {
return qryGcd(rc, mid + 1, nri, le, ri);
} else {
int lans = qryGcd(lc, nle, mid, le, mid);
int rans = qryGcd(rc, mid + 1, nri, mid + 1, ri);
return gcd(lans, rans);
}
}
int qryMax2(int p, int nle, int nri, int le, int ri) {
if (nle == le && nri == ri) {
return maxi2[p];
}
int mid = (nle + nri) >> 1, lc = p << 1, rc = lc | 1;
if (ri <= mid) {
return qryMax2(lc, nle, mid, le, ri);
} else if (le > mid) {
return qryMax2(rc, mid + 1, nri, le, ri);
} else {
int lans = qryMax2(lc, nle, mid, le, mid);
int rans = qryMax2(rc, mid + 1, nri, mid + 1, ri);
return max(lans, rans);
}
}
};
SegmentTree sgtr;
int getInt(void) {
int ch = getchar(), res = 0;
while (!isdigit(ch)) {
ch = getchar();
}
for (; isdigit(ch); ch = getchar()) {
res = (res << 1) + (res << 3) + (ch - '0');
}
return res;
}
using pii = pair<int, int>;
set<pii> st;
int main(void) {
n = getInt(), q = getInt();
for (int i = 1; i <= n; ++i) {
li[i] = getInt();
}
unordered_map<int, int> mp;
for (int i = 1; i <= n; ++i) {
if (!mp.count(li[i])) {
pre[i] = 0;
} else {
pre[i] = mp[li[i]];
}
mp[li[i]] = i;
}
mp.clear();
for (int i = n; i; --i) {
if (!mp.count(li[i])) {
nxt[i] = n + 1;
} else {
nxt[i] = mp[li[i]];
}
mp[li[i]] = i;
}
mp.clear();
sgtr.build(1, 1, n);
for (int i = 1; i <= n; ++i) {
st.insert(make_pair(li[i], i));
}
for (int _ = 1; _ <= q; ++_) {
int ty = getInt();
if (ty == 1) {
int id = getInt(), num = getInt();
if (pre[id]) {
nxt[pre[id]] = nxt[id];
}
if (nxt[id] != n + 1) {
pre[nxt[id]] = pre[id];
}
auto oldP = make_pair(li[id], id);
li[id] = num;
auto p = make_pair(li[id], id);
st.erase(oldP);
st.insert(p);
auto it = st.find(p);
if (it != st.begin()) {
auto tmp = it;
--tmp;
if (tmp->first == li[id]) {
pre[id] = tmp->second;
} else {
pre[id] = 0;
}
} else {
pre[id] = 0;
}
auto tmp = it;
++tmp;
if (tmp != st.end() && tmp->first == li[id]) {
nxt[id] = tmp->second;
} else {
nxt[id] = n + 1;
}
if (pre[id] != 0) {
nxt[pre[id]] = id;
}
if (nxt[id] != n + 1) {
pre[nxt[id]] = id;
}
sgtr.mdfNum(1, 1, n, id);
if (id != 1) {
sgtr.mdfDelta(1, 1, n, id - 1, abs(li[id] - li[id - 1]));
}
if (id != n) {
sgtr.mdfDelta(1, 1, n, id, abs(li[id + 1] - li[id]));
}
} else {
int le = getInt(), ri = getInt();
if (ri - le + 1 < 3) {
puts("YES");
continue;
}
int maxi = sgtr.qryMax(1, 1, n, le, ri);
int mini = sgtr.qryMin(1, 1, n, le, ri);
// printf("maxi = %d, mini = %d\n", maxi, mini);
if (maxi == mini) {
puts("YES");
continue;
}
int preMax = sgtr.qryMax2(1, 1, n, le, ri);
// printf("preMin = %d\n", preMin);
if (preMax >= le) {
puts("NO");
continue;
}
if ((maxi - mini) % (ri - le)) {
puts("NO");
continue;
}
int d = (maxi - mini) / (ri - le);
int gcdi = sgtr.qryGcd(1, 1, n, le, ri - 1);
// printf("d = %d, gcdi = %d\n", d, gcdi);
if (d == gcdi) {
puts("YES");
} else {
puts("NO");
}
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 7680kb
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: 1ms
memory: 9636kb
input:
2 1 0 1000000000 2 1 2
output:
YES
result:
ok single line: 'YES'
Test #3:
score: -100
Wrong Answer
time: 31ms
memory: 14812kb
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'