Monotonic stack, preprocessing To[i] represents the first smaller number than the a[i], always jump on it.
The problem is data water.
Here to learn the next monotonous stack.
Constructs a monotonically incrementing stack, and records its ID while recording the element size.
Every time a small element comes in, the stack is out, and the to[id of this element of the stack] = i, because this element is the largest at the time. Then this a[i] is the first to let it out of the stack, so that's it. Behind the same.
#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#defineIOS Ios::sync_with_stdio (False)using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>Const intMAXN = 1e5 + -;intA[MAXN];intTO[MAXN];structNode {intID; intVal;} STACK[MAXN];voidWork () {intN; scanf ("%d", &N); for(inti =1; I <= N; ++i) {scanf ("%d", &A[i]); } inttop =1; A[n+1] = -1; Stack[top].val= a[1]; Stack[top].id=1; for(inti =2; I <= n +1; ++i) { while(Top >=1&& A[i] <=stack[top].val) {To[stack[top].id]=i; Top--; } ++top; Stack[top].val=A[i]; Stack[top].id=i; }//for (int i = 1; I <= n; ++i) {//printf ("%d", To[i]);// }//printf ("\ n"); intm; scanf ("%d", &m); for(inti =1; I <= m; ++i) {intL, R; scanf ("%d%d", &l, &R); intAns =A[l]; intt =To[l]; while(T <=R) {ans%=A[t]; if(ans = =0) Break; T=To[t]; } printf ("%d\n", ans); }}intMain () {#ifdef local freopen ("Data.txt","R", stdin);#endif intT; scanf ("%d", &t); while(t--) work (); return 0;}
View Code
HDU 5875 h-function with a monotonous stack of water.