fork download
  1. import java.lang.*;
  2. import java.math.*;
  3. import java.util.*;
  4. import java.io.*;
  5.  
  6. public class Main {
  7. ArrayList<Line> v=new ArrayList<>();
  8. int blocksize=500;
  9. int totalblocks;
  10. ConvexHullTrick cht[];
  11. ArrayList<Line> vec[];
  12.  
  13.  
  14. ArrayList<Line> tmpvec=new ArrayList<>();
  15. int n,q;
  16.  
  17. void solve() throws Exception {
  18. n=ni();
  19.  
  20. blocksize=(int)Math.sqrt(n)+1;
  21.  
  22. totalblocks=n/blocksize+1;
  23. if(n%blocksize==0) totalblocks--;
  24. // pw.println(blocksize+" "+totalblocks);
  25. cht=new ConvexHullTrick[totalblocks];
  26. vec=new ArrayList[totalblocks];
  27. for(int i=0;i<totalblocks;i++){
  28. cht[i]=new ConvexHullTrick(blocksize);
  29. vec[i]=new ArrayList<>();
  30. }
  31.  
  32.  
  33. for(int i=0;i<n;i++) {
  34. long a=ni(),b=ni();
  35. if(a>100000 || b>100000) throw new Exception("out of bound");
  36.  
  37. // cht[i/blocksize].add_line(a,b);
  38. vec[i/blocksize].add(new Line(a,b));
  39. }
  40. for(int i=0;i<totalblocks;i++){
  41. tmpvec.clear();
  42. for(Line l : vec[i]) tmpvec.add(new Line(-l.a,-l.b));
  43. Collections.sort(tmpvec);
  44. for(Line l : tmpvec) cht[i].add_line(l.a,l.b);
  45. }
  46. q=ni();
  47.  
  48. int cnt=0;
  49. for(int qu=0;qu<q;qu++) {
  50. int type =ni();
  51.  
  52. if (type == 1) {
  53. int i1 = ni(), i2 = ni();
  54. int st = 0, ed = 0;
  55. while (i1 > vec[st].size()) {
  56. i1 -= vec[st].size();
  57. st++;
  58. }
  59. while (i2 > vec[ed].size()) {
  60. i2 -= vec[ed].size();
  61. ed++;
  62. }
  63. if (st == ed) {
  64. Line l = vec[st].remove(i1 - 1);
  65. vec[st].add(i2 - 1, l);
  66.  
  67. } else {
  68. Line l = vec[st].remove(i1 - 1);
  69. vec[ed].add(i2, l);
  70.  
  71. cht[st]=new ConvexHullTrick(vec[st].size());
  72. cht[ed]=new ConvexHullTrick(vec[ed].size());
  73. tmpvec.clear();
  74. for(Line p : vec[st]) tmpvec.add(new Line(-p.a,-p.b));
  75. Collections.sort(tmpvec);
  76. for(Line p : tmpvec) cht[st].add_line(p.a,p.b);
  77. tmpvec.clear();
  78. for(Line p : vec[ed]) tmpvec.add(new Line(-p.a,-p.b));
  79. Collections.sort(tmpvec);
  80. for(Line p : tmpvec) cht[ed].add_line(p.a,p.b);
  81.  
  82.  
  83. }
  84. // for(Line l : vec[st]) pw.println(l.a+" "+l.b);
  85. cnt++;
  86. if (cnt == blocksize) {
  87. cnt = 0;
  88. rebuild();
  89. }
  90. } else {
  91. int l = ni(), r =ni() ;
  92. int x = ni();
  93. int st = 0, ed = 0;
  94. if(x>100000) throw new Exception("out of bound");
  95.  
  96. while (l > vec[st].size()) {
  97. l -= vec[st].size();
  98. st++;
  99. }
  100. while (r > vec[ed].size()) {
  101. r -= vec[ed].size();
  102. ed++;
  103. }
  104. // pw.println(st+" "+ed+" "+l+" "+r);
  105. long ans = Long.MIN_VALUE;
  106. // pw.println(st+" "+ed);
  107. if(st==ed){
  108. for(int i=l-1;i<r;i++) {
  109. Line p=vec[st].get(i);
  110. ans=Math.max(ans,p.get(x));
  111. }
  112. }else {
  113. for (int i = st + 1; i < ed; i++) ans = Math.max(ans, -cht[i].querry(x));
  114. for (int i = l - 1; i < vec[st].size(); i++) {
  115. Line p = vec[st].get(i);
  116. ans = Math.max(ans, p.get(x));
  117. }
  118. for (int i = 0; i < r; i++) {
  119. Line p = vec[ed].get(i);
  120. ans = Math.max(ans, p.get(x));
  121. }
  122. }
  123.  
  124. pw.println(ans);
  125. }
  126.  
  127. }
  128.  
  129.  
  130. }
  131. void rebuild(){
  132. v.clear();
  133. for(int i=0;i<totalblocks;i++) cht[i]=new ConvexHullTrick(blocksize);
  134. for(int i=0;i<totalblocks;i++){
  135. v.addAll(vec[i]);
  136. vec[i].clear();
  137.  
  138. }
  139. for(int i=0;i<v.size();i++){
  140. Line l=v.get(i);
  141. vec[i/blocksize].add(new Line(l.a,l.b));
  142.  
  143. }
  144. for(int i=0;i<totalblocks;i++){
  145. tmpvec.clear();
  146. for(Line l : vec[i]) tmpvec.add(new Line(-l.a,-l.b));
  147. Collections.sort(tmpvec);
  148. for(Line l : tmpvec) cht[i].add_line(l.a,l.b);
  149. }
  150.  
  151.  
  152.  
  153. }
  154. static class ConvexHullTrick {
  155. int size;
  156. Line lines[];
  157. Line hull[];
  158.  
  159. public ConvexHullTrick(int len){
  160. size=0;
  161. // lines=new Line[len];
  162. hull=new Line[len];
  163. }
  164. boolean isbad(int prev,int curr,int next){
  165. Line l1=hull[prev],l2=hull[curr],l3=hull[next];
  166. // if((l1.b-l3.b)>Long.MAX_VALUE/(l2.a-l1.a) || (l1.b-l2.b)>Long.MAX_VALUE/(l3.a-l1.a) ){
  167. // return ((l1.b-l3.b)*1.0)/(l3.a-l1.a) <=((l1.b-l2.b)*1.0)/(l2.a-l1.a);
  168. // }
  169. return (double)(l1.b-l3.b)*(l2.a-l1.a)<=(double)(l1.b-l2.b)*(l3.a-l1.a);
  170. }
  171.  
  172. void add_line(long a,long b){
  173. if(size!=0 && hull[size-1].a==a) return;
  174. hull[size++]=new Line(a,b);
  175. while(size>2 && isbad(size-3,size-2,size-1)){
  176. hull[size-2]=hull[size-1];
  177. size--;
  178. }
  179. }
  180. void add_lines(){
  181. Arrays.sort(lines, new Comparator<Line>() {
  182. @Override
  183. public int compare(Line t1, Line t2) {
  184. int t=Long.compare(t2.a,t1.a);
  185. if(t!=0) return t;
  186. t=Long.compare(t1.b,t2.b);
  187. return t;
  188. }
  189. });
  190. for(Line l : lines) add_line(l.a,l.b);
  191. }
  192. long querry(long x){
  193.  
  194. if(size==0) return Long.MAX_VALUE;
  195. int l=0,r=size-1;
  196. long ans=Long.MAX_VALUE;
  197.  
  198. while(l<=r){
  199. int mid=(l+r)/2;
  200. if(mid>0 && x*(hull[mid-1].a-hull[mid].a)<((hull[mid].b-hull[mid-1].b))){
  201. r=mid-1;
  202. }else if(mid+1<size && x*(hull[mid].a-hull[mid+1].a)>((hull[mid+1].b-hull[mid].b))){
  203. l=mid+1;
  204. }else {
  205. ans=Math.min(ans,hull[mid].get(x));
  206. break;
  207. }
  208. }
  209.  
  210.  
  211. return ans;
  212. }
  213. }
  214.  
  215. static class Line implements Comparable<Line>{
  216. long a;
  217. long b;
  218. public Line(long a,long b){
  219. this.a=a;
  220. this.b=b;
  221. }
  222. long get(long x){
  223. return a*x+b;
  224. }
  225. public int compareTo(Line c){
  226. int t=Long.compare(c.a,this.a);
  227. if(t!=0) return t;
  228. t=Long.compare(this.b,c.b);
  229. return t;
  230. }
  231. }
  232. static class Line2 {
  233. long a;
  234. long b;
  235. int id;
  236. public Line2(int id,long a,long b){
  237. this.a=a;
  238. this.b=b;
  239. this.id=id;
  240. }
  241. long get(long x){
  242. return a*x+b;
  243. }
  244. }
  245.  
  246. static int randInt(int min,int max){
  247. Random rand=new Random();
  248. int randInt=rand.nextInt((max-min)+1)+min;
  249. return randInt;
  250. }
  251.  
  252.  
  253.  
  254.  
  255. long M= (long)1e9+7;
  256. String INPUT = "";
  257. void run() throws Exception {
  258. is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
  259. pw = new PrintWriter(System.out);
  260. long s = System.currentTimeMillis();
  261. solve();
  262. pw.flush();
  263. if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
  264.  
  265. }
  266. public static void main(String[] args) throws Exception { new Main().run(); }
  267.  
  268. private byte[] inbuf = new byte[1024];
  269. public int lenbuf = 0, ptrbuf = 0;
  270.  
  271. private int readByte() {
  272. if(lenbuf == -1)throw new InputMismatchException();
  273. if(ptrbuf >= lenbuf){
  274. ptrbuf = 0;
  275. try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
  276. if(lenbuf <= 0)return -1;
  277. }
  278. return inbuf[ptrbuf++];
  279. }
  280.  
  281. private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
  282. private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
  283.  
  284. private double nd() { return Double.parseDouble(ns()); }
  285. private char nc() { return (char)skip(); }
  286.  
  287. private String ns() {
  288. int b = skip();
  289. StringBuilder sb = new StringBuilder();
  290. while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
  291. sb.appendCodePoint(b);
  292. b = readByte();
  293. }
  294. return sb.toString();
  295. }
  296.  
  297. private char[] ns(int n) {
  298. char[] buf = new char[n];
  299. int b = skip(), p = 0;
  300. while(p < n && !(isSpaceChar(b))){
  301. buf[p++] = (char)b;
  302. b = readByte();
  303. }
  304. return n == p ? buf : Arrays.copyOf(buf, p);
  305. }
  306.  
  307. private char[][] nm(int n, int m) {
  308. char[][] map = new char[n][];
  309. for(int i = 0;i < n;i++)map[i] = ns(m);
  310. return map;
  311. }
  312.  
  313. private int[] na(int n) {
  314. int[] a = new int[n];
  315. for(int i = 0;i < n;i++)a[i] = ni();
  316. return a;
  317. }
  318.  
  319. private int ni() {
  320. int num = 0, b;
  321. boolean minus = false;
  322. while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
  323. if(b == '-'){
  324. minus = true;
  325. b = readByte();
  326. }
  327.  
  328. while(true){
  329. if(b >= '0' && b <= '9'){
  330. num = num * 10 + (b - '0');
  331. }else{
  332. return minus ? -num : num;
  333. }
  334. b = readByte();
  335. }
  336. }
  337.  
  338. private long nl() {
  339. long num = 0;
  340. int b;
  341. boolean minus = false;
  342. while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
  343. if(b == '-'){
  344. minus = true;
  345. b = readByte();
  346. }
  347.  
  348. while(true){
  349. if(b >= '0' && b <= '9'){
  350. num = num * 10 + (b - '0');
  351. }else{
  352. return minus ? -num : num;
  353. }
  354. b = readByte();
  355. }
  356. }
  357. private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
  358. private void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); }
  359.  
  360. }
Success #stdin #stdout 0.04s 2184192KB
stdin
5 
2 2 
4 5
1 3
3 6
7 1
3
2 2 3 3
1 2 4
2 2 3 3
stdout
17
15