QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#19118#1877. Matryoshka Dolls_siri_WA 46ms16736kbC++141.5kb2022-01-28 10:52:182022-05-06 04:15:07

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-06 04:15:07]
  • 评测
  • 测评结果:WA
  • 用时:46ms
  • 内存:16736kb
  • [2022-01-28 10:52:18]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long 
using namespace std;

struct section
{
	ll l, r, num;
};

ll n, m, l, r, i, len, a[500001], f[500001], b[500001], ans[500001]; 
section sec[500001];
set < ll > s;


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(ll pos1, ll opt)
{
	if (opt > 0)
		s.insert(a[pos1]);
		
	set < ll >::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("%lld%lld", &n, &m);
	
	len = sqrt(n);
	
	for(i = 1; i <= n; i++)
	{
		scanf("%lld", a + i);
		f[a[i]] = i;
		b[i] = (i - 1) / len;
	}
	
	for(i = 1; i <= m; sec[i].num = i, i++)	
		scanf("%lld%lld", &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("%lld\n", ans[sec[i].num]);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 11960kb

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: 2ms
memory: 11964kb

input:

1 1
1
1 1

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 46ms
memory: 16736kb

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:

4999950000

result:

ok 1 number(s): "4999950000"

Test #4:

score: 0
Accepted
time: 0ms
memory: 12012kb

input:

20 1
12 8 13 10 18 14 1 19 5 16 15 9 17 20 6 2 11 4 3 7
9 18

output:

36

result:

ok 1 number(s): "36"

Test #5:

score: -100
Wrong Answer
time: 2ms
memory: 12000kb

input:

20 10
5 16 11 7 19 8 12 13 17 18 6 1 14 3 4 2 15 20 10 9
7 11
7 13
7 17
11 15
1 7
4 6
1 5
6 14
3 5
9 9

output:

7
16
22
9
20
3
34
8
3
0

result:

wrong answer 3rd numbers differ - expected: '34', found: '22'