线段树-两道题

来源:互联网 发布:楚楚街和淘宝哪个好 编辑:程序博客网 时间:2024/06/15 12:22

Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1669    Accepted Submission(s): 734


Problem Description
There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can’t refuse any request from the devil. Also, this devil is looking like a very cute Loli.

Let us continue our story, z*p(actually you) defeat the 'MengMengDa' party's leader, and the 'MengMengDa' party dissolved. z*p becomes the most famous guy among the princess's knight party.

One day, the people in the party find that z*p has died. As what he has done in the past, people just say 'Oh, what a nice boat' and don't care about why he died.

Since then, many people died but no one knows why and everyone is fine about that. Meanwhile, the devil sends her knight to challenge you with Algorithm contest.

There is a hard data structure problem in the contest:

There are n numbers a_1,a_2,...,a_n on a line, everytime you can change every number in a segment [l,r] into a number x(type 1), or change every number a_i in a segment [l,r] which is bigger than x to gcd(a_i,x) (type 2).

You should output the final sequence.
 

Input
The first line contains an integer T, denoting the number of the test cases.
For each test case, the first line contains a integers n.
The next line contains n integers a_1,a_2,...,a_n separated by a single space.
The next line contains an integer Q, denoting the number of the operations.
The next Q line contains 4 integers t,l,r,x. t denotes the operation type.

T<=2,n,Q<=100000
a_i,x >=0
a_i,x is in the range of int32(C++)
 

Output
For each test case, output a line with n integers separated by a single space representing the final sequence.
Please output a single more space after end of the sequence
 

Sample Input
11016807 282475249 1622650073 984943658 1144108930 470211272 101027544 1457850878 1458777923 2007237709 101 3 6 742430422 4 8 165317291 3 4 14748331692 1 8 11315709332 7 9 15057953352 3 7 1019292671 4 10 16243791492 2 8 21100106722 6 7 1560917451 2 5 937186357
 
Sample Output
16807 937186357 937186357 937186357 937186357 1 1 1624379149 1624379149 1624379149


一开始没加lazy操作导致TLE,后来pushdown里面更新有问题又wa了很久,最终还是解决了,

AC代码:

#include<iostream>using namespace std;#define lson 2*rt#define rson 2*rt+1typedef __int64 ll;#define MAX 100005ll rec[MAX];int cnt=0;struct node{int l,r;ll max,value;}a[MAX<<2];ll max(ll a,ll b){return a>b?a:b;}ll gcd(ll a,ll b)  {      if(!b) return a;      else return gcd(b,a%b);  }void build(int l,int r,int rt){a[rt].l=l;a[rt].r=r;a[rt].value=-1;if(l==r){a[rt].value = rec[l];a[rt].max = rec[l];}else{int mid = (l+r)>>1;build(l,mid,lson);build(mid+1,r,rson);a[rt].max = max(a[lson].max,a[rson].max);}}void pushdown(int rt){if(a[rt].value != -1){a[lson].value = a[rson].value = a[rt].value;a[lson].max = a[rson].max = a[rt].max;//下面两句不加的话,前面的1操作会覆盖后面的a[rt].max = -1;//开始忘了加这一句,导致waa[rt].value=-1;//}}void type1(ll x,int l,int r,int rt){if(l==a[rt].l && a[rt].r==r){//不延迟会TLEa[rt].value = x;a[rt].max = x;}else{pushdown(rt);int mid = (a[rt].l+a[rt].r)>>1;if(mid>=r)type1(x,l,r,lson);else if(mid<l)type1(x,l,r,rson);else{type1(x,l,mid,lson);type1(x,mid+1,r,rson);}a[rt].max = max(a[lson].max,a[rson].max);if(a[lson].value==a[rson].value&&a[lson].value!=-1)a[rt].value = a[lson].value;}}void type2(ll x,int l,int r,int rt){//如果区间内最大值小于x,直接返回if(a[rt].l<=l&&a[rt].r>=r&&a[rt].max<x)return;if(a[rt].l==l&&a[rt].r==r&&a[rt].value!=-1){//不要写成l==rif(a[rt].value>x) {a[rt].value =gcd(a[rt].value,x);}a[rt].max = a[rt].value;}else{pushdown(rt);int mid = (a[rt].l+a[rt].r)>>1;if(mid>=r)type2(x,l,r,lson);else if(mid<l)type2(x,l,r,rson);else{type2(x,l,mid,lson);type2(x,mid+1,r,rson);}a[rt].max = max(a[lson].max,a[rson].max);if(a[lson].value==a[rson].value && a[lson].value != -1)a[rt].value = a[lson].value;elsea[rt].value = -1;}}void print(int n,int l,int r,int rt){if(cnt>n) return;if(l==r){printf("%I64d ",a[rt].value);cnt++;}else{pushdown(rt);int mid = (l+r)>>1;print(n,l,mid,lson);print(n,mid+1,r,rson);}}int main(){int T;scanf("%d",&T);while(T--){int n;cnt=0;scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%I64d",&rec[i]);build(1,n,1);int m;scanf("%d",&m);while(m--){int t,l,r;ll x;scanf("%d%d%d%I64d",&t,&l,&r,&x);if(t==1)type1(x,l,r,1);if(t==2)type2(x,l,r,1);}print(n,1,n,1);printf("\n");}return 0;}

顺便把没加延迟操作TLE的代码附上,思路很清晰
#include<iostream>#include<algorithm>using namespace std;#define lson 2*rt#define rson 2*rt+1typedef __int64 ll;#define MAX 100005ll rec[MAX];struct node{int l,r;ll max,value;}a[MAX<<2];ll max(ll a,ll b){return a>b?a:b;}ll gcd(ll a,ll b)  {      if(!b) return a;      else return gcd(b,a%b);  }void build(int l,int r,int rt){a[rt].l=l;a[rt].r=r;a[rt].value=-1;if(l==r){a[rt].value = rec[l];a[rt].max = rec[l];}else{int mid = (l+r)>>1;build(l,mid,lson);build(mid+1,r,rson);a[rt].max = max(a[lson].max,a[rson].max);}}void type1(ll x,int l,int r,int rt){if(a[rt].l==a[rt].r){//不要写成l==ra[rt].value = x;a[rt].max = x;rec[a[rt].l] = x;//直接在记录数组中更改,方便输出最终结果}else{int mid = (a[rt].l+a[rt].r)>>1;if(mid>=r)type1(x,l,r,lson);else if(mid<l)type1(x,l,r,rson);else{type1(x,l,mid,lson);type1(x,mid+1,r,rson);}a[rt].max = max(a[lson].max,a[rson].max);if(a[lson].value==a[rson].value&&a[lson].value!=-1)a[rt].value = a[lson].value;}}void type2(ll x,int l,int r,int rt){//如果区间内最大值小于x,直接返回if(a[rt].l<=l&&a[rt].r>=r&&a[rt].max<x)return;if(a[rt].l == a[rt].r){//不要写成l==rif(a[rt].value>x) {a[rt].value =gcd(a[rt].value,x);rec[a[rt].l] = a[rt].value;}a[rt].max = a[rt].value;}else{int mid = (a[rt].l+a[rt].r)>>1;if(mid>=r)type2(x,l,r,lson);else if(mid<l)type2(x,l,r,rson);else{type2(x,l,mid,lson);type2(x,mid+1,r,rson);}a[rt].max = max(a[lson].max,a[rson].max);if(a[lson].value==a[rson].value && a[lson].value != -1)a[rt].value = a[lson].value;elsea[rt].value = -1;}}int main(){int T;scanf("%d",&T);while(T--){int n;scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%I64d",&rec[i]);build(1,n,1);int m;scanf("%d",&m);while(m--){int t,l,r;ll x;scanf("%d%d%d%I64d",&t,&l,&r,&x);if(t==1)type1(x,l,r,1);if(t==2)type2(x,l,r,1);}for(i=1;i<=n;i++)printf("%I64d ",rec[i]);printf("\n");}return 0;}




Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 890    Accepted Submission(s): 326


Problem Description
There are n types of cells in the lab, numbered from 1 to n. These cells are put in a queue, the i-th cell belongs to type i. Each time I can use mitogen to double the cells in the interval [l, r]. For instance, the original queue is {1 2 3 3 4 5}, after using a mitogen in the interval [2, 5] the queue will be {1 2 2 3 3 3 3 4 4 5}. After some operations this queue could become very long, and I can’t figure out maximum count of cells of same type. Could you help me?
 

Input
The first line contains a single integer t (1 <= t <= 20), the number of test cases.

For each case, the first line contains 2 integers (1 <= n,m<= 50000) indicating the number of cell types and the number of operations.

For the following m lines, each line represents an operation. There are only two kinds of operations: Q and D. And the format is:

“Q l r”, query the maximum number of cells of same type in the interval [l, r];
“D l r”, double the cells in the interval [l, r];

(0 <= r – l <= 10^8, 1 <= l, r <= the number of all the cells)
 

Output
For each case, output the case number as shown. Then for each query "Q l r", print the maximum number of cells of same type in the interval [l, r].

Take the sample output for more details.
 

Sample Input
15 5D 5 5Q 5 6D 2 3D 1 2Q 1 7
 

Sample Output
Case #1:23
 线段树的题,涉及到单点更新和区间更新。自己写的程序超时了,后来借鉴了别人的写法AC,而且两者时间复杂度差距还是蛮大的。两个代码都贴出来,比较一下。

AC代码:
#include<iostream>using namespace std;#define ll __int64 #define lson 2*rt#define rson 2*rt+1#define N 50005#define max(a,b) (a>b?a:b)  int t,n,m,cas=1;  ll x,y,ans,res;  char str[10]; struct node{int l,r,flag;ll sum,max;}a[N<<2];void pushdown(int rt){if(a[rt].flag/*&&a[rt].l != a[rt].r*/){a[lson].max<<=a[rt].flag;a[lson].sum<<=a[rt].flag;a[rson].max<<=a[rt].flag;a[rson].sum<<=a[rt].flag;a[lson].flag += a[rt].flag;a[rson].flag += a[rt].flag;}a[rt].flag = 0;}void pushup(int rt){if(a[rt].l != a[rt].r){a[rt].max = max(a[lson].max,a[rson].max);a[rt].sum = a[lson].sum + a[rson].sum;}}void build(int l,int r,int rt){a[rt].l = l;a[rt].r = r;a[rt].flag = 0;a[rt].max = 1;if(l == r){a[rt].sum = 1;}else{int mid = (l+r)>>1;build(l,mid,lson);build(mid+1,r,rson);pushup(rt);}}//找区间端点位置,并返回x在区间内的位置及区间内元素个数int find(int rt,ll x,ll &pos,ll &sum){if(a[rt].l == a[rt].r){pos = x;sum = a[rt].sum;return a[rt].l;}pushdown(rt);if(x<=a[lson].sum) return find(lson,x,pos,sum);else return find(rson,x-a[lson].sum,pos,sum);}/**端点更新*@param whr 位置*@param inc 增量*/void insert1(int rt,int whr,ll inc){if(a[rt].l==a[rt].r){a[rt].sum += inc;a[rt].max += inc;}else{int mid = (a[rt].l+a[rt].r)>>1;pushdown(rt);if(whr<=mid)insert1(lson,whr,inc);else insert1(rson,whr,inc);pushup(rt);}}/**区间更新*@param whr  */void insert2(int l,int r,int rt){if(l<=a[rt].l && a[rt].r<=r){pushdown(rt);a[rt].max*=2;a[rt].sum*=2;a[rt].flag++;}else{pushdown(rt);int mid = (a[rt].l+a[rt].r)>>1;if(l <= mid) insert2(l,r,lson);if(r > mid) insert2(l,r,rson);pushup(rt);}}void query(int l,int r,int rt){if(l<=a[rt].l&&a[rt].r<=r){res=max(res,a[rt].max);}else{pushdown(rt);int mid = (a[rt].l+a[rt].r)>>1;if(l<=mid) query(l,r,lson);if(r>mid) query(l,r,rson);}}int main(){scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);build(1,n,1);printf("Case #%d:\n",cas++); while(m--){scanf("%s%I64d%I64d",str,&x,&y);ll lpos,rpos,sum;int r = find(1,y,rpos,sum);int l = find(1,x,lpos,sum);if(str[0]=='D'){if(l==r){insert1(1,l,rpos-lpos+1); }else{insert1(1,r,rpos);  insert1(1,l,sum-lpos+1);if(r-l>1)insert2(l+1,r-1,1);}}else{ans=0;if(l==r)                  {                      printf("%I64d\n",rpos-lpos+1);continue;                }ans = max(ans,max(rpos,sum-lpos+1));if(r-l>1)  {res=0;  query(l+1,r-1,1);  ans=max(ans,res);  }printf("%I64d\n",ans);}}}return 0;}

下面是TLE的代码。

#include<stdio.h>#include<iostream>using namespace std;#define lson 2*rt#define rson 2*rt+1#define MAX 50005struct node{int l,r,flag;int max,sum;}a[MAX<<2];int max(int a,int b){return a>b?a:b;}void pushdown(int rt){if(a[rt].flag==1&&a[rt].l != a[rt].r){a[lson].max<<=a[rt].flag;a[lson].sum<<=a[rt].flag;a[rson].max<<=a[rt].flag;a[rson].sum<<=a[rt].flag;a[lson].flag += a[rt].flag;a[rson].flag += a[rt].flag;}a[rt].flag = 0;}void pushup(int rt){if(a[rt].l != a[rt].r){a[rt].max = max(a[lson].max,a[rson].max);a[rt].sum = a[lson].sum + a[rson].sum;}}void build(int l,int r,int rt){a[rt].l = l;a[rt].r = r;a[rt].max = 1;a[rt].flag = 0;if(l == r){a[rt].sum = 1;}else{int mid = (l+r)>>1;build(l,mid,lson);build(mid+1,r,rson);pushup(rt);}}void db(int l,int r,int rt){if(l>r) return;if(a[rt].l == a[rt].r){a[rt].max += (r-l+1);a[rt].sum += (r-l+1);}else{pushdown(rt);if(a[lson].sum >= r){if(l==1 && a[lson].sum==r){a[lson].flag++;a[lson].sum*=2;a[lson].max*=2;}else db(l,r,lson);}else{int lsum = a[lson].sum;int rsum = a[rson].sum;if(l<=lsum){if(l==1 && lsum==r){a[lson].sum*=2;a[lson].max*=2;a[lson].flag++;}else db(l,a[lson].sum,lson);if(rsum == (r-lsum)){a[rson].sum*=2;a[rson].max*=2;a[rson].flag++;}else db(1,r-lsum,rson);}else{db(l-lsum,r-lsum,rson);}}pushup(rt);}}int query(int l,int r,int rt){if(l>r) return -1;if(l==r) return 1;if(a[rt].l == a[rt].r){return (r-l+1);}else{int lmax=0,rmax=0;pushdown(rt);if(a[lson].sum >= r){if((r-l+1) == a[lson].sum)return a[lson].max;return query(l,r,lson);//左边最大值}else{int lsum = a[lson].sum;if(l<=lsum){lmax = query(l,a[lson].sum,lson);rmax = query(1,r-lsum,rson);return max(lmax,rmax);}else{return query(l-lsum,r-lsum,rson);}}}}int main(){int T;scanf("%d",&T);int cnt=0;while(T--){printf("Case #%d:\n",++cnt);int n,m;scanf("%d%d",&n,&m);build(1,n,1);char op;int l,r;for(int i=1;i<=m;i++){getchar();//notescanf("%c%d%d",&op,&l,&r);if(op=='D')db(l,r,1);else if(op == 'Q')printf("%d\n",query(l,r,1));}}return 0;}



0 0
原创粉丝点击