UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#196750#2758. 计算题tkswls1001224ms82084kbC++3.9kb2023-10-31 10:49:302023-10-31 12:07:05

answer

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, a[500005], ans[500005];
struct que {
	int l, r, name;
} c[500005];
struct node {
	int l, r, sum, maxn, addtag, changetag, tadd;
} b[2000006];
struct nnode {
	int name, num;
} r[500005];
int l;
bool cmp(que p, que q) {
	return p.r < q.r;
}
inline void update(int p) {
	b[p].sum = b[2 * p].sum + b[2 * p + 1].sum;
	b[p].maxn = b[2 * p].maxn + b[2 * p + 1].maxn;
}
inline void push_down(int p) {
	if (b[p].tadd) {
		b[2 * p].tadd += b[p].tadd;
		b[2 * p + 1].tadd += b[p].tadd;
		b[2 * p].sum += (b[2 * p].r - b[2 * p].l + 1) * b[p].tadd;
		b[2 * p + 1].sum += (b[2 * p + 1].r - b[2 * p + 1].l + 1) * b[p].tadd;
		b[p].tadd = 0;
	}
	if (b[p].addtag) {
		if (b[2 * p].changetag) {
			b[2 * p].tadd += b[2 * p].changetag * b[p].addtag;
			b[2 * p].sum += (b[2 * p].r - b[2 * p].l + 1) * b[2 * p].changetag * b[p].addtag;
		} else {
			b[2 * p].addtag += b[p].addtag;
			b[2 * p].sum +=  b[p].addtag * b[2 * p].maxn;
		}
		if (b[2 * p + 1].changetag) {
			b[2 * p + 1].tadd += b[2 * p + 1].changetag * b[p].addtag;
			b[2 * p + 1].sum += (b[2 * p + 1].r - b[2 * p + 1].l + 1) * b[2 * p + 1].changetag * b[p].addtag;
		} else {
			b[2 * p + 1].addtag += b[p].addtag;
			b[2 * p + 1].sum +=  b[p].addtag * b[2 * p + 1].maxn;
		}
		b[p].addtag = 0;
	}
	if (b[p].changetag) {
		b[2 * p].changetag = b[2 * p + 1].changetag = b[p].changetag;
		b[2 * p].maxn = (b[2 * p].r - b[2 * p].l + 1) * b[p].changetag;
		b[2 * p + 1].maxn = (b[2 * p + 1].r - b[2 * p + 1].l + 1) * b[p].changetag;
		b[p].changetag = 0;
	}
}
inline void build(int p, int l, int r) {
	b[p].l = l, b[p].r = r;
	b[p].addtag = b[p].changetag = b[p].tadd = 0;
	b[p].sum = b[p].maxn = 0;
	if (l == r) {
		return;
	}
	build(2 * p, l, (l + r) >> 1);
	build(2 * p + 1, ((l + r) >> 1) + 1, r);
}
inline void change(int p, int l, int r, int w) {
	if (b[p].l >= l && b[p].r <= r) {
		b[p].changetag = w;
		b[p].maxn = (b[p].r - b[p].l + 1) * w;
		return;
	}
	push_down(p);
	int mid = (b[p].r + b[p].l) >> 1;
	if (r <= mid) change(2 * p, l, r, w);
	else if (l > mid) change(2 * p + 1, l, r, w);
	else change(2 * p, l, mid, w), change(2 * p + 1, mid + 1, r, w);
	update(p);
}
inline int query(int p, int l, int r) {
	if (b[p].l >= l && b[p].r <= r) {
		return b[p].sum;
	}
	push_down(p);
	int mid = (b[p].r + b[p].l) >> 1;
	if (r <= mid) return query(2 * p, l, r);
	else if (l > mid) return query(2 * p + 1, l, r);
	else return query(2 * p, l, mid) + query(2 * p + 1, mid + 1, r);
}
inline void add(int p, int l, int r) {
	if (b[p].l >= l && b[p].r <= r) {
		if (b[ p].changetag) {
			b[p].tadd += b[p].changetag;
			b[p].sum += (b[p].r - b[p].l + 1) * b[p].changetag;
		} else {
			b[p].addtag += 1;
			b[p].sum +=  b[p].maxn;
		}
		return;
	}
	push_down(p);
	int mid = (b[p].r + b[p].l) >> 1;
	if (r <= mid) add(2 * p, l, r);
	else if (l > mid) add(2 * p + 1, l, r);
	else add(2 * p, l, mid), add(2 * p + 1, mid + 1, r);
	update(p);
}
inline void update(nnode u) {
	int ll = 0, rr = l, mid;
	while (ll < rr) {
		mid = (ll + rr + 1) >> 1;
		if (r[mid].num >= u.num) {
			ll = mid;
		} else {
			rr = mid - 1;
		}
	}
	r[ll + 1] = u;
//	cout << r[ll].name + 1 << "|" << u.name << "|" << u.num << endl;
	l = ll + 1;
	change(1, r[ll].name + 1, u.name, u.num);
	add(1, 1, u.name);
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	for (int i = 1; i <= m; i++) {
		cin >> c[i].l >> c[i].r;
		c[i].name = i;
	}
	sort(c + 1, c + m + 1, cmp);
	r[0].num = 0x3f3f3f3f;
	build(1, 1, n);
	int cnt = 0;
	for (int i = 1; i <= n; i++) {
		update(nnode{i, a[i]});
		while (cnt + 1 <= m && c[cnt + 1].r <= i) {
			cnt++;
			ans[c[cnt].name] = query(1, c[cnt].l, c[cnt].r);
		}
	}
	for (int i = 1; i <= m; i++) {
		cout << ans[i] << "\n";
	}
}
//3 6
//1 3 2
//1 1
//1 2
//1 3
//2 2
//2 3
//3 3

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 1344kb

input:

200 200
100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 7...

output:

442284
9826
797974
1381071
180389
121270
1388011
120640
185096
68096
466539
3926
55182
14758
288508
...

result:

ok 200 lines

Test #2:

score: 10
Accepted
time: 0ms
memory: 1340kb

input:

200 200
100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 7...

output:

1082573
87955
1097447
736888
797549
1002178
12315
5180
33394
1179
2389676
63285
133045
2029
955640
1...

result:

ok 200 lines

Test #3:

score: 10
Accepted
time: 0ms
memory: 1344kb

input:

200 200
100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 7...

output:

1056472
138277
141487
282073
64825
1408215
415060
770606
1078940
416
2279753
1140683
50575
92565
812...

result:

ok 200 lines

Test #4:

score: 10
Accepted
time: 4ms
memory: 1904kb

input:

3000 3000
1500 1499 1498 1497 1496 1495 1494 1493 1492 1491 1490 1489 1488 1487 1486 1485 1484 1483 ...

output:

706782140
185067005
5730553508
4328565481
4313040644
7173037636
3842450
170663680
23455950
439333694...

result:

ok 3000 lines

Test #5:

score: 10
Accepted
time: 4ms
memory: 1904kb

input:

3000 3000
1500 1499 1498 1497 1496 1495 1494 1493 1492 1491 1490 1489 1488 1487 1486 1485 1484 1483 ...

output:

5130400609
105642
36920474
169161902
200067560
1978979384
2394867415
591767792
352222920
7162880872
...

result:

ok 3000 lines

Test #6:

score: 10
Accepted
time: 32ms
memory: 6304kb

input:

30000 30000
15000 14999 14998 14997 14996 14995 14994 14993 14992 14991 14990 14989 14988 14987 1498...

output:

2223635170274
7189988466178
2644855174592
58030169512
72531079799
3140093687241
492300738194
1270133...

result:

ok 30000 lines

Test #7:

score: 10
Accepted
time: 40ms
memory: 6304kb

input:

30000 30000
15000 14999 14998 14997 14996 14995 14994 14993 14992 14991 14990 14989 14988 14987 1498...

output:

3003232159486
518764940193
24095445034
494257923551
174357728503
331182349653
4136125307170
71174180...

result:

ok 30000 lines

Test #8:

score: 10
Accepted
time: 153ms
memory: 20344kb

input:

100000 100000
50000 49999 49998 49997 49996 49995 49994 49993 49992 49991 49990 49989 49988 49987 49...

output:

44674375106822
75832733747788
39049632936041
222165767500434
79697682680
36884343747518
663895210966...

result:

ok 100000 lines

Test #9:

score: 10
Accepted
time: 137ms
memory: 20340kb

input:

100000 100000
50000 49999 49998 49997 49996 49995 49994 49993 49992 49991 49990 49989 49988 49987 49...

output:

74612974757040
126715643667914
15365239043056
63869835075637
85960942307786
316978366493
25155200499...

result:

ok 100000 lines

Test #10:

score: 10
Accepted
time: 854ms
memory: 82084kb

input:

500000 500000
250000 249999 249998 249997 249996 249995 249994 249993 249992 249991 249990 249989 24...

output:

106948953821764
95827568044076
20350687506870
18732880285911811
6119814730830547
6035555660318435
56...

result:

ok 500000 lines