题意:n个数 m个询问
每个询问[l, r]的和, 再把[l, r]之间所有的数变为平方(模为9223372034707292160LL)
很明显的线段树
看到这个模(LLONG_MAX为9223372036854775807) 很明显平方时会爆LL
很容易发现所有数平方模了几次之后值就不再改变了 而且这个“几次”相当小 因此直接暴力搞就好了
public static void main(String[] args) { Scanner in = new Scanner(System.in); BigInteger a=BigInteger.valueOf(2); // 这里看的是2的平方 Long b=9223372034707292160L; BigInteger mod=new BigInteger(b.toString()); for(int i=1;i<=40;i++) { a=a.multiply(a).mod(mod); System.out.println(i + ":" + a); // 平方i次之后的值 } }
1 import java.io.*; 2 import java.util.*; 3 import java.math.*; 4 import java.nio.charset.StandardCharsets; 5 6 public class Main 7 { 8 static BigInteger li=BigInteger.ZERO; 9 static Long b=9223372034707292160L; 10 static BigInteger mod=new BigInteger(b.toString()); 11 static BigInteger[] sum=new BigInteger[400005]; 12 static boolean[] num=new boolean[400005]; 13 static InputReader in = new InputReader(); 14 public static void pushup(int rt) 15 { 16 sum[rt]=(sum[rt*2].add(sum[rt*2+1])).mod(mod); 17 num[rt]=num[rt*2]&num[rt*2+1]; 18 } 19 public static void build(int l, int r, int rt) 20 { 21 if(l==r) 22 { 23 sum[rt]=in.nextBigInteger(); 24 num[rt]=false; 25 return ; 26 } 27 int m=(l+r)/2; 28 build(l, m, rt*2); 29 build(m+1, r, rt*2+1); 30 pushup(rt); 31 } 32 public static BigInteger query(int L, int R, int l, int r, int rt) 33 { 34 if(L<=l && r<=R) 35 return sum[rt]; 36 int m=(l+r)/2; 37 BigInteger ret=li; 38 if(L<=m) 39 ret=ret.add(query(L, R, l, m, rt*2)).mod(mod); 40 if(R>m) 41 ret=ret.add(query(L, R, m+1, r, rt*2+1)).mod(mod); 42 return ret.mod(mod); 43 } 44 public static void update(int L, int R, int l, int r, int rt) 45 { 46 if(num[rt]) 47 return ; 48 if(l==r) 49 { 50 BigInteger cur=(sum[rt].multiply(sum[rt])).mod(mod); 51 if(sum[rt].equals(cur)) 52 num[rt]=true; 53 sum[rt]=cur; 54 return ; 55 } 56 int m=(l+r)/2; 57 if(L<=m) 58 update(L, R, l, m, rt*2); 59 if(R>m) 60 update(L, R, m+1, r, rt*2+1); 61 pushup(rt); 62 } 63 public static void main(String[] args) 64 { 65 PrintWriter out = new PrintWriter(System.out); 66 int t, ca=1; 67 t=in.nextInt(); 68 while((t--)!=0) 69 { 70 int n=in.nextInt(); 71 int m=in.nextInt(); 72 build(1, n, 1); 73 System.out.println("Case #" + ca + ":"); 74 ca++; 75 BigInteger ans=li; 76 while((m--)!=0) 77 { 78 int l, r; 79 l=in.nextInt(); 80 r=in.nextInt(); 81 ans=ans.add(query(l, r, 1, n, 1)).mod(mod); 82 System.out.println(ans); 83 update(l, r, 1, n, 1); 84 } 85 } 86 } 87 } 88 89 class InputReader 90 { 91 BufferedReader buf; 92 StringTokenizer tok; 93 InputReader() 94 { 95 buf = new BufferedReader(new InputStreamReader(System.in)); 96 } 97 boolean hasNext() 98 { 99 while(tok == null || !tok.hasMoreElements()) 100 { 101 try 102 { 103 tok = new StringTokenizer(buf.readLine()); 104 } 105 catch(Exception e) 106 { 107 return false; 108 } 109 } 110 return true; 111 } 112 String next() 113 { 114 if(hasNext()) 115 return tok.nextToken(); 116 return null; 117 } 118 int nextInt() 119 { 120 return Integer.parseInt(next()); 121 } 122 long nextLong() 123 { 124 return Long.parseLong(next()); 125 } 126 double nextDouble() 127 { 128 return Double.parseDouble(next()); 129 } 130 BigInteger nextBigInteger() 131 { 132 return new BigInteger(next()); 133 } 134 BigDecimal nextBigDecimal() 135 { 136 return new BigDecimal(next()); 137 } 138 }