답안 #226905

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
226905 2020-04-25T17:04:48 Z errorgorn Fire (JOI20_ho_t5) C++14
100 / 100
883 ms 71904 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ll,ii>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;

#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

ll MAX(ll a){return a;}
ll MIN(ll a){return a;}
template<typename... Args>
ll MAX(ll a,Args... args){return max(a,MAX(args...));}
template<typename... Args>
ll MIN(ll a,Args... args){return min(a,MIN(args...));}

#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
struct dat{
	ll val=0,lazy=0;
	
	dat(){}
};

struct node{
	dat seg[1050000];

	ll propo(int u,int s,int e){
		if (seg[u].lazy){
			seg[u].val+=seg[u].lazy*(e-s+1);
			if (s!=e) seg[u<<1].lazy+=seg[u].lazy,seg[u<<1|1].lazy+=seg[u].lazy;
			seg[u].lazy=0;
		}
		
		return seg[u].val;
	}

	void update(int i,int j,ll k,int u=1,int s=-200005,int e=200005){
		//cerr<<i<<" "<<j<<" "<<k<<" "<<u<<" "<<s<<" "<<e<<endl;
		int m=s+e>>1;
		
		if (s==i && e==j){
			seg[u].lazy+=k;
		}
		else{	
			if (j<=m) update(i,j,k,u<<1,s,m);
			else if (m<i) update(i,j,k,u<<1|1,m+1,e);
			else update(i,m,k,u<<1,s,m),update(m+1,j,k,u<<1|1,m+1,e);
			
			seg[u].val=propo(u<<1,s,m)+propo(u<<1|1,m+1,e);	
		}
	}

	ll query(int i,int j,int u=1,int s=-200005,int e=200005){
		propo(u,s,e);
		
		int m=s+e>>1;
		
		if (s==i && e==j) return seg[u].val;
		else if (j<=m) return query(i,j,u<<1,s,m);
		else if (m<i) return query(i,j,u<<1|1,m+1,e);
		else return query(i,m,u<<1,s,m)+query(m+1,j,u<<1|1,m+1,e);
	}
}rect=node(),diag=node();

struct upd{
	int row;
	int s,e;
	ll val;
	
	upd(int a,int b,int c,ll d){
		row=a,s=b,e=c,val=d;
	}
};

struct Q{
	int a,b,c;
	int index;
	
	Q(int _a,int _b,int _c,int _index){
		a=_a,b=_b,c=_c,index=_index;
	}
};

int n,q;
int arr[200005];
int l[200005];
int r[200005];

vector<int> stk;

vector<upd> ru;
vector<upd> du;

vector<Q> queries;

ll ans[200005];

void tri(int i,int j,ll val){
	//cout<<i<<" "<<j<<" "<<val<<endl;
	if (j<i) return; //stupid shit

	
	diag.update(i,200005,val);
	rect.update(j+1,200005,-val);
	
	du.push_back(upd(j-i,i,200005,val));
	ru.push_back(upd(j-i,j+1,200005,-val));
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	cin>>n>>q;
	rep(x,1,n+1) cin>>arr[x];
	
	rep(x,1,n+1){
		while (!stk.empty() && arr[stk.back()]<arr[x]){
			r[stk.back()]=x-1;
			stk.pop_back();
		}
		
		stk.push_back(x);
	}
	while (!stk.empty()){
		r[stk.back()]=n;
		stk.pop_back();
	}
	
	rep(x,n+1,1){
		while (!stk.empty() && arr[stk.back()]<=arr[x]){
			l[stk.back()]=x+1;
			stk.pop_back();
		}
		
		stk.push_back(x);
	}
	while (!stk.empty()){
		l[stk.back()]=-200005; //yea
		stk.pop_back();
	}
	
	rep(x,1,n+1){
		tri(l[x],x-1,-arr[x]);
		tri(x+1,r[x],-arr[x]);
		tri(l[x],r[x],arr[x]);
	}
	
	int a,b,c;
	rep(x,0,q){
		cin>>a>>b>>c;
		
		queries.push_back(Q(a,b,c,x));
	}
	
	sort(all(queries),[](Q &i,Q &j){
		return i.a<j.a;
	});
	
	sort(all(du),[](upd &i,upd &j){
		return i.row>j.row;
	});
	
	sort(all(ru),[](upd &i,upd &j){
		return i.row>j.row;
	});
	
	for (auto &it:queries){
		while (!du.empty() && du.back().row<it.a){
			diag.update(du.back().s,du.back().e,-du.back().val);
			du.pop_back();
		}
		
		while (!ru.empty() && ru.back().row<it.a){
			rect.update(ru.back().s,ru.back().e,-ru.back().val);
			ru.pop_back();
		}
		
		ans[it.index]=rect.query(it.b,it.c)+diag.query(it.b-it.a,it.c-it.a);
	}
	
	rep(x,0,q) cout<<ans[x]<<endl;
}

Compilation message

ho_t5.cpp: In member function 'void node::update(int, int, long long int, int, int, int)':
ho_t5.cpp:49:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int m=s+e>>1;
         ~^~
ho_t5.cpp: In member function 'long long int node::query(int, int, int, int, int)':
ho_t5.cpp:66:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int m=s+e>>1;
         ~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 33280 KB Output is correct
2 Correct 24 ms 33280 KB Output is correct
3 Correct 24 ms 33280 KB Output is correct
4 Correct 25 ms 33272 KB Output is correct
5 Correct 24 ms 33280 KB Output is correct
6 Correct 24 ms 33280 KB Output is correct
7 Correct 24 ms 33272 KB Output is correct
8 Correct 24 ms 33280 KB Output is correct
9 Correct 23 ms 33280 KB Output is correct
10 Correct 24 ms 33272 KB Output is correct
11 Correct 25 ms 33280 KB Output is correct
12 Correct 24 ms 33280 KB Output is correct
13 Correct 24 ms 33280 KB Output is correct
14 Correct 24 ms 33280 KB Output is correct
15 Correct 24 ms 33280 KB Output is correct
16 Correct 24 ms 33408 KB Output is correct
17 Correct 27 ms 33280 KB Output is correct
18 Correct 29 ms 33272 KB Output is correct
19 Correct 24 ms 33280 KB Output is correct
20 Correct 25 ms 33272 KB Output is correct
21 Correct 27 ms 33256 KB Output is correct
22 Correct 27 ms 33280 KB Output is correct
23 Correct 25 ms 33280 KB Output is correct
24 Correct 24 ms 33280 KB Output is correct
25 Correct 25 ms 33280 KB Output is correct
26 Correct 24 ms 33280 KB Output is correct
27 Correct 27 ms 33400 KB Output is correct
28 Correct 24 ms 33280 KB Output is correct
29 Correct 24 ms 33280 KB Output is correct
30 Correct 24 ms 33280 KB Output is correct
31 Correct 30 ms 33280 KB Output is correct
32 Correct 24 ms 33280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 33280 KB Output is correct
2 Correct 800 ms 65480 KB Output is correct
3 Correct 766 ms 71024 KB Output is correct
4 Correct 778 ms 71132 KB Output is correct
5 Correct 770 ms 71524 KB Output is correct
6 Correct 779 ms 71068 KB Output is correct
7 Correct 802 ms 71388 KB Output is correct
8 Correct 826 ms 71560 KB Output is correct
9 Correct 826 ms 71516 KB Output is correct
10 Correct 796 ms 70748 KB Output is correct
11 Correct 784 ms 71768 KB Output is correct
12 Correct 770 ms 70660 KB Output is correct
13 Correct 797 ms 71260 KB Output is correct
14 Correct 784 ms 71140 KB Output is correct
15 Correct 774 ms 71260 KB Output is correct
16 Correct 774 ms 71128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 33280 KB Output is correct
2 Correct 736 ms 64648 KB Output is correct
3 Correct 709 ms 64104 KB Output is correct
4 Correct 747 ms 70876 KB Output is correct
5 Correct 721 ms 69844 KB Output is correct
6 Correct 725 ms 70124 KB Output is correct
7 Correct 767 ms 70360 KB Output is correct
8 Correct 733 ms 70192 KB Output is correct
9 Correct 737 ms 69832 KB Output is correct
10 Correct 729 ms 69416 KB Output is correct
11 Correct 736 ms 70696 KB Output is correct
12 Correct 785 ms 70416 KB Output is correct
13 Correct 730 ms 70596 KB Output is correct
14 Correct 729 ms 69852 KB Output is correct
15 Correct 731 ms 70616 KB Output is correct
16 Correct 743 ms 70104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 745 ms 63912 KB Output is correct
2 Correct 741 ms 68224 KB Output is correct
3 Correct 756 ms 68776 KB Output is correct
4 Correct 736 ms 68152 KB Output is correct
5 Correct 731 ms 68144 KB Output is correct
6 Correct 733 ms 68524 KB Output is correct
7 Correct 747 ms 68684 KB Output is correct
8 Correct 748 ms 68416 KB Output is correct
9 Correct 739 ms 68136 KB Output is correct
10 Correct 749 ms 68260 KB Output is correct
11 Correct 765 ms 68392 KB Output is correct
12 Correct 759 ms 68404 KB Output is correct
13 Correct 738 ms 68136 KB Output is correct
14 Correct 749 ms 68392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 33280 KB Output is correct
2 Correct 24 ms 33280 KB Output is correct
3 Correct 24 ms 33280 KB Output is correct
4 Correct 25 ms 33272 KB Output is correct
5 Correct 24 ms 33280 KB Output is correct
6 Correct 24 ms 33280 KB Output is correct
7 Correct 24 ms 33272 KB Output is correct
8 Correct 24 ms 33280 KB Output is correct
9 Correct 23 ms 33280 KB Output is correct
10 Correct 24 ms 33272 KB Output is correct
11 Correct 25 ms 33280 KB Output is correct
12 Correct 24 ms 33280 KB Output is correct
13 Correct 24 ms 33280 KB Output is correct
14 Correct 24 ms 33280 KB Output is correct
15 Correct 24 ms 33280 KB Output is correct
16 Correct 24 ms 33408 KB Output is correct
17 Correct 27 ms 33280 KB Output is correct
18 Correct 29 ms 33272 KB Output is correct
19 Correct 24 ms 33280 KB Output is correct
20 Correct 25 ms 33272 KB Output is correct
21 Correct 27 ms 33256 KB Output is correct
22 Correct 27 ms 33280 KB Output is correct
23 Correct 25 ms 33280 KB Output is correct
24 Correct 24 ms 33280 KB Output is correct
25 Correct 25 ms 33280 KB Output is correct
26 Correct 24 ms 33280 KB Output is correct
27 Correct 27 ms 33400 KB Output is correct
28 Correct 24 ms 33280 KB Output is correct
29 Correct 24 ms 33280 KB Output is correct
30 Correct 24 ms 33280 KB Output is correct
31 Correct 30 ms 33280 KB Output is correct
32 Correct 24 ms 33280 KB Output is correct
33 Correct 814 ms 65272 KB Output is correct
34 Correct 873 ms 71812 KB Output is correct
35 Correct 844 ms 71560 KB Output is correct
36 Correct 846 ms 71132 KB Output is correct
37 Correct 801 ms 70872 KB Output is correct
38 Correct 851 ms 71580 KB Output is correct
39 Correct 828 ms 71004 KB Output is correct
40 Correct 826 ms 71264 KB Output is correct
41 Correct 845 ms 71440 KB Output is correct
42 Correct 817 ms 71052 KB Output is correct
43 Correct 808 ms 71796 KB Output is correct
44 Correct 850 ms 71644 KB Output is correct
45 Correct 828 ms 70744 KB Output is correct
46 Correct 815 ms 71396 KB Output is correct
47 Correct 775 ms 70964 KB Output is correct
48 Correct 817 ms 70616 KB Output is correct
49 Correct 796 ms 71000 KB Output is correct
50 Correct 839 ms 71904 KB Output is correct
51 Correct 803 ms 71776 KB Output is correct
52 Correct 803 ms 71260 KB Output is correct
53 Correct 820 ms 71264 KB Output is correct
54 Correct 876 ms 70516 KB Output is correct
55 Correct 827 ms 70888 KB Output is correct
56 Correct 883 ms 71256 KB Output is correct
57 Correct 842 ms 70688 KB Output is correct
58 Correct 856 ms 71612 KB Output is correct
59 Correct 800 ms 65480 KB Output is correct
60 Correct 766 ms 71024 KB Output is correct
61 Correct 778 ms 71132 KB Output is correct
62 Correct 770 ms 71524 KB Output is correct
63 Correct 779 ms 71068 KB Output is correct
64 Correct 802 ms 71388 KB Output is correct
65 Correct 826 ms 71560 KB Output is correct
66 Correct 826 ms 71516 KB Output is correct
67 Correct 796 ms 70748 KB Output is correct
68 Correct 784 ms 71768 KB Output is correct
69 Correct 770 ms 70660 KB Output is correct
70 Correct 797 ms 71260 KB Output is correct
71 Correct 784 ms 71140 KB Output is correct
72 Correct 774 ms 71260 KB Output is correct
73 Correct 774 ms 71128 KB Output is correct
74 Correct 736 ms 64648 KB Output is correct
75 Correct 709 ms 64104 KB Output is correct
76 Correct 747 ms 70876 KB Output is correct
77 Correct 721 ms 69844 KB Output is correct
78 Correct 725 ms 70124 KB Output is correct
79 Correct 767 ms 70360 KB Output is correct
80 Correct 733 ms 70192 KB Output is correct
81 Correct 737 ms 69832 KB Output is correct
82 Correct 729 ms 69416 KB Output is correct
83 Correct 736 ms 70696 KB Output is correct
84 Correct 785 ms 70416 KB Output is correct
85 Correct 730 ms 70596 KB Output is correct
86 Correct 729 ms 69852 KB Output is correct
87 Correct 731 ms 70616 KB Output is correct
88 Correct 743 ms 70104 KB Output is correct
89 Correct 745 ms 63912 KB Output is correct
90 Correct 741 ms 68224 KB Output is correct
91 Correct 756 ms 68776 KB Output is correct
92 Correct 736 ms 68152 KB Output is correct
93 Correct 731 ms 68144 KB Output is correct
94 Correct 733 ms 68524 KB Output is correct
95 Correct 747 ms 68684 KB Output is correct
96 Correct 748 ms 68416 KB Output is correct
97 Correct 739 ms 68136 KB Output is correct
98 Correct 749 ms 68260 KB Output is correct
99 Correct 765 ms 68392 KB Output is correct
100 Correct 759 ms 68404 KB Output is correct
101 Correct 738 ms 68136 KB Output is correct
102 Correct 749 ms 68392 KB Output is correct