QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#18854#1877. Matryoshka DollsiyeWA 44ms21620kbC++143.0kb2022-01-27 14:09:322022-05-06 02:55:47

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 02:55:47]
  • 评测
  • 测评结果:WA
  • 用时:44ms
  • 内存:21620kb
  • [2022-01-27 14:09:32]
  • 提交

answer

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

#define ll long long
#define fo(i,a,b) for(int i=a;i<=b;++i)

int read () {
	int f = 0; char ch = getchar ();
	for (; ch < '0' || ch > '9'; ch = getchar ());
	for (; ch >= '0' && ch <= '9'; ch = getchar ())
		f = (f<<3) + (f<<1) + ch-'0';
	return f;
}

const int N = 5e5 + 5;

int n, m;
int a[N];
struct nod {
	int ri, l, r;
}q[N];
int u[N], tu;
int d[N], td;
struct Seg {
	int lc, rc;
	int Max;
}t[N<<1];
int tl=0;
ll tr[N];
ll ans[N];

bool cmp (nod A, nod B) {
	if (A.r == B.r)return A.l > B.l;
	return A.r < B.r;
}

void build(int l,int r) {
	int now = ++tl;
	t[tl].lc = t[tl].rc = t[tl].Max = 0;
	if (l<r) {
		int mid=l+r >> 1;
		t[now].lc = tl+1; build(l,mid);
		t[now].rc = tl+1; build(mid+1,r);
	}
	return;
}

void add(int l, int r, int k, int x,int y) {
	if (l==r) {t[k].Max = y;return;}
	int mid = l+r >> 1;
	if (x <= mid) add (l, mid, t[k].lc, x, y);
	else add (mid+1, r, t[k].rc, x, y);
	t[k].Max = max (t[t[k].lc].Max, t[t[k].rc].Max);
	return;
}

int query (int l, int r, int k, int L, int R) {
	if (L > r || l > R)return 0;
	if (l >= L && r <= R)return t[k].Max;
	int mid = l+r >> 1;
	return max (query (l,mid, t[k].lc,L,R), query(mid+1,r,t[k].rc,L,R));
}

#define lowbit(x) ((x)&(-(x)))

void addtr (int x, int y) {
	for (int i = x; i <= n; i += lowbit(i))
		tr[i] += y;
	return;
}

int Calc (int x, int y) {
	if (!x||!y)return 0;
	return abs(x-y);
}

int quetr(int x) {
	int res=0;
	for (int i = x; i >= 1; i -= lowbit(i))
		res += tr[i];
	return res;
}

int main () {
	scanf ("%d %d", &n, &m);
	fo (i,1,n) a[i] = read ();
	build (1,n);
	fo (i,1,m) {
		q[i].l = read ();
		q[i].r = read ();
		q[i].ri = i;
	}
	sort (q+1, q+1+m, cmp);
	memset (tr, 0, sizeof (tr));
	memset (ans, 0, sizeof (ans));
	memset (u, 0, sizeof (u));
	memset (d, 0, sizeof (d));
	int j = 0;
	fo (i,1,n) {
		tu = td = 0;
		int tmp;
		tmp = query (1, n, 1, a[i]+1, n);
		while (tmp) {
			u[++tu] = tmp;
			tmp = query (1, n, 1, a[i]+1, a[tmp]-1);
		}
		tmp = query (1, n, 1, 1, a[i]-1);
		while (tmp) {
			d[++td] = tmp;
			tmp = query (1, n, 1, a[tmp]+1, a[i]-1);
		}
		int j1 = 0, j2 = 0;
		int lst = 0;
		while (j1 < tu && j2 < td) {
			if (u[j1 + 1] > d[j2 + 1]) {
				++j1;
				int now = -Calc (u[j1-1], d[j2]) + Calc (u[j1], i);
				addtr (n-u[j1]+1, now-lst);
				lst = now;
			} else {
				++j2;
				int now = -Calc (u[j1], d[j2-1]) + Calc (d[j2], i);
				addtr (n-d[j2]+1, now-lst);
				lst = now;
			}
		}
		while (j1 < tu) {
			++j1;
			int now = -Calc (u[j1-1], d[j2]) + Calc (u[j1], i);
			addtr (n-u[j1]+1, now-lst);
			lst = now;
		}
		while (j2 < td) {
			++j2;
			int now = -Calc (u[j1], d[j2-1]) + Calc (d[j2], i);
			addtr (n-d[j2]+1, now-lst);
			lst = now;
		}
		while (j < m && q[j+1].r == i) {
			++j;
			ans[q[j].ri] = quetr (n-q[j].l+1);
		}
		add (1, n, 1, a[i], i);
//		printf ("%d\n", quetr (n));
	}
	fo (i,1,m) printf ("%lld\n", ans[i]);
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 5ms
memory: 19900kb

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

input:

1 1
1
1 1

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: -100
Wrong Answer
time: 44ms
memory: 21620kb

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:

-1794867298

result:

wrong answer 1st numbers differ - expected: '4999950000', found: '-1794867298'