HDU 5875 h-function with a monotonous stack of water.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.