线段树和树状数组的全面配合与比较
2016-07-24 19:17
274 查看
线段树的考题,当然其实也可以用树状数组去解决的:
`One day, little Vasya found himself in a maze consisting of (n + 1) rooms, numbered from 1 to (n + 1). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the (n + 1)-th one.
The maze is organized as follows. Each room of the maze has two one-way portals. Let’s consider room number i(1 ≤ i ≤ n), someone can use the first portal to move from it to room number (i + 1), also someone can use the second portal to move from it to room number pi, where 1 ≤ pi ≤ i.
In order not to get lost, Vasya decided to act as follows.
Each time Vasya enters some room, he paints a cross on its ceiling. Initially, Vasya paints a cross at the ceiling of room 1.
Let’s assume that Vasya is in room i and has already painted a cross on its ceiling. Then, if the ceiling now contains an odd number of crosses, Vasya uses the second portal (it leads to room pi), otherwise Vasya uses the first portal.
Help Vasya determine the number of times he needs to use portals to get to room (n + 1) in the end.
Input
The first line contains integer n (1 ≤ n ≤ 103) — the number of rooms. The second line contains n integers pi (1 ≤ pi ≤ i). Each pi denotes the number of the room, that someone can reach, if he will use the second portal in the i-th room.
Output
Print a single number — the number of portal moves the boy needs to go out of the maze. As the number can be rather large, print it modulo 1000000007(109 + 7).
Sample Input
Input
2
1 2
Output
4
Input
4
1 1 2 3
Output
20
Input
5
1 1 1 1 1
Output
62“
这里我用下线段树去做下:
“`
`One day, little Vasya found himself in a maze consisting of (n + 1) rooms, numbered from 1 to (n + 1). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the (n + 1)-th one.
The maze is organized as follows. Each room of the maze has two one-way portals. Let’s consider room number i(1 ≤ i ≤ n), someone can use the first portal to move from it to room number (i + 1), also someone can use the second portal to move from it to room number pi, where 1 ≤ pi ≤ i.
In order not to get lost, Vasya decided to act as follows.
Each time Vasya enters some room, he paints a cross on its ceiling. Initially, Vasya paints a cross at the ceiling of room 1.
Let’s assume that Vasya is in room i and has already painted a cross on its ceiling. Then, if the ceiling now contains an odd number of crosses, Vasya uses the second portal (it leads to room pi), otherwise Vasya uses the first portal.
Help Vasya determine the number of times he needs to use portals to get to room (n + 1) in the end.
Input
The first line contains integer n (1 ≤ n ≤ 103) — the number of rooms. The second line contains n integers pi (1 ≤ pi ≤ i). Each pi denotes the number of the room, that someone can reach, if he will use the second portal in the i-th room.
Output
Print a single number — the number of portal moves the boy needs to go out of the maze. As the number can be rather large, print it modulo 1000000007(109 + 7).
Sample Input
Input
2
1 2
Output
4
Input
4
1 1 2 3
Output
20
Input
5
1 1 1 1 1
Output
62“
这里我用下线段树去做下:
#include<cstdio> #include<cstring> #include<string> #include<iostream> #include<sstream> #include<algorithm> #include<utility> #include<vector> #include<set> #include<map> #include<queue> #include<cmath> #include<iterator> #include<stack> using namespace std; #define e tree[id] #define lson tree[id*2] #define rson tree[id*2+1] typedef __int64 LL; const int INF=1e9+7; const double eps=1e-7; const int maxn=100005; bool f[maxn]; int A[maxn]; int judge(int n) { int x=4; while(n>0&&(x==4||x==7)) { x=n%10; n/=10; } if(x==4||x==7)return 1; else return 0; } struct Tree { int le,ri; int sum,d; }tree[4*maxn]; void pushup(int id){ e.sum=lson.sum+rson.sum; }//取其和 void pushdown(int id) //延迟更新 { if(e.d!=0&&e.le!=e.ri) //d不为0且不是叶子节点 { lson.sum+=(lson.ri-lson.le+1)*e.d; //左右儿子的和要加上他们自身的长度乘以d rson.sum+=(rson.ri-rson.le+1)*e.d; lson.d+=e.d; //推到下一层,因为子树还没更新 rson.d+=e.d; e.d=0; //更新完了得置为0,不然会重复更新 } } void Build_tree(int id,int le,int ri) //建树 { e.le=le,e.ri=ri,e.d=0; if(le==ri){ e.sum=f[A[le]];return; } int mid=(le+ri)/2; Build_tree(id*2,le,mid); Build_tree(id*2+1,mid+1,ri); pushup(id); } void Update(int id,int x,int y,int d) //更新 { int le=e.le,ri=e.ri; if(x<=le&&ri<=y){ e.sum+=f[e.sum+(ri-le+1)*d];e.d+=d; return; } pushdown(id); //推下去 int mid=(le+ri)/2; if(x<=mid) Update(id*2,x,y,d); //更新左边 if(y>mid) Update(id*2+1,x,y,d); //更新右边 pushup(id); //推上去 return; } //void update(int id,int x,int y) //更新 //{ // int le=e.le,ri=e.ri; // if(le==ri){ e.sum+=y;return;} // pushdown(id); //推下去 // int mid=(le+ri)/2; // if(x<=mid) update(id*2,x,y); //更新左边 // else update(id*2+1,x,y); //更新右边 // pushup(id); //推上去 // return; //} void update(int id,int k,int v) //更新 { int le=e.le,ri=e.ri; if(le==ri){ e.sum+=v; return; } pushdown(id); //推下去 int mid=(le+ri)/2; if(k<=mid) update(id*2,k,v); //更新左边 else update(id*2+1,k,v); //更新右边 pushup(id); //推上去 return; } int Query(int id,int x,int y) { int le=e.le,ri=e.ri; if(x<=le&&ri<=y) return e.sum; pushdown(id); //这个一定要,不然查询结果会出错 int mid=(le+ri)/2; int ret=0; if(x<=mid) ret+=Query(id*2,x,y); if(y>mid) ret+=Query(id*2+1,x,y); return ret; } bool jug(int a) { while(a) { if(a%10!=4&&a%10!=7) return 0; a/=< ec1f span class="hljs-number">10; } return 1; } int main() { int N,Q,x,y,d; char op[10]; memset(f,0,sizeof f); for(int i=0;i<=10001;i++) if(judge(i)) f[i]=1; while(~scanf("%d%d",&N,&Q)) { for(int i=1;i<=N;i++) scanf("%d",&A[i]); Build_tree(1,1,N); while(Q--) { scanf("%s",op); if(op[0]=='a') { scanf("%d%d%d",&x,&y,&d); for(int i=x;i<=y;i++) { int x1=f[A[i]]; A[i]+=d; int x2=f[A[i]]; if(x2==1&&x1==0)update(1,i,1); if(x1==1&&x2==0)update(1,i,-1) ; } } else { scanf("%d%d",&x,&y); printf("%d\n",Query(1,x,y)); } } } return 0; } 当然树状数组也可以写: ```#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#include <iostream>#define LL long long using namespace std; int a[100001],c[100002],kk[10002]={0}; int maxn; inline int lowbit(int a) { return a&(-a); } void add(int x,int num) { while(x<=maxn) { c[x]+=num; x+=lowbit(x); } } int getsum(int x) { int sum=0; while(x) { sum+=c[x]; x-=lowbit(x); } return sum; } bool jug(int a) { while(a) { if(a%10!=4&&a%10!=7) return 0; a/=10; } return 1; } int main() { int i,ans,n,m,b,a1,c1,x1,x2; char s[10]; for(i=1;i<=10001;i++) if(jug(i)) kk[i]=1; while(~scanf("%d %d",&n,&m)) { maxn=n; for(i=0; i<=n+1; i++) c[i]=0; for( i=1; i<=n; i++) { scanf("%d",&a[i]); if(kk[a[i]]==1) add(i,1); } while(m--) { scanf("%s",s); if(s[0]=='c') { scanf("%d %d",&a1,&b); printf("%d\n",getsum(b)-getsum(a1-1)); } else { scanf("%d %d %d",&a1,&b,&c1); for(i=a1; i<=b; i++) { x1=kk[a[i]]; a[i]+=c1; x2=kk[a[i]]; if(x1==1&&x2==0) add(i,-1); else if(x1==0&&x2==1) add(i,1); } } } } return 0; } <div class="se-preview-section-delimiter"></div>
“`
相关文章推荐
- HDU 1298 T9(字典树的经典应用)
- CentOS vi 配置文字高亮,缩进,以及生效方法
- 【算法学习】POJ3070——利用分治法来计算Fibonacci数列的值
- linux下搭建hadoop环境步骤
- 英文的典故(成语)
- 数据交换格式FlatBuffers介绍
- poj1083贪心
- 通过链表来输出各个结构体中的数据
- python核心编程作业--正则表达式和图形界面--电影搜索器(电影天堂)
- No.004 Median of Two Sorted Arrays
- 阅读计划
- 并查集小结
- Netty编解码框架分析
- 使用vnc,ssh
- Android Studio使用SVN代码管理工具遇到的问题及解决办法
- HDU 2594 Simpsons’ Hidden Talents (kmp)
- 服务器用map来接收数据
- centos6.x常用命令
- 通过迭代器来获得对应元素的下标╮(╯▽╰)╭ ~
- Python开发柜员机系统