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. win10笔记本触摸板手势大全

  2. 002-IP地址及分类以及子网掩码

    一.概述 IP地址是一个4段2进制码组成的,每一段二进制码有8位,共32位二进制数.占用4个字节. IP地址是指互联网协议地址(Internet Protocol Address,又译为网际协议地址) ...

  3. PyNest——Part1:neurons and simple neural networks

    neurons and simple neural networks pynest – nest模拟器的界面 神经模拟工具(NEST:www.nest-initiative.org)专为仿真点神经元的 ...

  4. python的分布式爬虫框架

    scrapy + celery: Scrapy原生不支持js渲染,需要单独下载[scrapy-splash](GitHub - scrapy-plugins/scrapy-splash: Scrapy ...

  5. 剑指offer 面试33题

    面试33题:题:二叉搜索树的后序遍历序列 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 解题思路:递 ...

  6. C语言预处理命令的使用

    cppreference.com -> 预处理命令 -> 详细说明 预处理命令 #,## # 和 ## 操作符是和#define宏使用的. 使用# 使在#后的首个参数返回为一个带引号的字符 ...

  7. LVM逻辑磁盘管理

    一.简介 LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵 ...

  8. Linux CentOS7安装Mysql5.7

    一.下载mysql mkdir /home/install #创建install目录 在/home/install目录下下载mysql5.7 wget https://cdn.mysql.com//D ...

  9. php token 生成

    php token的生成   接口特点汇总: 1.因为是非开放性的,所以所有的接口都是封闭的,只对公司内部的产品有效: 2.因为是非开放性的,所以OAuth那套协议是行不通的,因为没有中间用户的授权过 ...

  10. HttpServlet---getLastModified与缓存

    在HttpServlet中重写service方法的代码如下: protected void service(HttpServletRequest req, HttpServletResponse re ...