hdu 5239 Doom(线段树)
Doom
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1401 Accepted Submission(s): 368
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.
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.
For more details you can take a look at the example.
4 4
2 3 4 5
1 2
2 3
3 4
1 4
1 3
2
1 1
1 1
1 1
5
18
39
405
Case #2:
2
6
22
#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(线段树)的更多相关文章
- HDU 5239 Doom 线段树
题意: 有\(n(1 \leq n \leq 10^5)\)个数,和\(m(1 \leq m \leq 10^5)\)操作,和一个计算\(s\),一切运算都在模\(MOD\)进行的. 操作\(l, \ ...
- hdu 4031 attack 线段树区间更新
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Subm ...
- hdu 4288 离线线段树+间隔求和
Coder Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- HDU 5877 dfs+ 线段树(或+树状树组)
1.HDU 5877 Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...
- HDU 3308 LCIS (线段树区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[ ...
- HDU 2795 Billboard (线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题目大意:有一块h*w的矩形广告板,要往上面贴广告; 然后给n个1*wi的广告,要求把广告贴 ...
- hdu 5480 Conturbatio 线段树 单点更新,区间查询最小值
Conturbatio Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=54 ...
- hdu 1828 Picture(线段树 || 普通hash标记)
http://acm.hdu.edu.cn/showproblem.php?pid=1828 Picture Time Limit: 6000/2000 MS (Java/Others) Mem ...
随机推荐
- Vue.js之组件传值
Vue.js之组件传值 属性传值可以从父组件到子组件,也可以从子组件到父组件. 这里讲一下从父组件到子组件的传值 还以上次的demo为例,demo里有APP.vue是父组件,Header.vue,Us ...
- nodejs koa2 获取get值和post值,提交表单
// 获取get值 router.get('/b', async(ctx, next) => { ctx.state = { title: 'Koa2', name: '小明-list' } l ...
- 高斯过程(GP)
随机过程基本概念: 随机过程是一个比随机变量更广泛的概念.在概率论中,通常研究一个或多个这样有限个数的随机变量,即使在大数定律和中心极限定理中考虑了无穷多个随机变量,但也要假设随机变量之间互相独立.而 ...
- linux shell 入门
本文是本人学习linux shell入门收集整理,不完全原创. 参考博文: http://www.cnblogs.com/suyang/archive/2008/05/18/1201990.html ...
- Python之函数2 嵌套,作用域和闭包(Day12)
一.函数对象 1.函数是第一类对象,即函数可以当做数据传递 1.1 可以被引用 1.2 可以当做参数传递 1.3 返回值可以是函数 1.4 可以当做容器类型的元素 二.函数的嵌套 1.函数嵌套的调用: ...
- Django——admin组件
Django提供了基于web的管理工具. Django自动管理工具是django.contrib的一部分.你可以在项目的settings.py中的INSTALLED_APPS看到它: # Applic ...
- PHP基本语法,类基本函数
C#中函数四要素返回类型,函数名,参数列表,函数体pulic void show()php函数定义1.最简单的定义function show(){echo "hello"}show ...
- PAT 天梯赛 L1-043. 阅览室 【STL】
题目链接 https://www.patest.cn/contests/gplt/L1-043 思路 将每一次 借出和归还 都用 MAP 标记 如果归还的时候 已经被标记过了 那么 ANS ++ 并且 ...
- 8.22 ps课堂练习
真是做得超烂!以前学的快忘光了!
- Linux下运行java项目
最近初步接触了linux,感觉很有新鲜感.之前在windows下干过的事情也便想到在linux环境下实现一下.正好手头在编java,就想既然java可以在windows的DOS操作下运行,是不是也可以 ...