// This program was written on 25.05.2022
// for problem fountain
// by Mircea Rebengiuc
#include <stdio.h>
#include <ctype.h>
#include <vector>
#define MAXN 100000
int r[MAXN];
int cap[MAXN];
std::vector< std::pair<int, int> > qs[MAXN];
int qans[MAXN];
int stack[MAXN];
int psum[1 + MAXN];
int sp;
static inline int fgetint(){
int n = 0, ch;
while( !isdigit( ch = fgetc( stdin ) ) );
do
n = n * 10 + ch - '0';
while( isdigit( ch = fgetc( stdin ) ) );
return n;
}
int main(){
int n, q, i, j, st, dr, mij;
n = fgetint();
q = fgetint();
for( i = n ; i-- ; ){
r[i] = fgetint();
cap[i] = fgetint();
}
for( i = 0 ; i < q ; i++ ){
j = n - fgetint();
qs[j].push_back( std::make_pair( fgetint(), i ) );
}
sp = 0;
psum[0] = 0;
for( i = 0 ; i < n ; i++ ){
// update stack
while( sp && r[stack[sp - 1]] <= r[i] )
sp--;
stack[sp++] = i;
psum[sp] = psum[sp - 1] + cap[i];
// binary search query answers
for( auto &[vol, ansindex] : qs[i] ){
if( vol <= psum[sp] ){
st = 0;
dr = sp;
while( dr - st > 1 ){// [st, dr)
if( psum[mij = ((st + dr) >> 1)] + vol > psum[sp] )
dr = mij;
else
st = mij;
}
qans[ansindex] = n - stack[st];
}else
qans[ansindex] = 0;
}
}
for( i = 0 ; i < q ; i++ )
printf( "%d\n", qans[i] );
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2656 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
3 ms |
2664 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
64 ms |
16392 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2656 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
3 ms |
2664 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
8 |
Runtime error |
64 ms |
16392 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |