Submission #754945

# Submission time Handle Problem Language Result Execution time Memory
754945 2023-06-09T03:14:50 Z pcc Fire (JOI20_ho_t5) C++14
100 / 100
659 ms 103684 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pll pair<ll,ll>
#define fs first
#define sc second
#define int ll

const ll mxn = 4e5+10;

struct Seg{
	pll segtree[mxn*4];
	Seg(){
		for(auto &i:segtree)i = {0,0};
	}
	void modify(int now,int l,int r,int s,int e,ll v){
		if(l>=s&&e>=r){
			segtree[now].sc += v;
			return;
		}
		int mid = (l+r)>>1;
		if(mid>=s)modify(now*2+1,l,mid,s,e,v);
		if(mid<e)modify(now*2+2,mid+1,r,s,e,v);
		segtree[now].fs = 
			segtree[now*2+1].fs+segtree[now*2+1].sc*(mid-l+1)+segtree[now*2+2].fs+segtree[now*2+2].sc*(r-mid);
	}
	ll getval(int now,int l,int r,int s,int e){
		if(l>=s&&e>=r){
			return segtree[now].fs+segtree[now].sc*(r-l+1);
		}
		int mid = (l+r)>>1;
		if(mid>=e)return getval(now*2+1,l,mid,s,e)+segtree[now].sc*(min(e,r)-max(s,l)+1);
		else if(mid<s)return getval(now*2+2,mid+1,r,s,e)+segtree[now].sc*(min(e,r)-max(s,l)+1);
		else return getval(now*2+1,l,mid,s,e)+getval(now*2+2,mid+1,r,s,e)+segtree[now].sc*(min(e,r)-max(s,l)+1);
	}
};
Seg seg1,seg2;
vector<pll> op[mxn];
ll arr[mxn];
ll n,q;
vector<tuple<ll,ll,ll>> req[mxn];
pll range[mxn];
ll ans[mxn];
ll sh = mxn>>1;

void addtri(int r,int c,ll val){
	op[c].push_back({r,val});
}

void add(int now,int h,int w){
	//cout<<now<<":"<<h<<' '<<w<<endl;
	addtri(now,0,arr[now]);
	addtri(now,h,-arr[now]);
	addtri(now+w,w,-arr[now]);
	addtri(now+w,h+w,arr[now]);
	return;
}

int32_t main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n>>q;
	for(int i = 1;i<=n;i++){
		cin>>arr[i];
	}
	for(int i = 1;i<=q;i++){
		ll t,l,r;
		cin>>t>>l>>r;
		req[t].push_back(make_tuple(1LL*i,l,r));
	}
	arr[0] = arr[n+1] = 1e18;
	deque<int> dq;
	dq.push_back(0);
	for(int i = 1;i<=n;i++){
		while(arr[dq.back()]<=arr[i])dq.pop_back();
		range[i].fs = dq.back();
		dq.push_back(i);
	}
	dq.clear();
	dq.push_back(n+1);
	for(int i = n;i>=1;i--){
		while(arr[dq.back()]<arr[i])dq.pop_back();
		range[i].sc = dq.back();
		dq.push_back(i);
		//cout<<i<<":"<<range[i].fs<<' '<<range[i].sc<<'\n';
	}
	for(int i = 1;i<=n;i++){
		if(range[i].fs == 0)range[i].fs = n+1;
		else range[i].fs = i-range[i].fs;
		range[i].sc = range[i].sc-i;
		add(i,range[i].fs,range[i].sc);
	}
	for(int i = 0;i<=n;i++){
		for(auto &j:op[i]){
			seg1.modify(0,1,mxn,j.fs,mxn,j.sc);
			seg2.modify(0,1,mxn,sh+j.fs+1,mxn,-j.sc);
		}
		for(auto &j:req[i]){
			ans[get<0>(j)] = 
				seg1.getval(0,1,mxn,get<1>(j),get<2>(j))+seg2.getval(0,1,mxn,sh+get<1>(j),sh+get<2>(j));
		}
		/*
		for(int i = 1;i<=n;i++)cout<<seg1.getval(0,1,mxn,i,i)<<' ';cout<<endl;
		for(int i  = 1;i<=n;i++)cout<<seg2.getval(0,1,mxn,sh+i,sh+i)<<' ';cout<<endl;

	   */
		sh--;
	}
	for(int i = 1;i<=q;i++)cout<<ans[i]<<'\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 35 ms 69204 KB Output is correct
2 Correct 36 ms 69196 KB Output is correct
3 Correct 35 ms 69132 KB Output is correct
4 Correct 36 ms 69240 KB Output is correct
5 Correct 35 ms 69244 KB Output is correct
6 Correct 36 ms 69248 KB Output is correct
7 Correct 35 ms 69196 KB Output is correct
8 Correct 35 ms 69180 KB Output is correct
9 Correct 35 ms 69240 KB Output is correct
10 Correct 37 ms 69196 KB Output is correct
11 Correct 35 ms 69176 KB Output is correct
12 Correct 35 ms 69200 KB Output is correct
13 Correct 36 ms 69156 KB Output is correct
14 Correct 35 ms 69192 KB Output is correct
15 Correct 36 ms 69220 KB Output is correct
16 Correct 35 ms 69252 KB Output is correct
17 Correct 35 ms 69152 KB Output is correct
18 Correct 37 ms 69172 KB Output is correct
19 Correct 37 ms 69204 KB Output is correct
20 Correct 38 ms 69176 KB Output is correct
21 Correct 38 ms 69168 KB Output is correct
22 Correct 35 ms 69204 KB Output is correct
23 Correct 35 ms 69200 KB Output is correct
24 Correct 36 ms 69212 KB Output is correct
25 Correct 35 ms 69256 KB Output is correct
26 Correct 35 ms 69228 KB Output is correct
27 Correct 35 ms 69196 KB Output is correct
28 Correct 36 ms 69192 KB Output is correct
29 Correct 35 ms 69172 KB Output is correct
30 Correct 35 ms 69268 KB Output is correct
31 Correct 35 ms 69188 KB Output is correct
32 Correct 35 ms 69196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 69204 KB Output is correct
2 Correct 538 ms 101040 KB Output is correct
3 Correct 517 ms 99268 KB Output is correct
4 Correct 519 ms 100908 KB Output is correct
5 Correct 548 ms 102572 KB Output is correct
6 Correct 521 ms 100800 KB Output is correct
7 Correct 542 ms 99800 KB Output is correct
8 Correct 619 ms 103428 KB Output is correct
9 Correct 595 ms 101408 KB Output is correct
10 Correct 517 ms 100744 KB Output is correct
11 Correct 555 ms 101484 KB Output is correct
12 Correct 519 ms 100652 KB Output is correct
13 Correct 549 ms 101828 KB Output is correct
14 Correct 546 ms 98352 KB Output is correct
15 Correct 532 ms 103060 KB Output is correct
16 Correct 515 ms 101128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 69204 KB Output is correct
2 Correct 500 ms 97028 KB Output is correct
3 Correct 484 ms 96608 KB Output is correct
4 Correct 528 ms 98320 KB Output is correct
5 Correct 500 ms 96696 KB Output is correct
6 Correct 513 ms 96936 KB Output is correct
7 Correct 504 ms 96880 KB Output is correct
8 Correct 512 ms 96816 KB Output is correct
9 Correct 501 ms 96752 KB Output is correct
10 Correct 516 ms 96428 KB Output is correct
11 Correct 556 ms 98352 KB Output is correct
12 Correct 530 ms 98348 KB Output is correct
13 Correct 521 ms 98304 KB Output is correct
14 Correct 487 ms 96760 KB Output is correct
15 Correct 522 ms 98320 KB Output is correct
16 Correct 515 ms 97812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 512 ms 96604 KB Output is correct
2 Correct 587 ms 98780 KB Output is correct
3 Correct 550 ms 99724 KB Output is correct
4 Correct 556 ms 98684 KB Output is correct
5 Correct 523 ms 100212 KB Output is correct
6 Correct 512 ms 98244 KB Output is correct
7 Correct 516 ms 98476 KB Output is correct
8 Correct 526 ms 98772 KB Output is correct
9 Correct 528 ms 98288 KB Output is correct
10 Correct 537 ms 98580 KB Output is correct
11 Correct 533 ms 98452 KB Output is correct
12 Correct 539 ms 98468 KB Output is correct
13 Correct 556 ms 98332 KB Output is correct
14 Correct 540 ms 99184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 69204 KB Output is correct
2 Correct 36 ms 69196 KB Output is correct
3 Correct 35 ms 69132 KB Output is correct
4 Correct 36 ms 69240 KB Output is correct
5 Correct 35 ms 69244 KB Output is correct
6 Correct 36 ms 69248 KB Output is correct
7 Correct 35 ms 69196 KB Output is correct
8 Correct 35 ms 69180 KB Output is correct
9 Correct 35 ms 69240 KB Output is correct
10 Correct 37 ms 69196 KB Output is correct
11 Correct 35 ms 69176 KB Output is correct
12 Correct 35 ms 69200 KB Output is correct
13 Correct 36 ms 69156 KB Output is correct
14 Correct 35 ms 69192 KB Output is correct
15 Correct 36 ms 69220 KB Output is correct
16 Correct 35 ms 69252 KB Output is correct
17 Correct 35 ms 69152 KB Output is correct
18 Correct 37 ms 69172 KB Output is correct
19 Correct 37 ms 69204 KB Output is correct
20 Correct 38 ms 69176 KB Output is correct
21 Correct 38 ms 69168 KB Output is correct
22 Correct 35 ms 69204 KB Output is correct
23 Correct 35 ms 69200 KB Output is correct
24 Correct 36 ms 69212 KB Output is correct
25 Correct 35 ms 69256 KB Output is correct
26 Correct 35 ms 69228 KB Output is correct
27 Correct 35 ms 69196 KB Output is correct
28 Correct 36 ms 69192 KB Output is correct
29 Correct 35 ms 69172 KB Output is correct
30 Correct 35 ms 69268 KB Output is correct
31 Correct 35 ms 69188 KB Output is correct
32 Correct 35 ms 69196 KB Output is correct
33 Correct 538 ms 101040 KB Output is correct
34 Correct 517 ms 99268 KB Output is correct
35 Correct 519 ms 100908 KB Output is correct
36 Correct 548 ms 102572 KB Output is correct
37 Correct 521 ms 100800 KB Output is correct
38 Correct 542 ms 99800 KB Output is correct
39 Correct 619 ms 103428 KB Output is correct
40 Correct 595 ms 101408 KB Output is correct
41 Correct 517 ms 100744 KB Output is correct
42 Correct 555 ms 101484 KB Output is correct
43 Correct 519 ms 100652 KB Output is correct
44 Correct 549 ms 101828 KB Output is correct
45 Correct 546 ms 98352 KB Output is correct
46 Correct 532 ms 103060 KB Output is correct
47 Correct 515 ms 101128 KB Output is correct
48 Correct 500 ms 97028 KB Output is correct
49 Correct 484 ms 96608 KB Output is correct
50 Correct 528 ms 98320 KB Output is correct
51 Correct 500 ms 96696 KB Output is correct
52 Correct 513 ms 96936 KB Output is correct
53 Correct 504 ms 96880 KB Output is correct
54 Correct 512 ms 96816 KB Output is correct
55 Correct 501 ms 96752 KB Output is correct
56 Correct 516 ms 96428 KB Output is correct
57 Correct 556 ms 98352 KB Output is correct
58 Correct 530 ms 98348 KB Output is correct
59 Correct 521 ms 98304 KB Output is correct
60 Correct 487 ms 96760 KB Output is correct
61 Correct 522 ms 98320 KB Output is correct
62 Correct 515 ms 97812 KB Output is correct
63 Correct 512 ms 96604 KB Output is correct
64 Correct 587 ms 98780 KB Output is correct
65 Correct 550 ms 99724 KB Output is correct
66 Correct 556 ms 98684 KB Output is correct
67 Correct 523 ms 100212 KB Output is correct
68 Correct 512 ms 98244 KB Output is correct
69 Correct 516 ms 98476 KB Output is correct
70 Correct 526 ms 98772 KB Output is correct
71 Correct 528 ms 98288 KB Output is correct
72 Correct 537 ms 98580 KB Output is correct
73 Correct 533 ms 98452 KB Output is correct
74 Correct 539 ms 98468 KB Output is correct
75 Correct 556 ms 98332 KB Output is correct
76 Correct 540 ms 99184 KB Output is correct
77 Correct 616 ms 101056 KB Output is correct
78 Correct 659 ms 101360 KB Output is correct
79 Correct 601 ms 102100 KB Output is correct
80 Correct 603 ms 101032 KB Output is correct
81 Correct 643 ms 100872 KB Output is correct
82 Correct 607 ms 102220 KB Output is correct
83 Correct 614 ms 100860 KB Output is correct
84 Correct 633 ms 100936 KB Output is correct
85 Correct 600 ms 102024 KB Output is correct
86 Correct 610 ms 101012 KB Output is correct
87 Correct 577 ms 103468 KB Output is correct
88 Correct 602 ms 103576 KB Output is correct
89 Correct 580 ms 101684 KB Output is correct
90 Correct 590 ms 103132 KB Output is correct
91 Correct 547 ms 101732 KB Output is correct
92 Correct 550 ms 101760 KB Output is correct
93 Correct 567 ms 102048 KB Output is correct
94 Correct 584 ms 103684 KB Output is correct
95 Correct 587 ms 103440 KB Output is correct
96 Correct 570 ms 101896 KB Output is correct
97 Correct 567 ms 102164 KB Output is correct
98 Correct 561 ms 101712 KB Output is correct
99 Correct 565 ms 102800 KB Output is correct
100 Correct 574 ms 103104 KB Output is correct
101 Correct 593 ms 101760 KB Output is correct
102 Correct 581 ms 103548 KB Output is correct