题意: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次之后的值
}
}
 import java.io.*;
import java.util.*;
import java.math.*;
import java.nio.charset.StandardCharsets; public class Main
{
static BigInteger li=BigInteger.ZERO;
static Long b=9223372034707292160L;
static BigInteger mod=new BigInteger(b.toString());
static BigInteger[] sum=new BigInteger[400005];
static boolean[] num=new boolean[400005];
static InputReader in = new InputReader();
public static void pushup(int rt)
{
sum[rt]=(sum[rt*2].add(sum[rt*2+1])).mod(mod);
num[rt]=num[rt*2]&num[rt*2+1];
}
public static void build(int l, int r, int rt)
{
if(l==r)
{
sum[rt]=in.nextBigInteger();
num[rt]=false;
return ;
}
int m=(l+r)/2;
build(l, m, rt*2);
build(m+1, r, rt*2+1);
pushup(rt);
}
public static BigInteger query(int L, int R, int l, int r, int rt)
{
if(L<=l && r<=R)
return sum[rt];
int m=(l+r)/2;
BigInteger ret=li;
if(L<=m)
ret=ret.add(query(L, R, l, m, rt*2)).mod(mod);
if(R>m)
ret=ret.add(query(L, R, m+1, r, rt*2+1)).mod(mod);
return ret.mod(mod);
}
public static void update(int L, int R, int l, int r, int rt)
{
if(num[rt])
return ;
if(l==r)
{
BigInteger cur=(sum[rt].multiply(sum[rt])).mod(mod);
if(sum[rt].equals(cur))
num[rt]=true;
sum[rt]=cur;
return ;
}
int m=(l+r)/2;
if(L<=m)
update(L, R, l, m, rt*2);
if(R>m)
update(L, R, m+1, r, rt*2+1);
pushup(rt);
}
public static void main(String[] args)
{
PrintWriter out = new PrintWriter(System.out);
int t, ca=1;
t=in.nextInt();
while((t--)!=0)
{
int n=in.nextInt();
int m=in.nextInt();
build(1, n, 1);
System.out.println("Case #" + ca + ":");
ca++;
BigInteger ans=li;
while((m--)!=0)
{
int l, r;
l=in.nextInt();
r=in.nextInt();
ans=ans.add(query(l, r, 1, n, 1)).mod(mod);
System.out.println(ans);
update(l, r, 1, n, 1);
}
}
}
} class InputReader
{
BufferedReader buf;
StringTokenizer tok;
InputReader()
{
buf = new BufferedReader(new InputStreamReader(System.in));
}
boolean hasNext()
{
while(tok == null || !tok.hasMoreElements())
{
try
{
tok = new StringTokenizer(buf.readLine());
}
catch(Exception e)
{
return false;
}
}
return true;
}
String next()
{
if(hasNext())
return tok.nextToken();
return null;
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
BigInteger nextBigInteger()
{
return new BigInteger(next());
}
BigDecimal nextBigDecimal()
{
return new BigDecimal(next());
}
}

Java

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

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

 #include <bits/stdc++.h>
using namespace std;
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1 typedef __int128 LL;
const LL mod=;
const int N=1e5+;
LL sum[N<<];
bool num[N<<];
template <class T>
bool read(T &ret){char c;int sgn;if(c=getchar(),c==EOF)return ;while(c!='-' && (c<'' || c>'')) c = getchar();sgn = (c == '-') ? - : ;ret = (c == '-') ? : (c - '');while (c = getchar(), c >= ''&&c <= '') ret = ret * + (c - '');ret *= sgn;return ;}
template <class T>
void out(T x){if(x<){putchar('-');x=-x;}if(x>)out(x/);putchar(x%+'');}
void pushup(int rt)
{
sum[rt]=(sum[rt<<]+sum[rt<<|])%mod;
num[rt]=num[rt<<]&num[rt<<|];
}
void build(int l, int r, int rt)
{
if(l==r)
{
// scanf("%I64d", &sum[rt]);
read(sum[rt]);
num[rt]=;
return ;
}
int m=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
}
LL query(int L, int R, int l, int r, int rt)
{
if(L<=l && r<=R)
return sum[rt];
int m=(l+r)>>;
LL ret=;
if(L<=m)
ret=(ret+query(L, R, lson))%mod;
if(R>m)
ret=(ret+query(L, R, rson))%mod;
return ret%mod;
}
void update(int L, int R, int l, int r, int rt)
{
if(num[rt])
return ;
if(l==r)
{
LL cur=(sum[rt]*sum[rt])%mod;
if(sum[rt]==cur)
num[rt]=;
sum[rt]=cur;
return ;
}
int m=(l+r)>>;
if(L<=m)
update(L, R, lson);
if(R>m)
update(L, R, rson);
pushup(rt);
}
int main()
{
int t, ca=;
scanf("%d", &t);
while(t--)
{
int n, m;
scanf("%d%d", &n, &m);
build(, n, );
printf("Case #%d:\n", ca++);
LL ans=;
while(m--)
{
int l, r;
scanf("%d%d", &l, &r);
ans=(ans+query(l, r, , n, ))%mod;
// printf("%I64d\n", ans);
out(ans);
puts("");
update(l, r, , n, );
}
}
return ;
}

G++

[java线段树]2015上海邀请赛 D Doom的更多相关文章

  1. UESTC_秋实大哥与线段树 2015 UESTC Training for Data Structures<Problem M>

    M - 秋实大哥与线段树 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

  2. Java线段树

    线段树不是完全二叉树,是平衡二叉树 堆也是平衡二叉树 堆满二叉树: h层,一共有2^h-1个节点(大约是2^h) 最后一层(h-1层)有2^(h-1)个节点 最后一层的节点数大致等于前面所有层节点之和 ...

  3. java——线段树 SegmentTree

    应用: 区间染色 区间查询 线段树不是完全二叉树,线段树是平衡二叉树 使用数组来实现线段树:存储空间为4n 以下是使用数组实现的静态线段树: public class SegmentTree<E ...

  4. [java]2015上海邀请赛 B Base64

    题意: 给n和一个字符串(可以有空格) 求字符串编码n次后的字符串 编码方式:字符串的每个字符转化成ASCII码, ASCII码转化成8位2进制,    将二进制数分割成6位为一组的(不够的补0), ...

  5. [POI 2001+2014acm上海邀请赛]Gold Mine/Beam Cannon 线段树+扫描线

    Description  Byteman, one of the most deserving employee of The Goldmine of Byteland, is about to re ...

  6. hdu 5475 模拟计算器乘除 (2015上海网赛H题 线段树)

    给出有多少次操作 和MOD 初始值为1 操作1 y 表示乘上y操作2 y 表示除以第 y次操作乘的那个数 线段树的叶子结点i 表示 第i次操作乘的数 将1替换成y遇到操作2 就把第i个结点的值 替换成 ...

  7. ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)

    Problem Description One day, a useless calculator was being built by Kuros. Let's assume that number ...

  8. 2015上海网络赛 HDU 5475 An easy problem 线段树

    题意就不说了 思路:线段树,维护区间乘积.2操作就将要除的点更新为1. #include<iostream> #include<cstdio> #include<cstr ...

  9. hdu 5239 Doom(线段树)

    Doom Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Sub ...

随机推荐

  1. c# DateTime时间格式和JAVA时间戳格式相互转换

    /// java时间戳格式时间戳转为C#格式时间 public static DateTime GetTime(long timeStamp) { DateTime dtStart = TimeZon ...

  2. (转)实战Memcached缓存系统(3)Memcached配置参数初解

    一.基本参数 在我们第一次安装Memcached时,一般都是用过这个命令: memcached -m 512 -u root -d -l 127.0.0.1 -p 11211 我们先来解释这几个参数的 ...

  3. 添加线标注ILineElement

    private void AddLineElement(IPolyline polyline) { try { IPolyline pPolyline = polyline; IRgbColor pL ...

  4. c语言与c++基础知识

    1.后缀名: C++/C程序的头文件以.h为后缀,C程序的源文件以.c为后缀,C++程序的源文件通常以.cpp为后缀(有些书中介绍有一些系统以.cc或.cxx为后缀的源文件).在Linux系统下的gc ...

  5. VS2013编译WEBKIT

    0,安装VS2013:DXSDK_Jun10.exe:QuickTimeSDK.exe 1,WebKit-r174650.tar.bz2 以管理员解压(非管理员解压最后几下总是报错) 2,设置环境变量 ...

  6. 微软有完善的WP开发教程

    微软的Windows Phone 开发者中心 地址:http://dev.windowsphone.com/zh-cn/develop由于这里的教程非常完善,大家直要把开发者中心的内容看完就可以了,所 ...

  7. QMessageBox 弹出框上的按钮设置为中文

    Qt 默认的弹出框上的按钮式英文,虽然也知道是什么意思,但终究不如中文看着顺眼. QMessageBox box(QMessageBox::Warning,"标题","弹 ...

  8. 超棒的阿里巴巴矢量图标库——支持IE6

    Iconfont.cn 是由阿里巴巴UX部门推出的矢量图标管理网站,也是国内首家推广Webfont形式图标的平台.网站涵盖了1000多个常用图标并还在持续更新 中,Iconfont平台为用户提供在线图 ...

  9. Catalyst揭秘 Day6 Physical plan解析

    Catalyst揭秘 Day6 Physical plan解析 物理计划是Spark和Sparksql相对比而言的,因为SparkSql是在Spark core上的一个抽象,物理化就是变成RDD,是S ...

  10. closest()一个在评论里很有用的函数

    实例 本例演示如何通过 closest() 完成事件委托.当被最接近的列表元素或其子后代元素被点击时,会切换黄色背景: $( document ).bind("click", fu ...