QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#133604 | #4940. Token Distance | salvator_noster# | WA | 31ms | 14752kb | C++14 | 5.8kb | 2023-08-02 11:50:54 | 2023-08-02 11:50:56 |
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 mini2[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]);
mini2[p] = min(mini2[lc], mini2[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]));
mini2[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];
mini2[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 qryMin2(int p, int nle, int nri, int le, int ri) {
if (nle == le && nri == ri) {
return mini2[p];
}
int mid = (nle + nri) >> 1, lc = p << 1, rc = lc | 1;
if (ri <= mid) {
return qryMin2(lc, nle, mid, le, ri);
} else if (le > mid) {
return qryMin2(rc, mid + 1, nri, le, ri);
} else {
int lans = qryMin2(lc, nle, mid, le, mid);
int rans = qryMin2(rc, mid + 1, nri, mid + 1, ri);
return min(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;
}
}
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 preMin = sgtr.qryMin2(1, 1, n, le, ri);
// printf("preMin = %d\n", preMin);
if (preMin >= 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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9664kb
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: 2ms
memory: 7628kb
input:
2 1 0 1000000000 2 1 2
output:
YES
result:
ok single line: 'YES'
Test #3:
score: -100
Wrong Answer
time: 31ms
memory: 14752kb
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 YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES ...
result:
wrong answer 5th lines differ - expected: 'NO', found: 'YES'