[java线段树]2015上海邀请赛 D Doom

题意: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次之后的值

        }

    }

 

[java线段树]2015上海邀请赛 D Doom
  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 }
Java

 

C++11 有个神奇的东西叫做__int128   128位的整型,这题够了~

P.s. 这题很诡异的在HDOJ  java 过不了。。。MLE

[java线段树]2015上海邀请赛 D Doom
 1 #include <bits/stdc++.h>

 2 using namespace std;

 3 #define lson l, m, rt<<1

 4 #define rson m+1, r, rt<<1|1

 5 

 6 typedef __int128 LL;

 7 const LL mod=9223372034707292160;

 8 const int N=1e5+5;

 9 LL sum[N<<3];

10 bool num[N<<3];

11 template <class T>

12 bool read(T &ret){char c;int sgn;if(c=getchar(),c==EOF)return 0;while(c!='-' && (c<'0' || c>'9')) c = getchar();sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');ret *= sgn;return 1;}

13 template <class T>

14 void out(T x){if(x<0){putchar('-');x=-x;}if(x>9)out(x/10);putchar(x%10+'0');}

15 void pushup(int rt)

16 {

17     sum[rt]=(sum[rt<<1]+sum[rt<<1|1])%mod;

18     num[rt]=num[rt<<1]&num[rt<<1|1];

19 }

20 void build(int l, int r, int rt)

21 {

22     if(l==r)

23     {

24 //        scanf("%I64d", &sum[rt]);

25         read(sum[rt]);

26         num[rt]=0;

27         return ;

28     }

29     int m=(l+r)>>1;

30     build(lson);

31     build(rson);

32     pushup(rt);

33 }

34 LL query(int L, int R, int l, int r, int rt)

35 {

36     if(L<=l && r<=R)

37         return sum[rt];

38     int m=(l+r)>>1;

39     LL ret=0;

40     if(L<=m)

41         ret=(ret+query(L, R, lson))%mod;

42     if(R>m)

43         ret=(ret+query(L, R, rson))%mod;

44     return ret%mod;

45 }

46 void update(int L, int R, int l, int r, int rt)

47 {

48     if(num[rt])

49         return ;

50     if(l==r)

51     {

52         LL cur=(sum[rt]*sum[rt])%mod;

53         if(sum[rt]==cur)

54             num[rt]=1;

55         sum[rt]=cur;

56         return ;

57     }

58     int m=(l+r)>>1;

59     if(L<=m)

60         update(L, R, lson);

61     if(R>m)

62         update(L, R, rson);

63     pushup(rt);

64 }

65 int main()

66 {

67     int t, ca=1;

68     scanf("%d", &t);

69     while(t--)

70     {

71         int n, m;

72         scanf("%d%d", &n, &m);

73         build(1, n, 1);

74         printf("Case #%d:\n", ca++);

75         LL ans=0;

76         while(m--)

77         {

78             int l, r;

79             scanf("%d%d", &l, &r);

80             ans=(ans+query(l, r, 1, n, 1))%mod;

81 //            printf("%I64d\n", ans);

82             out(ans);

83             puts("");

84             update(l, r, 1, n, 1);

85         }

86     }

87     return 0;

88 }
G++

 

你可能感兴趣的:(java)