#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define in ({int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';n?-x:x;})
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r){return l+rng()%(r-l+1);}
#define fasty ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define forinc(x,a,b) for (int x=a;x<=b;x++)
#define fordec(x,a,b) for (int x=a;x>=b;x--)
#define forv(a,b) for(auto&a:b)
#define fi first
#define se second
#define pb push_back
#define ii pair<int,int>
#define all(a) a.begin(),a.end()
#define reset(f, x) memset(f, x, sizeof(f))
#define getbit(x,i) ((x>>i)&1)
#define batbit(x,i) (x|(1ll<<i))
#define tatbit(x,i) (x&~(1<<i))
#define gg exit(0);
const int N=1e6+10;
ll tab[N][21], f[N];
int a[N], n, q, lg2[N];
struct dat
{
int l, r;
} val[N];
vector<int> h[N], cons[N];
void build()
{
forinc(i,1,n) tab[i][0]=a[i];
for(int j=1; j<=lg2[n] ; j++)
{
for(int i=1; i+(1<<j)-1<=n; i++) tab[i][j]=max(tab[i][j-1],tab[i+(1<<(j-1))][j-1]);
}
}
ll query(int l, int r)
{
int j=lg2[r-l+1];
return max(tab[l][j],tab[r-(1<<j)+1][j]);
}
signed main()
{
freopen("pilot.inp","r",stdin);
freopen("pilot.out","w",stdout);
n=in; q=in;
forinc(i,1,n) a[i]=in;
forinc(i,1,n) h[a[i]].pb(i);
forinc(i,2,n) lg2[i]=lg2[i/2]+1;
build();
forinc(i,1,n)
{
int l=0,r=i-1,l_bound=0,r_bound=0;
while(l<=r)
{
int mid=(l+r)/2;
if(query(i-mid,i)<=a[i])
{
l_bound=mid+1;
l=mid+1;
}
else r=mid-1;
}
val[i].l=l_bound;
l=0,r=n-i;
while(l<=r)
{
int mid=(l+r)/2;
if(query(i,i+mid)<=a[i])
{
r_bound=mid+1;
l=mid+1;
}
else r=mid-1;
}
val[i].r=r_bound;
}
forinc(i,1,1e6)
{
f[i]=f[i-1];
int last=-1;
forv(pos,h[i])
{
if(last==-1) f[i]+=(ll)val[pos].l*val[pos].r;
else f[i]+=(ll)min(pos-last,val[pos].l)*val[pos].r;
last=pos;
}
}
while(q--) cout << f[in] << "\n";
}
Compilation message
pilot.cpp: In function 'int main()':
pilot.cpp:45:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | freopen("pilot.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
pilot.cpp:46:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | freopen("pilot.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1088 ms |
47172 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1088 ms |
47172 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1088 ms |
47172 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1088 ms |
47172 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1079 ms |
47252 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1084 ms |
47172 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1068 ms |
47172 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1088 ms |
47172 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1088 ms |
47172 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1088 ms |
47172 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |