Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.55 KB | None | 0 0
  1. package codechef;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. public class Main4 {
  7.     public static void main(String[] args) {
  8.         StdIn in = new StdIn();
  9.         StringBuilder output = new StringBuilder();
  10.         int n=in.nextInt(), q=in.nextInt(), sqrt=(int)(Math.sqrt(n)+0.0001), maxDist=100;
  11.         long[] a = new long[n], a2 = new long[(n-1)/sqrt+1];
  12.         for(int i=0; i<n; ++i)
  13.             a[i] = in.nextLong();
  14.         int[] nxt = new int[n], nxt2 = new int[n], jmps = new int[n];
  15.         Stack<Integer> stack = new Stack<Integer>();
  16.         for(int i=n-1; i>=0; --i) {
  17.             int top=-1;
  18.             while(!stack.isEmpty()&&(a[(top=stack.peek())]<=a[i]||top-i>maxDist))
  19.                 stack.pop();
  20.             if(stack.isEmpty())
  21.                 nxt[i]=-1;
  22.             else
  23.                 nxt[i]=top;
  24.             stack.add(i);
  25.         }
  26.         stack.clear();
  27.         for(int i=0; i<a2.length; ++i) {
  28.             for(int j=Math.min(sqrt-1, n-sqrt*i-1); j>=0; --j) {
  29.                 int top=-1;
  30.                 while(!stack.isEmpty()&&(a[(top=stack.peek())]<=a[i*sqrt+j]||top-(i*sqrt+j)>maxDist))
  31.                     stack.pop();
  32.                 if(stack.isEmpty()) {
  33.                     nxt2[i*sqrt+j]=-1;
  34.                     jmps[i*sqrt+j]=0;
  35.                 } else {
  36.                     nxt2[i*sqrt+j]=nxt2[top]==-1?top:nxt2[top];
  37.                     jmps[i*sqrt+j]=jmps[top]+1;
  38.                 }
  39.                 stack.add(i*sqrt+j);
  40.             }
  41.             stack.clear();
  42.         }
  43.         for(int q_i=0; q_i<q; ++q_i) {
  44.             if(in.nextInt()==1) {
  45.                 int i=in.nextInt()-1, k=in.nextInt();
  46.                 while(k>0) {
  47.                     if(nxt2[i]==-1||jmps[i]>k) {
  48.                         if(nxt[i]==-1)
  49.                             break;
  50.                         else {
  51.                             i=nxt[i];
  52.                             --k;
  53.                         }
  54.                     } else {
  55.                         k-=jmps[i];
  56.                         i=nxt2[i];
  57.                     }
  58.                 }
  59.                 output.append(i+1).append('\n');
  60.             } else {
  61.                 int l=in.nextInt()-1, r=in.nextInt()-1, x=in.nextInt(), lB=(l-1)/sqrt+1, rB=(r+1)/sqrt-1;
  62.                 int i2=0;
  63.                 while((i2+1)*sqrt<=l-1) {
  64.                     a2[i2]-=x;
  65.                     ++i2;
  66.                 }
  67.                 i2*=sqrt;
  68.                 for(; i2<=l-1; ++i2)
  69.                     a[i2]-=x;
  70.                 i2=0;
  71.                 while((i2+1)*sqrt<=r) {
  72.                     a2[i2]+=x;
  73.                     ++i2;
  74.                 }
  75.                 i2*=sqrt;
  76.                 for(; i2<=r; ++i2)
  77.                     a[i2]+=x;
  78.                 /*for(int i=0; i<n; ++i)
  79.                     System.out.print(a[i]+a2[i/sqrt]+" ");
  80.                 System.out.println();*/
  81.                 for(int i=Math.min(l+maxDist, n-1); i>=Math.max(l-maxDist, 0); --i) {
  82.                     int top=-1;
  83.                     while(!stack.isEmpty()&&(a[(top=stack.peek())]+a2[top/sqrt]<=a[i]+a2[i/sqrt]||top-i>maxDist))
  84.                         stack.pop();
  85.                     if(i<=l) {
  86.                         if(stack.isEmpty())
  87.                             nxt[i]=-1;
  88.                         else
  89.                             nxt[i]=top;
  90.                     }
  91.                     stack.push(i);
  92.                 }
  93.                 stack.clear();
  94.                 for(int i=Math.min(r+maxDist, n-1); i>=Math.max(r-maxDist, 0); --i) {
  95.                     int top=-1;
  96.                     while(!stack.isEmpty()&&(a[(top=stack.peek())]+a2[top/sqrt]<=a[i]+a2[i/sqrt]||top-i>maxDist))
  97.                         stack.pop();
  98.                     if(i<=r) {
  99.                         if(stack.isEmpty())
  100.                             nxt[i]=-1;
  101.                         else
  102.                             nxt[i]=top;
  103.                     }
  104.                     stack.push(i);
  105.                 }
  106.                 stack.clear();
  107.                 for(int i=Math.max((l-maxDist)/sqrt, 0); i<=l/sqrt; ++i) {
  108.                     for(int j=Math.min(sqrt-1, n-sqrt*i-1); j>=0; --j) {
  109.                         int top=-1, newI=i*sqrt+j;
  110.                         while(!stack.isEmpty()&&(a[(top=stack.peek())]<=a[newI]||top-(newI)>maxDist))
  111.                             stack.pop();
  112.                         if(stack.isEmpty()) {
  113.                             nxt2[newI]=-1;
  114.                             jmps[newI]=0;
  115.                         } else {
  116.                             nxt2[newI]=nxt2[top]==-1?top:nxt2[top];
  117.                             jmps[newI]=jmps[top]+1;
  118.                         }
  119.                         stack.push(newI);
  120.                     }
  121.                     stack.clear();
  122.                 }
  123.                 for(int i=Math.max((r-maxDist)/sqrt, 0); i<=r/sqrt; ++i) {
  124.                     for(int j=Math.min(sqrt-1, n-sqrt*i-1); j>=0; --j) {
  125.                         int top=-1, newI=i*sqrt+j;
  126.                         while(!stack.isEmpty()&&(a[(top=stack.peek())]<=a[newI]||top-(newI)>maxDist))
  127.                             stack.pop();
  128.                         if(stack.isEmpty()) {
  129.                             nxt2[newI]=-1;
  130.                             jmps[newI]=0;
  131.                         } else {
  132.                             nxt2[newI]=nxt2[top]==-1?top:nxt2[top];
  133.                             jmps[newI]=jmps[top]+1;
  134.                         }
  135.                         stack.push(newI);
  136.                     }
  137.                     stack.clear();
  138.                 }
  139.             }
  140.         }
  141.         System.out.println(output);
  142.     }
  143.    
  144.     interface Input {
  145.         public String next();
  146.         public String nextLine();
  147.         public int nextInt();
  148.         public long nextLong();
  149.         public double nextDouble();
  150.     }
  151.     static class StdIn implements Input {
  152.         final private int BUFFER_SIZE = 1 << 16;
  153.         private DataInputStream din;
  154.         private byte[] buffer;
  155.         private int bufferPointer, bytesRead;
  156.  
  157.         public StdIn() {
  158.             din = new DataInputStream(System.in);
  159.             buffer = new byte[BUFFER_SIZE];
  160.             bufferPointer = bytesRead = 0;
  161.         }
  162.        
  163.         public StdIn(String filename) {
  164.             try{
  165.                 din = new DataInputStream(new FileInputStream(filename));
  166.             } catch(Exception e) {
  167.                 throw new RuntimeException();
  168.             }
  169.             buffer = new byte[BUFFER_SIZE];
  170.             bufferPointer = bytesRead = 0;
  171.         }
  172.        
  173.         public String next() {
  174.             int c;
  175.             while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r'));
  176.             StringBuilder s = new StringBuilder();
  177.             while (c != -1)
  178.             {
  179.                 if (c == ' ' || c == '\n'||c=='\r')
  180.                     break;
  181.                 s.append((char)c);
  182.                 c=read();
  183.             }
  184.             return s.toString();
  185.         }
  186.  
  187.         public String nextLine() {
  188.             int c;
  189.             while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r'));
  190.             StringBuilder s = new StringBuilder();
  191.             while (c != -1)
  192.             {
  193.                 if (c == '\n'||c=='\r')
  194.                     break;
  195.                 s.append((char)c);
  196.                 c = read();
  197.             }
  198.             return s.toString();
  199.         }
  200.  
  201.         public int nextInt() {
  202.             int ret = 0;
  203.             byte c = read();
  204.             while (c <= ' ')
  205.                 c = read();
  206.             boolean neg = (c == '-');
  207.             if (neg)
  208.                 c = read();
  209.             do
  210.             {
  211.                 ret = ret * 10 + c - '0';
  212.             }  while ((c = read()) >= '0' && c <= '9');
  213.  
  214.             if (neg)
  215.                 return -ret;
  216.             return ret;
  217.         }
  218.  
  219.         public long nextLong() {
  220.             long ret = 0;
  221.             byte c = read();
  222.             while (c <= ' ')
  223.                 c = read();
  224.             boolean neg = (c == '-');
  225.             if (neg)
  226.                 c = read();
  227.             do {
  228.                 ret = ret * 10 + c - '0';
  229.             }
  230.             while ((c = read()) >= '0' && c <= '9');
  231.             if (neg)
  232.                 return -ret;
  233.             return ret;
  234.         }
  235.  
  236.         public double nextDouble() {
  237.             double ret = 0, div = 1;
  238.             byte c = read();
  239.             while (c <= ' ')
  240.                 c = read();
  241.             boolean neg = (c == '-');
  242.             if (neg)
  243.                 c = read();
  244.  
  245.             do {
  246.                 ret = ret * 10 + c - '0';
  247.             }
  248.             while ((c = read()) >= '0' && c <= '9');
  249.  
  250.             if (c == '.')
  251.             {
  252.                 while ((c = read()) >= '0' && c <= '9')
  253.                 {
  254.                     ret += (c - '0') / (div *= 10);
  255.                 }
  256.             }
  257.  
  258.             if (neg)
  259.                 return -ret;
  260.             return ret;
  261.         }
  262.  
  263.         private void fillBuffer() throws IOException {
  264.             bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  265.             if (bytesRead == -1)
  266.                 buffer[0] = -1;
  267.         }
  268.  
  269.         private byte read() {
  270.             try{
  271.                 if (bufferPointer == bytesRead)
  272.                     fillBuffer();
  273.                 return buffer[bufferPointer++];
  274.             } catch(IOException e) {
  275.                 throw new RuntimeException();
  276.             }
  277.         }
  278.  
  279.         public void close() throws IOException {
  280.             if (din == null)
  281.                 return;
  282.             din.close();
  283.         }
  284.     }
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement