/* [Author: Nguyen Ngoc Hung] - From THPT Ngo Gia Tu with Love */
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <locale>
#include <map>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <unordered_set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
#include <cstring>
#include <unordered_map>
#include <cmath>
#include <array>
#include <cassert>
#include <random>
#include <chrono>
using namespace std;
#define __nnhzzz__ signed main()
#define BIT(i,j) ((i>>j)&1LL)
#define MASK(i) (1LL<<i)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) (int)(x).size()
#define _PB push_back
#define fi first
#define se second
#define ll long long
#define ld long double
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define pii pair<int,int>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define REPDIS(i,be,en,j) for(int i = (be); i<=(en); i+=j)
#define REPD(i,be,en) for(int i = (be); i>=(en); i--)
#define REP(i,be,en) for(int i = (be); i<=(en); i++)
#define endl "\n"
#define MP make_pair
#define int ll
//-----------------------------------------------------------------------------------------------//
int readInt(){
char c;
do{ c = getchar(); }while(c!='-' && !isdigit(c));
bool neg = (c=='-');
int res = neg?0:c-'0';
while(isdigit(c=getchar())) res = (res<<3)+(res<<1)+(c-'0');
return neg?-res:res;
}
//------------------------------------------------------------------------------------------------//
const ll LINF = 1e18;
const int INF = 1e9;
const int LOG = 20;
const int MAXN = 1e6+3;
const int N = 1e2+3;
const int MOD = 1e9+7;
const int BASE = 1e5;
const ld EPS = 1e-9;
const ld PI = acos(-1);
const int OFFSET = 1e3;
//------------------------------------------------------------------------------------------------//
template<typename T1, typename T2> bool mini(T1 &a, T2 b){if(a>b){a=b;return true;}return false;}
template<typename T1, typename T2> bool maxi(T1 &a, T2 b){if(a<b){a=b;return true;}return false;}
template<typename T> T gcd(T a, T b) { while(b) { a %= b; swap(a,b); } return a; }
template<typename T> T lcm(T a, T b) { return a/gcd(a,b)*b; }
//------------------------------------------------------------------------------------------------//
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Nguyen Ngoc Hung - nnhzzz
Training for VOI24 gold medal
----------------------------------------------------------------
*/
int a[MAXN],res[MAXN],q[MAXN];
int n,query;
namespace sub3{
const int MAX = 2e2+3;
int f[MAX][MAX];
void solve(){
REP(i,1,n){
int ma = 0;
REP(j,i,n){
maxi(ma,a[j]);
f[i][j] = ma;
}
}
REP(t,1,query){
int x = q[t],res = 0;
REP(i,1,n){
REP(j,i,n){
if(f[i][j]>x) break;
++res;
}
}
cout << res << endl;
}
}
}
namespace sub11{
int sum = 0;
vi adj[MAXN],g[MAXN];
struct DSU{
vi par,s;
int n;
int find(int u){
while(u!=par[u]) u = par[u] = par[par[u]];
return u;
}
void unite(int u, int v){
if(min(u,v)<1 || max(u,v)>n) return ;
u = find(u); v = find(v);
if(u==v) return ;
sum += 1LL*s[u]*s[v];
if(s[u]<s[v]) swap(u,v);
s[u] += s[v];
par[v] = u;
}
DSU(int _n):n(_n){
par.resize(n+3); iota(ALL(par),0);
s.assign(n+3,1);
}
};
void solve(){
REP(i,1,n) adj[a[i]].push_back(i);
REP(i,1,query) g[q[i]].push_back(i);
DSU dsu(n);
REP(x,1,MAXN-1){
for(int &i:adj[x]){
if(a[i-1]<=x) dsu.unite(i-1,i);
if(a[i+1]<=x) dsu.unite(i,i+1);
}
sum += SZ(adj[x]);
for(int &i:g[x]) res[i] = sum;
}
REP(i,1,query) cout << res[i] << endl;
}
}
void solve(){
// cin >> n >> query;
n = readInt(); query = readInt();
REP(i,1,n) a[i] = readInt();
REP(i,1,query) q[i] = readInt();
sub11::solve(); return ;
if(n<=200 && query<=200){
sub3::solve();
return ;
}
sub11::solve();
}
__nnhzzz__{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define task "test"
if(fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
#define task1 "nnhzzz"
if(fopen(task1".inp","r")){
freopen(task1".inp","r",stdin);
freopen(task1".out","w",stdout);
}
int test = 1;
while(test--){
solve();
}
cerr << "\nTime elapsed: " << 1000*clock()/CLOCKS_PER_SEC << "ms\n";
return 0;
}
Compilation message
pilot.cpp: In function 'int main()':
pilot.cpp:192:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
192 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
pilot.cpp:193:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
193 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
pilot.cpp:197:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
197 | freopen(task1".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
pilot.cpp:198:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
198 | freopen(task1".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
51800 KB |
Output is correct |
2 |
Correct |
14 ms |
51804 KB |
Output is correct |
3 |
Correct |
14 ms |
51944 KB |
Output is correct |
4 |
Correct |
15 ms |
51804 KB |
Output is correct |
5 |
Correct |
14 ms |
51804 KB |
Output is correct |
6 |
Correct |
14 ms |
51800 KB |
Output is correct |
7 |
Correct |
14 ms |
51936 KB |
Output is correct |
8 |
Correct |
13 ms |
51804 KB |
Output is correct |
9 |
Correct |
13 ms |
51804 KB |
Output is correct |
10 |
Correct |
13 ms |
51940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
51800 KB |
Output is correct |
2 |
Correct |
14 ms |
51804 KB |
Output is correct |
3 |
Correct |
14 ms |
51944 KB |
Output is correct |
4 |
Correct |
15 ms |
51804 KB |
Output is correct |
5 |
Correct |
14 ms |
51804 KB |
Output is correct |
6 |
Correct |
14 ms |
51800 KB |
Output is correct |
7 |
Correct |
14 ms |
51936 KB |
Output is correct |
8 |
Correct |
13 ms |
51804 KB |
Output is correct |
9 |
Correct |
13 ms |
51804 KB |
Output is correct |
10 |
Correct |
13 ms |
51940 KB |
Output is correct |
11 |
Correct |
13 ms |
51800 KB |
Output is correct |
12 |
Correct |
14 ms |
51928 KB |
Output is correct |
13 |
Correct |
15 ms |
51800 KB |
Output is correct |
14 |
Correct |
14 ms |
52060 KB |
Output is correct |
15 |
Correct |
14 ms |
51860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
51800 KB |
Output is correct |
2 |
Correct |
14 ms |
51804 KB |
Output is correct |
3 |
Correct |
14 ms |
51944 KB |
Output is correct |
4 |
Correct |
15 ms |
51804 KB |
Output is correct |
5 |
Correct |
14 ms |
51804 KB |
Output is correct |
6 |
Correct |
14 ms |
51800 KB |
Output is correct |
7 |
Correct |
14 ms |
51936 KB |
Output is correct |
8 |
Correct |
13 ms |
51804 KB |
Output is correct |
9 |
Correct |
13 ms |
51804 KB |
Output is correct |
10 |
Correct |
13 ms |
51940 KB |
Output is correct |
11 |
Correct |
13 ms |
51800 KB |
Output is correct |
12 |
Correct |
14 ms |
51928 KB |
Output is correct |
13 |
Correct |
15 ms |
51800 KB |
Output is correct |
14 |
Correct |
14 ms |
52060 KB |
Output is correct |
15 |
Correct |
14 ms |
51860 KB |
Output is correct |
16 |
Correct |
14 ms |
51804 KB |
Output is correct |
17 |
Correct |
14 ms |
51856 KB |
Output is correct |
18 |
Correct |
13 ms |
51800 KB |
Output is correct |
19 |
Correct |
14 ms |
51804 KB |
Output is correct |
20 |
Correct |
14 ms |
51952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
51800 KB |
Output is correct |
2 |
Correct |
14 ms |
51804 KB |
Output is correct |
3 |
Correct |
14 ms |
51944 KB |
Output is correct |
4 |
Correct |
15 ms |
51804 KB |
Output is correct |
5 |
Correct |
14 ms |
51804 KB |
Output is correct |
6 |
Correct |
14 ms |
51800 KB |
Output is correct |
7 |
Correct |
14 ms |
51936 KB |
Output is correct |
8 |
Correct |
13 ms |
51804 KB |
Output is correct |
9 |
Correct |
13 ms |
51804 KB |
Output is correct |
10 |
Correct |
13 ms |
51940 KB |
Output is correct |
11 |
Correct |
13 ms |
51800 KB |
Output is correct |
12 |
Correct |
14 ms |
51928 KB |
Output is correct |
13 |
Correct |
15 ms |
51800 KB |
Output is correct |
14 |
Correct |
14 ms |
52060 KB |
Output is correct |
15 |
Correct |
14 ms |
51860 KB |
Output is correct |
16 |
Correct |
14 ms |
51804 KB |
Output is correct |
17 |
Correct |
14 ms |
51856 KB |
Output is correct |
18 |
Correct |
13 ms |
51800 KB |
Output is correct |
19 |
Correct |
14 ms |
51804 KB |
Output is correct |
20 |
Correct |
14 ms |
51952 KB |
Output is correct |
21 |
Correct |
15 ms |
51804 KB |
Output is correct |
22 |
Correct |
14 ms |
51804 KB |
Output is correct |
23 |
Correct |
15 ms |
52056 KB |
Output is correct |
24 |
Correct |
14 ms |
52032 KB |
Output is correct |
25 |
Correct |
14 ms |
51804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
55900 KB |
Output is correct |
2 |
Correct |
42 ms |
57056 KB |
Output is correct |
3 |
Correct |
26 ms |
55892 KB |
Output is correct |
4 |
Correct |
41 ms |
56516 KB |
Output is correct |
5 |
Correct |
26 ms |
55864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
64868 KB |
Output is correct |
2 |
Correct |
45 ms |
65168 KB |
Output is correct |
3 |
Correct |
47 ms |
65024 KB |
Output is correct |
4 |
Correct |
47 ms |
65104 KB |
Output is correct |
5 |
Correct |
47 ms |
64872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
65104 KB |
Output is correct |
2 |
Correct |
60 ms |
65080 KB |
Output is correct |
3 |
Correct |
59 ms |
65228 KB |
Output is correct |
4 |
Correct |
51 ms |
65368 KB |
Output is correct |
5 |
Correct |
53 ms |
65376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
51800 KB |
Output is correct |
2 |
Correct |
14 ms |
51804 KB |
Output is correct |
3 |
Correct |
14 ms |
51944 KB |
Output is correct |
4 |
Correct |
15 ms |
51804 KB |
Output is correct |
5 |
Correct |
14 ms |
51804 KB |
Output is correct |
6 |
Correct |
14 ms |
51800 KB |
Output is correct |
7 |
Correct |
14 ms |
51936 KB |
Output is correct |
8 |
Correct |
13 ms |
51804 KB |
Output is correct |
9 |
Correct |
13 ms |
51804 KB |
Output is correct |
10 |
Correct |
13 ms |
51940 KB |
Output is correct |
11 |
Correct |
26 ms |
55900 KB |
Output is correct |
12 |
Correct |
42 ms |
57056 KB |
Output is correct |
13 |
Correct |
26 ms |
55892 KB |
Output is correct |
14 |
Correct |
41 ms |
56516 KB |
Output is correct |
15 |
Correct |
26 ms |
55864 KB |
Output is correct |
16 |
Correct |
25 ms |
55644 KB |
Output is correct |
17 |
Correct |
42 ms |
56912 KB |
Output is correct |
18 |
Correct |
43 ms |
56924 KB |
Output is correct |
19 |
Correct |
26 ms |
55776 KB |
Output is correct |
20 |
Correct |
43 ms |
56924 KB |
Output is correct |
21 |
Correct |
26 ms |
55644 KB |
Output is correct |
22 |
Correct |
39 ms |
56524 KB |
Output is correct |
23 |
Correct |
27 ms |
56144 KB |
Output is correct |
24 |
Correct |
40 ms |
56464 KB |
Output is correct |
25 |
Correct |
26 ms |
56032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
51800 KB |
Output is correct |
2 |
Correct |
14 ms |
51804 KB |
Output is correct |
3 |
Correct |
14 ms |
51944 KB |
Output is correct |
4 |
Correct |
15 ms |
51804 KB |
Output is correct |
5 |
Correct |
14 ms |
51804 KB |
Output is correct |
6 |
Correct |
14 ms |
51800 KB |
Output is correct |
7 |
Correct |
14 ms |
51936 KB |
Output is correct |
8 |
Correct |
13 ms |
51804 KB |
Output is correct |
9 |
Correct |
13 ms |
51804 KB |
Output is correct |
10 |
Correct |
13 ms |
51940 KB |
Output is correct |
11 |
Correct |
13 ms |
51800 KB |
Output is correct |
12 |
Correct |
14 ms |
51928 KB |
Output is correct |
13 |
Correct |
15 ms |
51800 KB |
Output is correct |
14 |
Correct |
14 ms |
52060 KB |
Output is correct |
15 |
Correct |
14 ms |
51860 KB |
Output is correct |
16 |
Correct |
14 ms |
51804 KB |
Output is correct |
17 |
Correct |
14 ms |
51856 KB |
Output is correct |
18 |
Correct |
13 ms |
51800 KB |
Output is correct |
19 |
Correct |
14 ms |
51804 KB |
Output is correct |
20 |
Correct |
14 ms |
51952 KB |
Output is correct |
21 |
Correct |
15 ms |
51804 KB |
Output is correct |
22 |
Correct |
14 ms |
51804 KB |
Output is correct |
23 |
Correct |
15 ms |
52056 KB |
Output is correct |
24 |
Correct |
14 ms |
52032 KB |
Output is correct |
25 |
Correct |
14 ms |
51804 KB |
Output is correct |
26 |
Correct |
26 ms |
55900 KB |
Output is correct |
27 |
Correct |
42 ms |
57056 KB |
Output is correct |
28 |
Correct |
26 ms |
55892 KB |
Output is correct |
29 |
Correct |
41 ms |
56516 KB |
Output is correct |
30 |
Correct |
26 ms |
55864 KB |
Output is correct |
31 |
Correct |
46 ms |
64868 KB |
Output is correct |
32 |
Correct |
45 ms |
65168 KB |
Output is correct |
33 |
Correct |
47 ms |
65024 KB |
Output is correct |
34 |
Correct |
47 ms |
65104 KB |
Output is correct |
35 |
Correct |
47 ms |
64872 KB |
Output is correct |
36 |
Correct |
51 ms |
65104 KB |
Output is correct |
37 |
Correct |
60 ms |
65080 KB |
Output is correct |
38 |
Correct |
59 ms |
65228 KB |
Output is correct |
39 |
Correct |
51 ms |
65368 KB |
Output is correct |
40 |
Correct |
53 ms |
65376 KB |
Output is correct |
41 |
Correct |
25 ms |
55644 KB |
Output is correct |
42 |
Correct |
42 ms |
56912 KB |
Output is correct |
43 |
Correct |
43 ms |
56924 KB |
Output is correct |
44 |
Correct |
26 ms |
55776 KB |
Output is correct |
45 |
Correct |
43 ms |
56924 KB |
Output is correct |
46 |
Correct |
26 ms |
55644 KB |
Output is correct |
47 |
Correct |
39 ms |
56524 KB |
Output is correct |
48 |
Correct |
27 ms |
56144 KB |
Output is correct |
49 |
Correct |
40 ms |
56464 KB |
Output is correct |
50 |
Correct |
26 ms |
56032 KB |
Output is correct |
51 |
Correct |
67 ms |
64084 KB |
Output is correct |
52 |
Correct |
68 ms |
64896 KB |
Output is correct |
53 |
Correct |
52 ms |
64064 KB |
Output is correct |
54 |
Correct |
70 ms |
64848 KB |
Output is correct |
55 |
Correct |
55 ms |
63936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
51800 KB |
Output is correct |
2 |
Correct |
14 ms |
51804 KB |
Output is correct |
3 |
Correct |
14 ms |
51944 KB |
Output is correct |
4 |
Correct |
15 ms |
51804 KB |
Output is correct |
5 |
Correct |
14 ms |
51804 KB |
Output is correct |
6 |
Correct |
14 ms |
51800 KB |
Output is correct |
7 |
Correct |
14 ms |
51936 KB |
Output is correct |
8 |
Correct |
13 ms |
51804 KB |
Output is correct |
9 |
Correct |
13 ms |
51804 KB |
Output is correct |
10 |
Correct |
13 ms |
51940 KB |
Output is correct |
11 |
Correct |
13 ms |
51800 KB |
Output is correct |
12 |
Correct |
14 ms |
51928 KB |
Output is correct |
13 |
Correct |
15 ms |
51800 KB |
Output is correct |
14 |
Correct |
14 ms |
52060 KB |
Output is correct |
15 |
Correct |
14 ms |
51860 KB |
Output is correct |
16 |
Correct |
14 ms |
51804 KB |
Output is correct |
17 |
Correct |
14 ms |
51856 KB |
Output is correct |
18 |
Correct |
13 ms |
51800 KB |
Output is correct |
19 |
Correct |
14 ms |
51804 KB |
Output is correct |
20 |
Correct |
14 ms |
51952 KB |
Output is correct |
21 |
Correct |
15 ms |
51804 KB |
Output is correct |
22 |
Correct |
14 ms |
51804 KB |
Output is correct |
23 |
Correct |
15 ms |
52056 KB |
Output is correct |
24 |
Correct |
14 ms |
52032 KB |
Output is correct |
25 |
Correct |
14 ms |
51804 KB |
Output is correct |
26 |
Correct |
26 ms |
55900 KB |
Output is correct |
27 |
Correct |
42 ms |
57056 KB |
Output is correct |
28 |
Correct |
26 ms |
55892 KB |
Output is correct |
29 |
Correct |
41 ms |
56516 KB |
Output is correct |
30 |
Correct |
26 ms |
55864 KB |
Output is correct |
31 |
Correct |
46 ms |
64868 KB |
Output is correct |
32 |
Correct |
45 ms |
65168 KB |
Output is correct |
33 |
Correct |
47 ms |
65024 KB |
Output is correct |
34 |
Correct |
47 ms |
65104 KB |
Output is correct |
35 |
Correct |
47 ms |
64872 KB |
Output is correct |
36 |
Correct |
51 ms |
65104 KB |
Output is correct |
37 |
Correct |
60 ms |
65080 KB |
Output is correct |
38 |
Correct |
59 ms |
65228 KB |
Output is correct |
39 |
Correct |
51 ms |
65368 KB |
Output is correct |
40 |
Correct |
53 ms |
65376 KB |
Output is correct |
41 |
Correct |
25 ms |
55644 KB |
Output is correct |
42 |
Correct |
42 ms |
56912 KB |
Output is correct |
43 |
Correct |
43 ms |
56924 KB |
Output is correct |
44 |
Correct |
26 ms |
55776 KB |
Output is correct |
45 |
Correct |
43 ms |
56924 KB |
Output is correct |
46 |
Correct |
26 ms |
55644 KB |
Output is correct |
47 |
Correct |
39 ms |
56524 KB |
Output is correct |
48 |
Correct |
27 ms |
56144 KB |
Output is correct |
49 |
Correct |
40 ms |
56464 KB |
Output is correct |
50 |
Correct |
26 ms |
56032 KB |
Output is correct |
51 |
Correct |
67 ms |
64084 KB |
Output is correct |
52 |
Correct |
68 ms |
64896 KB |
Output is correct |
53 |
Correct |
52 ms |
64064 KB |
Output is correct |
54 |
Correct |
70 ms |
64848 KB |
Output is correct |
55 |
Correct |
55 ms |
63936 KB |
Output is correct |
56 |
Correct |
580 ms |
144780 KB |
Output is correct |
57 |
Correct |
580 ms |
145640 KB |
Output is correct |
58 |
Correct |
551 ms |
140708 KB |
Output is correct |
59 |
Correct |
544 ms |
142952 KB |
Output is correct |
60 |
Correct |
562 ms |
147128 KB |
Output is correct |