Doom

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1401    Accepted Submission(s): 368

Problem Description
THE END IS COMINGGGGGG!

Mike has got stuck on a mystery machine. If he cannot solve this problem, he will go to his doom.

This machine is consist of n cells, and a screen. The i-th cell contains a number ai(1≤i≤n). The screen also contains a number s, which is initially 0.

There is a button on each cell. When the i-th is pushed, Mike observes that, the number on the screen will be changed to s+ai, where s is the original number. and the number on the i-th cell will be changed to a2i.

Mike observes that the number is stored in radix p, where p=9223372034707292160. In other words  , the operation is under modulo p.

And now, Mike has got a list of operations. One operation is to push buttons between from l-th to r-th (both included), and record the number on the screen. He is tired of this stupid work, so he asks for your help. Can you tell him, what are the numbers recorded.

 
Input
The first line contains an integer T(T≤5), denoting the number of test cases.

For each test case, the first line contains two integers n,m(1≤n,m≤105).

The next line contains n integers ai(0≤ai<p), which means the initial values of the n cells.

The next m lines describe operations. In each line, there are two integers l,r(1≤l≤r≤n), representing the operation.

 
Output
For each test case, output ''Case #t:'', to represent this is the t-th case. And then output the answer for each query operation, one answer in a line.

For more details you can take a look at the example.

 
Sample Input
2
4 4
2 3 4 5
1 2
2 3
3 4
1 4
1 3
2
1 1
1 1
1 1
 
Sample Output
Case #1:
5
18
39
405
Case #2:
2
6
22
 
Source
 
 
给出n个数和一个初始值为0的答案。每次操作给出一个区间[l,r],把区间所有的数加到答案中,
之后把区间的每个数都平方。每次操作都需要输出答案 mod 9223372034707292160(2 ^ 63 - 2 ^ 31)
思路,
求区间和,容易想到线段树,但是这个题要把区间中的数平方取模,好像没法维护区间和。
但是关键是任意数的平方对题目中的mod取模,重复操作至多29次就会进入一个不变的数。
怎么知道最后会不变的...坑爹
 #include <bits/stdc++.h>
using namespace std; #define L(root) ((root) << 1)
#define R(root) (((root) << 1) | 1) #define LL long long
#define ULL unsigned long long
//const long long mod=((1ll<<63)-(1ll<<31));//这是个什么数 const ULL MOD = 9223372034707292160ULL; //乘法转加法
ULL squareMod(ULL a)
{
ULL b = a;
ULL sum = ;
while (b) {
if (b & ) {
sum = (sum + a) % MOD;
}
a = (a + a) % MOD;
b >>= ;
}
return sum;
} const int MAXN = 1e5 + ;
ULL numbers[MAXN]; struct Node {
int left, right;
ULL sum;
bool same;//
//int cnt;//
int mid()
{
return left + ((right - left) >> );
}
} tree[MAXN * ]; void pushUp(int root)
{
tree[root].sum = (tree[L(root)].sum + tree[R(root)].sum) % MOD;
tree[root].same = tree[L(root)].same && tree[R(root)].same;
//tree[root].cnt = min(tree[L(root)].cnt, tree[R(root)].cnt);
} void build(int root, int left, int right)
{
tree[root].left = left;
tree[root].right = right;
if (left == right) {
tree[root].sum = numbers[left];
tree[root].same = false;
//tree[root].cnt = 0;
return;
}
int mid = tree[root].mid();
build(L(root), left, mid);
build(R(root), mid + , right);
pushUp(root);
} ULL query(int root, int left, int right)
{
if (tree[root].left == left && tree[root].right == right) {
return tree[root].sum;
}
int mid = tree[root].mid();
if (right <= mid) {
return query(L(root), left, right);
} else if (mid < left) {
return query(R(root), left, right);
} else {
return (query(L(root), left, mid) + query(R(root), mid + , right)) % MOD;
}
} void update(int root, int left, int right, int add)
{
//重点,如区间内所有数字乘方取模已经不变,则无需更新
if (tree[root].same) {
//也可以用乘方次数,问题是怎么知道这个数字捏?
//if (tree[root].cnt > 30) {
return;
}
if (tree[root].left == tree[root].right) {
//直接乘会超限
//ULL tmp = tree[root].sum * tree[root].sum % MOD;
ULL tmp = squareMod(tree[root].sum);
if (tmp == tree[root].sum) {
tree[root].same = true;
return;
}
//++tree[root].cnt;
tree[root].sum = tmp;
return;
}
int mid = tree[root].mid();
if (right <= mid) {
update(L(root), left, right, add);
} else if (left > mid) {
update(R(root), left, right, add);
} else {
update(L(root), left, mid, add);
update(R(root), mid + , right, add);
}
pushUp(root);
} int main()
{
// printf("%lld\n", mod);
// printf("%lld\n", MOD);
// test();
int t;
int n, m;
int l, r;
int i;
ULL s;
int cas = ;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &m);
for (i = ; i <= n; ++i) {
scanf("%llu", &numbers[i]);
}
build(, , n);
printf("Case #%d:\n", ++cas);
s = ;
for (i = ; i < m; ++i) {
scanf("%d%d", &l, &r);
//printf("%d\n", query(1, l, r));
s = (s + query(, l, r)) % MOD;
printf("%llu\n", s);
update(, l, r, );
}
}
return ;
}

hdu 5239 Doom(线段树)的更多相关文章

  1. HDU 5239 Doom 线段树

    题意: 有\(n(1 \leq n \leq 10^5)\)个数,和\(m(1 \leq m \leq 10^5)\)操作,和一个计算\(s\),一切运算都在模\(MOD\)进行的. 操作\(l, \ ...

  2. hdu 4031 attack 线段树区间更新

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Subm ...

  3. hdu 4288 离线线段树+间隔求和

    Coder Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  4. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  5. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

  6. HDU 3308 LCIS (线段树区间合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[ ...

  7. HDU 2795 Billboard (线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题目大意:有一块h*w的矩形广告板,要往上面贴广告;   然后给n个1*wi的广告,要求把广告贴 ...

  8. hdu 5480 Conturbatio 线段树 单点更新,区间查询最小值

    Conturbatio Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=54 ...

  9. hdu 1828 Picture(线段树 || 普通hash标记)

    http://acm.hdu.edu.cn/showproblem.php?pid=1828 Picture Time Limit: 6000/2000 MS (Java/Others)    Mem ...

随机推荐

  1. 前端之 JS 实现全选、反选、取消选中

    需求 制作如下可选表格,实现“全选”.“反选”.“取消”功能 代码示例 <!DOCTYPE html> <html lang="zh-CN"> <he ...

  2. 理解java注解

    @是java注解,即annotation. 注解功能可以理解为插件,是代码级别的插件,在类的方法上写:@XXX,就是在代码上插入了一个插件. Java注解是附加在代码中的一些元信息,用于一些工具在编译 ...

  3. 002-JVM运行时数据区【内存模型】

    一.概述 JVM定义了不同运行时数据区,他们是用来执行应用程序的.某些区域随着JVM启动及销毁,另外一些区域的数据是线程性独立的,随着线程创建和销毁. 1.1.jvm自身物理结构 1.2.java内存 ...

  4. SpringMVC:学习笔记(6)——转换器和格式化

    转换器和格式化 说明 SpringMVC的数据绑定并非没有限制,有案例表明,在SpringMVC如何正确绑定数据方面是杂乱无章的,比如在处理日期映射到Date对象上. 为了能够让SpringMVC进行 ...

  5. python3 multiprocessing 模块

    多进程 Multiprocessing 模块 multiprocessing 模块官方说明文档 Process 类 Process 类用来描述一个进程对象.创建子进程的时候,只需要传入一个执行函数和函 ...

  6. javascript;先弹出提示框,再跳转到其他页面。

    context.Response.Write("<script>alert('删除成功!" + r.ToString() + "条');window.loca ...

  7. 【HackerRank】Sherlock and Array

    Watson gives an array A1,A2...AN to Sherlock. Then he asks him to find if there exists an element in ...

  8. Delphi中ComPort通信中的数据处理

    源: Delphi中ComPort通信中的数据处理

  9. awk遇到windows 的^M

    windows在编辑的文档,在linux中显示会在行尾出现一个^M window下编辑的文档:末尾带^M$ linux下编辑的文档:末尾带$ awk中如果存在^M,则会限制print的输出列数(只能输 ...

  10. 生信概念之global alignment VS local alignment