Guest User

Untitled

a guest
Jun 12th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1.  
  2. /*
  3.  * Created by Thomas Boulbes
  4.  */
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.io.PrintWriter;
  9. import java.util.StringTokenizer;
  10.  
  11. // TODO: Auto-generated Javadoc
  12. /**
  13.  * The Class Main.
  14.  */
  15. public class Main {
  16.  
  17.     /**
  18.      * The main method.
  19.      *
  20.      * @param args
  21.      *            the arguments
  22.      * @throws IOException
  23.      *             Signals that an I/O exception has occurred.
  24.      */
  25.     public static void main(String[] args) throws IOException {
  26.         FastReader rdr = new FastReader();
  27.         PrintWriter stdout = new PrintWriter(System.out);
  28.         int T = rdr.nextInt();
  29.         while (T-- > 0) {
  30.             int n = rdr.nextInt(), index = 0, count = 0;
  31.             int[] arr = new int[n];
  32.             for (int i = 0; i < n; i++) {
  33.                 arr[i] = rdr.nextInt();
  34.             }
  35.             while (index < n) {
  36.                 int i = index, j = index;
  37.                 while (j < n && arr[i] <= arr[j]) {
  38.                     j++;
  39.                     i = j - 1;
  40.                 }
  41.                 count += (j - index + 1) * (j - index) / 2;
  42.                 index = j;
  43.             }
  44.             stdout.println(count);
  45.         }
  46.         stdout.flush();
  47.         stdout.close();
  48.     }
  49.  
  50.     //////////////////////////////////////////////////
  51.     ////////////////// +--------+ ////////////////////
  52.     ////////////////// | READER | ////////////////////
  53.     ////////////////// +--------+ ////////////////////
  54.     //////////////////////////////////////////////////
  55.  
  56.     /**
  57.      * The Class FastReader.
  58.      */
  59.     static class FastReader {
  60.  
  61.         /** The br. */
  62.         BufferedReader br;
  63.  
  64.         /** The st. */
  65.         StringTokenizer st;
  66.  
  67.         /**
  68.          * Instantiates a new fast reader.
  69.          */
  70.         public FastReader() {
  71.             br = new BufferedReader(new InputStreamReader(System.in));
  72.         }
  73.  
  74.         /**
  75.          * Next.
  76.          *
  77.          * @return the string
  78.          */
  79.         String next() {
  80.             while (st == null || !st.hasMoreElements()) {
  81.                 try {
  82.                     st = new StringTokenizer(br.readLine());
  83.                 } catch (IOException e) {
  84.                     e.printStackTrace();
  85.                 }
  86.             }
  87.             return st.nextToken();
  88.         }
  89.  
  90.         /**
  91.          * Next int.
  92.          *
  93.          * @return the int
  94.          */
  95.         int nextInt() {
  96.             return Integer.parseInt(next());
  97.         }
  98.  
  99.         /**
  100.          * Next long.
  101.          *
  102.          * @return the long
  103.          */
  104.         long nextLong() {
  105.             return Long.parseLong(next());
  106.         }
  107.  
  108.         /**
  109.          * Next double.
  110.          *
  111.          * @return the double
  112.          */
  113.         double nextDouble() {
  114.             return Double.parseDouble(next());
  115.         }
  116.  
  117.         /**
  118.          * Next line.
  119.          *
  120.          * @return the string
  121.          */
  122.         String nextLine() {
  123.             String str = "";
  124.             try {
  125.                 str = br.readLine();
  126.             } catch (IOException e) {
  127.                 e.printStackTrace();
  128.             }
  129.             return str;
  130.         }
  131.     }
  132.  
  133. }
Add Comment
Please, Sign In to add comment