QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#19114 | #1877. Matryoshka Dolls | _siri_ | WA | 40ms | 16572kb | C++14 | 1.6kb | 2022-01-28 10:46:25 | 2022-05-06 04:14:44 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
struct section
{
int l, r, num;
};
int n, m, l, r, i, len, a[500001], f[500001], b[500001], ans[500001];
section sec[500001];
set < int > s;
int abs(int x)
{
return x > 0 ? x : -x;
}
bool cmp(section l, section r)
{
return b[l.l] ^ b[r.l] ? l.l < r.l : (b[l.l] & 1 ? l.r < r.r : l.r > r.r);
}
void move(int pos1, int opt)
{
if (opt > 0)
s.insert(a[pos1]);
set < int >::iterator pos, end, last, next;
pos = s.find(a[pos1]);
end = s.end();
if (pos != s.begin())
{
last = --pos;
pos++;
}
if (pos != --end)
{
next = ++pos;
pos--;
}
if (pos == s.begin())
ans[i] += abs(f[*next] - f[*pos]) * opt;
else if (pos == end)
ans[i] += abs(f[*last] - f[*pos]) * opt;
else
ans[i] += (abs(f[*next] - f[*pos]) + abs(f[*pos] - f[*last]) - abs(f[*last] - f[*next])) * opt;
if (opt < 0)
s.erase(pos);
}
int main()
{
scanf("%d%d", &n, &m);
len = sqrt(n);
for(i = 1; i <= n; i++)
{
scanf("%d", a + i);
f[a[i]] = i;
b[i] = (i - 1) / len;
}
for(i = 1; i <= m; sec[i].num = i, i++)
scanf("%d%d", &sec[i].l, &sec[i].r);
sort(sec + 1, sec + m + 1, cmp);
l = 1;
r = 1;
s.insert(a[1]);
for (i = 1; i <= m; i++)
{
ans[i] = ans[i - 1];
while (l > sec[i].l)
move(--l, 1);
while (r < sec[i].r)
move(++r, 1);
while (l < sec[i].l)
move(l++, -1);
while (r > sec[i].r)
move(r--, -1);
}
for (i = 1; i <= m; i++)
printf("%d\n", ans[sec[i].num]);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 11884kb
input:
5 5 1 5 2 4 3 1 5 1 4 1 3 1 2 1 1
output:
7 5 3 1 0
result:
ok 5 number(s): "7 5 3 1 0"
Test #2:
score: 0
Accepted
time: 5ms
memory: 12064kb
input:
1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Wrong Answer
time: 40ms
memory: 16572kb
input:
100000 1 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 ...
output:
704982704
result:
wrong answer 1st numbers differ - expected: '4999950000', found: '704982704'