QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#19115#1877. Matryoshka Dolls_siri_Compile Error//C++141.6kb2022-01-28 10:49:462022-05-18 04:05:40

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-18 04:05:40]
  • 评测
  • [2022-01-28 10:49:46]
  • 提交

answer

#include <cstdio>
#include <algorithm>
#include <set>
#define ll long long 
using namespace std;

struct section
{
	int 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

answer.code: In function ‘int main()’:
answer.code:64:15: error: ‘sqrt’ was not declared in this scope
   64 |         len = sqrt(n);
      |               ^~~~
answer.code:74:27: warning: format ‘%lld’ expects argument of type ‘long long int*’, but argument 2 has type ‘int*’ [-Wformat=]
   74 |                 scanf("%lld%lld", &sec[i].l, &sec[i].r);
      |                        ~~~^       ~~~~~~~~~
      |                           |       |
      |                           |       int*
      |                           long long int*
      |                        %d
answer.code:74:31: warning: format ‘%lld’ expects argument of type ‘long long int*’, but argument 3 has type ‘int*’ [-Wformat=]
   74 |                 scanf("%lld%lld", &sec[i].l, &sec[i].r);
      |                            ~~~^              ~~~~~~~~~
      |                               |              |
      |                               long long int* int*
      |                            %d
answer.code:62:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   62 |         scanf("%lld%lld", &n, &m);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~
answer.code:68:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   68 |                 scanf("%lld", a + i);
      |                 ~~~~~^~~~~~~~~~~~~~~
answer.code:74:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   74 |                 scanf("%lld%lld", &sec[i].l, &sec[i].r);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~