Doom HDU - 5239(线段树+思维)

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
给出n个数和m个操作,每个操作给出l和r,求出l到r的和来后,就把l到r之间的数都平方。每次输出的结果都加上之前的结果。。
打表找规律,不断模上那个数之后将近三十次之后,就不会变了。这样就不会一直更新。设一个flag标记一下就行。注意要用unsigned long long,并且在平方的时候注意用快速乘。
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<string>
#define ll unsigned long long
using namespace std;

const int maxx=1e5+100;
const ll mod=9223372034707292160;
struct node{
	int l;
	int r;
	ll v;
	bool flag;
}p[maxx<<2];
int n,m;
ll ans;

inline ll qsj(ll x,ll y)
{
	ll ans1=0;
	while(y)
	{
		if(y&1) ans1=(ans1+x)%mod;
		x=(x+x)%mod;
		y>>=1;
	}
	return ans1%mod;
}
inline void pushup(int cur)
{
	p[cur].flag=p[cur<<1].flag&p[cur<<1|1].flag;
	p[cur].v=(p[cur<<1].v+p[cur<<1|1].v)%mod;
}
inline void build(int l,int r,int cur)
{
	p[cur].l=l;
	p[cur].r=r;
	p[cur].flag=0;
	if(l==r)
	{
		scanf("%lld",&p[cur].v);
		return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,cur<<1);
	build(mid+1,r,cur<<1|1);
	pushup(cur);
}
inline void update(int l,int r,int cur)
{
	if(p[cur].flag) return ;
	int L=p[cur].l;
	int R=p[cur].r;
	if(L==R)
	{
		ll s=p[cur].v;
		p[cur].v=qsj(p[cur].v,p[cur].v);
		if(s==p[cur].v) p[cur].flag=1;
		return ;
	}
	int mid=(L+R)>>1;
	if(r<=mid) update(l,r,cur<<1);
	else if(l>mid) update(l,r,cur<<1|1);
	else 
	{
		update(l,mid,cur<<1);
		update(mid+1,r,cur<<1|1);
	}
	pushup(cur);
}
inline ll query(int l,int r,int cur)
{
	int L=p[cur].l;
	int R=p[cur].r;
	if(l<=L&&R<=r)
	{
		return p[cur].v%mod;
	}
	int mid=(L+R)>>1;
	if(r<=mid) return query(l,r,cur<<1)%mod;
	else if(l>mid) return query(l,r,cur<<1|1)%mod;
	else return (query(l,mid,cur<<1)+query(mid+1,r,cur<<1|1))%mod;
	pushup(cur);
}

int main()
{
	int t,k=0;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		build(1,n,1);
		int x,y;
		ans=0;
		printf("Case #%d:\n",++k);
		while(m--)
		{
			scanf("%d%d",&x,&y);
			if(x>y) swap(x,y); 
			printf("%lld\n",(ans=(ans+query(x,y,1))%mod));
			update(x,y,1);
		}
	}
	return 0;
}

努力加油a啊,(o)/~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值