qwerty787788

SQRT v2

Aug 21st, 2020
1,559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.83 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Arrays;
  3. import java.util.Comparator;
  4. import java.util.StringTokenizer;
  5.  
  6. public class SolSqrt {
  7.     FastScanner in;
  8.     PrintWriter out;
  9.  
  10.     class Cell {
  11.         long currentStart;
  12.         long atLeast;
  13.         int pos;
  14.  
  15.         public Cell(long currentStart, long atLeast, int pos) {
  16.             this.currentStart = currentStart;
  17.             this.atLeast = atLeast;
  18.             this.pos = pos;
  19.         }
  20.  
  21.         public long getValue(long currentAdd) {
  22.             return Math.max(atLeast, currentStart + currentAdd);
  23.         }
  24.     }
  25.  
  26.     class Block {
  27.         int l, r;
  28.         long currentAdd;
  29.         Cell[] cells;
  30.         long[] minCurrentStartPrefix;
  31.         long[] minAtLeast;
  32.  
  33.         void sortAndComputePrefixSums() {
  34.             Arrays.sort(cells, new Comparator<Cell>() {
  35.                 @Override
  36.                 public int compare(Cell o1, Cell o2) {
  37.                     return Long.compare(o1.atLeast - o1.currentStart, o2.atLeast - o2.currentStart);
  38.                 }
  39.             });
  40.             minCurrentStartPrefix[0] = Long.MAX_VALUE / 2;
  41.             for (int i = 0; i < cells.length; i++) {
  42.                 minCurrentStartPrefix[i + 1] = Math.min(minCurrentStartPrefix[i], cells[i].currentStart);
  43.             }
  44.             minAtLeast[cells.length] = Long.MAX_VALUE / 2;
  45.             for (int i = cells.length - 1; i >= 0; i--) {
  46.                 minAtLeast[i] = Math.min(minAtLeast[i + 1], cells[i].atLeast);
  47.             }
  48.         }
  49.  
  50.         long getMinWholeArray() {
  51.             // find last pos: currentStart + currentAdd >= atLeast
  52.             int l = -1, r = cells.length;
  53.             while (r - l > 1) {
  54.                 int m = (l + r) >> 1;
  55.                 if (cells[m].currentStart + currentAdd >= cells[m].atLeast) {
  56.                     l = m;
  57.                 } else {
  58.                     r = m;
  59.                 }
  60.             }
  61.             return Math.min(minCurrentStartPrefix[r] + currentAdd, minAtLeast[r]);
  62.         }
  63.  
  64.         long getMin(int lQuery, int rQuery) {
  65.             if (lQuery > r || rQuery < l) {
  66.                 return Long.MAX_VALUE;
  67.             }
  68.             if (lQuery <= l && rQuery >= r) {
  69.                 return getMinWholeArray();
  70.             }
  71.             long min = Long.MAX_VALUE;
  72.             for (Cell c : cells) {
  73.                 if (c.pos >= lQuery && c.pos <= rQuery) {
  74.                     min = Math.min(min, c.getValue(currentAdd));
  75.                 }
  76.             }
  77.             return min;
  78.         }
  79.  
  80.         void add(int lQuery, int rQuery, long delta) {
  81.             if (lQuery > r || rQuery < l) {
  82.                 return;
  83.             }
  84.             if (lQuery <= l && rQuery >= r) {
  85.                 currentAdd += delta;
  86.                 return;
  87.             }
  88.             for (Cell c : cells) {
  89.                 if (c.pos >= lQuery && c.pos <= rQuery) {
  90.                     c.currentStart += delta;
  91.                 }
  92.             }
  93.             sortAndComputePrefixSums();
  94.         }
  95.  
  96.         Block(long[] currentStart, long[] atLeast, int l, int r) {
  97.             this.l = l;
  98.             this.r = r;
  99.             cells = new Cell[r - l + 1];
  100.             for (int i = 0; i < cells.length; i++) {
  101.                 int pos = l + i;
  102.                 cells[i] = new Cell(currentStart[pos], atLeast[pos], pos);
  103.             }
  104.             minCurrentStartPrefix = new long[cells.length + 1];
  105.             minAtLeast = new long[cells.length + 1];
  106.             sortAndComputePrefixSums();
  107.         }
  108.  
  109.     }
  110.  
  111.     void solve() {
  112.         int n = in.nextInt();
  113.         int q = in.nextInt();
  114.         long[] expected = new long[n];
  115.         long[] current = new long[n];
  116.         for (int i = 0; i < n; i++) {
  117.             current[i] = in.nextLong();
  118.             expected[i] = in.nextLong();
  119.         }
  120.         final int SQRT = 150;
  121.         Block[] blocks = new Block[(n + SQRT - 1) / SQRT];
  122.         for (int i = 0; i < blocks.length; i++) {
  123.             int from = i * SQRT;
  124.             int to = Math.min(n, (i + 1) * SQRT) - 1;
  125.             blocks[i] = new Block(current, expected, from, to);
  126.         }
  127.         for (int i = 0; i < q; i++) {
  128.             if (in.nextInt() == 1) {
  129.                 int left = in.nextInt() - 1, right = in.nextInt() - 1;
  130.                 int delta = in.nextInt();
  131.                 for (Block b : blocks) {
  132.                     b.add(left, right, delta);
  133.                 }
  134.             } else {
  135.                 int left = in.nextInt() - 1, right = in.nextInt() - 1;
  136.                 long min = Long.MAX_VALUE;
  137.                 for (Block b : blocks) {
  138.                     min = Math.min(b.getMin(left, right), min);
  139.                 }
  140.                 out.println(min);
  141.             }
  142.         }
  143.     }
  144.  
  145.     void run() {
  146.         try {
  147.             in = new FastScanner(new File("Sol.in"));
  148.             out = new PrintWriter(new File("Sol.out"));
  149.  
  150.             solve();
  151.  
  152.             out.close();
  153.         } catch (FileNotFoundException e) {
  154.             e.printStackTrace();
  155.         }
  156.     }
  157.  
  158.     void runIO() {
  159.  
  160.         in = new FastScanner(System.in);
  161.         out = new PrintWriter(System.out);
  162.  
  163.         solve();
  164.  
  165.         out.close();
  166.     }
  167.  
  168.     class FastScanner {
  169.         BufferedReader br;
  170.         StringTokenizer st;
  171.  
  172.         public FastScanner(File f) {
  173.             try {
  174.                 br = new BufferedReader(new FileReader(f));
  175.             } catch (FileNotFoundException e) {
  176.                 e.printStackTrace();
  177.             }
  178.         }
  179.  
  180.         public FastScanner(InputStream f) {
  181.             br = new BufferedReader(new InputStreamReader(f));
  182.         }
  183.  
  184.         String next() {
  185.             while (st == null || !st.hasMoreTokens()) {
  186.                 String s = null;
  187.                 try {
  188.                     s = br.readLine();
  189.                 } catch (IOException e) {
  190.                     e.printStackTrace();
  191.                 }
  192.                 if (s == null)
  193.                     return null;
  194.                 st = new StringTokenizer(s);
  195.             }
  196.             return st.nextToken();
  197.         }
  198.  
  199.         boolean hasMoreTokens() {
  200.             while (st == null || !st.hasMoreTokens()) {
  201.                 String s = null;
  202.                 try {
  203.                     s = br.readLine();
  204.                 } catch (IOException e) {
  205.                     e.printStackTrace();
  206.                 }
  207.                 if (s == null)
  208.                     return false;
  209.                 st = new StringTokenizer(s);
  210.             }
  211.             return true;
  212.         }
  213.  
  214.         int nextInt() {
  215.             return Integer.parseInt(next());
  216.         }
  217.  
  218.         long nextLong() {
  219.             return Long.parseLong(next());
  220.         }
  221.  
  222.         double nextDouble() {
  223.             return Double.parseDouble(next());
  224.         }
  225.     }
  226.  
  227.     public static void main(String[] args) {
  228.         new SolSqrt().runIO();
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment