-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathraman_stack.cpp
More file actions
190 lines (170 loc) · 5.31 KB
/
raman_stack.cpp
File metadata and controls
190 lines (170 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define sp ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define cps CLOCKS_PER_SEC
#define mod (ll)1000000007
#define f first
#define s second
#define debug1(x) cerr<<x<<"\n"
#define debug2(x,y) cerr<<x<<" "<<y<<"\n"
#define debug3(x,y,z) cerr<<x<<" "<<y<<" "<<z<<"\n"
#define nl cerr<<"\n";
#define pq priority_queue
#define inf 0x3f3f3f3f
#define test cerr<<"abcd\n";
#define pi pair<ll,ll>
#define pii pair<int,pi>
#define pb push_back
#define mxn 200005
ll a[mxn],l1[mxn],l2[mxn],r1[mxn],r2[mxn];
unordered_map<ll,ll> mp1,mp2;
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cout << *it << " =: " << a << endl;
err(++it, args...);
}
template <typename T1, typename T2>
inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& buf) {
return os << "(" << buf.first << ": " << buf.second << ")";
}
template<typename T>
inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v) {
bool first = true;
os << "[";
for(unsigned int i = 0; i < v.size(); i++) {
if(!first) os << ", ";
os << v[i];
first = false;
}
return os << "]";
}
template<typename T>
inline std::ostream &operator << (std::ostream & os,const std::set<T>& v) {
bool first = true;
os << "{";
for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) {
if(!first) os << ", ";
os << *ii;
first = false;
}
return os << "}";
}
template<typename T1, typename T2>
inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v) {
bool first = true;
os << "{";
for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii) {
if(!first) os << ", ";
os << *ii ;
first = false;
}
return os << "}";
}
template<typename T>
inline std::ostream &operator << (std::ostream & os,const vector<vector<T>>& v) {
os << '\n';
for (auto &u: v) os << u << '\n';
return os;
}
#define dbg1(x) cout << #x << ": " << x << endl;
#define dbg2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << endl;
#define dbg3(x, y, z) cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define dbg4(a, b, c, d) cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define dbg5(a, b, c, d, e) cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define dbg6(a, b, c, d, e, f) cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define dbgv(x) cout << #x << " = " << x << endl
#define dbga(arr, len) {cout << #arr << " = "; for (int _ = 0; _ < len; _++)\
cout << arr[_] << " "; cout << endl;}
#define dbgi(it) {cout << #it << " = "; for (const auto& _ : it)\
cout << _ << " "; cout << endl;}
void showstack(stack <int> s)
{
while (!s.empty())
{
cout << ' ' << s.top();
s.pop();
}
cout << '\n';
}
int main(){
sp;
int n,q;
cin>>n>>q;
for(int i=0; i<n; ++i){
cin>>a[i];
}
ll x;
stack<int> s;
for(int i=0; i<n; ++i){
l1[i]=l2[i]=-1;
r1[i]=r2[i]=n;
}
// calculate min upto iteration
// arr[] agar chota hai toh keep popping
for(int i=0; i<n; ++i){
while(!s.empty()&&a[s.top()]>=a[i])
s.pop();
if(!s.empty())l1[i]=s.top();
s.push(i);
showstack(s);
}
dbga(l1,n);
while(!s.empty())s.pop();
// calculate min backwards
for(int i=n-1; i>=0; --i){
while(!s.empty()&&a[s.top()]>a[i])
s.pop();
if(!s.empty())r1[i]=s.top();
s.push(i);
showstack(s);
}
dbga(r1,n);
while(!s.empty())s.pop();
// calculate max upto iteration
for(int i=0; i<n; ++i){
while(!s.empty()&&a[s.top()]<=a[i])
s.pop();
if(!s.empty())l2[i]=s.top();
s.push(i);
showstack(s);
}
dbga(l2,n);
while(!s.empty())s.pop();
// calculate max backwards
for(int i=n-1; i>=0; --i){
while(!s.empty()&&a[s.top()]<a[i])
s.pop();
if(!s.empty())r2[i]=s.top();
s.push(i);
showstack(s);
}
dbga(r2,n);
// min calculation
for(int i=0; i<n; ++i){
ll a1=i-l1[i]-1;
ll a2=r1[i]-i-1;
ll p=1LL*a1*a2+a1+a2+1;
mp1[a[i]]+=p;
// dbg5(a1, a2, mp1[a[i]], l1[i], r1[i]);
}
// max calculation
for(int i=0; i<n; ++i){
ll a1=i-l2[i]-1;
ll a2=r2[i]-i-1;
ll p=1LL*a1*a2+a1+a2+1;
mp2[a[i]]+=p;
// dbg5(a1, a2, mp2[a[i]], l2[i], r2[i]);
}
while(q--){
cin>>x;
assert(x>=1&&x<=1000000000);
if(mp1.count(x)==0)
cout<<"0 0\n";
else cout<<mp2[x]<<" "<<mp1[x]<<"\n";
}
return 0;
}