学习内容

来源:互联网 发布:独立数据集市 编辑:程序博客网 时间:2024/04/30 15:59
 

Nice boat(多校联合1006)

分类: 线段树 多校联合2014 14人阅读 评论(0) 收藏 举报

Nice boat

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


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 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.
 

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

这个破题挺坑的,RE了N次。简单线段树,挺好写的

AC代码:

[cpp] view plaincopy
  1. #include<iostream>  
  2. #include<stdio.h>  
  3. #include <algorithm>  
  4. using namespace std;  
  5. struct Node{  
  6.     int left,right;  
  7.     int s;             //这里要是long long的话就会RE   
  8. }a[400100];  
  9.   
  10. void built(int cur,int x,int y){  
  11.     a[cur].left=x;  
  12.     a[cur].right=y;  
  13.     a[cur].s=-1;  
  14.     if(x==y){  
  15.         scanf("%d",&a[cur].s);  
  16.         return;  
  17.     }  
  18.     if(x<y){  
  19.         int mid=(x+y)>>1;  
  20.         built(cur<<1,x,mid);  
  21.         built(cur<<1|1,mid+1,y);  
  22.         if(a[cur<<1].s==a[cur<<1|1].s)  
  23.             a[cur].s=a[cur<<1].s;  
  24.     }  
  25. }  
  26.   
  27. void insert1(int cur,int x,int y,int val){  
  28.     if(a[cur].left==x && a[cur].right==y){  
  29.         a[cur].s=val;  
  30.         return;  
  31.     }  
  32.     if(a[cur].s>=0){  
  33.         a[cur<<1].s=a[cur].s;  
  34.         a[cur<<1|1].s=a[cur].s;  
  35.         a[cur].s=-1;  
  36.     }  
  37.     int mid=(a[cur].left+a[cur].right)>>1;  
  38.     if(y<=mid)  
  39.         insert1(cur<<1,x,y,val);  
  40.     else if(x>mid)  
  41.         insert1(cur<<1|1,x,y,val);  
  42.     else{  
  43.         insert1(cur<<1,x,mid,val);  
  44.         insert1(cur<<1|1,mid+1,y,val);  
  45.     }  
  46. }  
  47. void insert2(int cur,int x,int y,int val){  
  48.     if(a[cur].left==x && a[cur].right==y && a[cur].s>=0){  
  49.         if(a[cur].s>val){  
  50.             a[cur].s=__gcd(a[cur].s,val);  
  51.         }  
  52.         return ;  
  53.     }  
  54.     if(a[cur].s>=0){  
  55.         a[cur<<1].s=a[cur<<1|1].s=a[cur].s;  
  56.         a[cur].s=-1;  
  57.     }  
  58.     int mid=(a[cur].left+a[cur].right)>>1;  
  59.     if(y<=mid)  
  60.         insert2(cur<<1,x,y,val);  
  61.     else if(x>mid)  
  62.         insert2(cur<<1|1,x,y,val);  
  63.     else{  
  64.         insert2(cur<<1,x,mid,val);  
  65.         insert2(cur<<1|1,mid+1,y,val);  
  66.     }  
  67. }  
  68.   
  69. void output(int cur){  
  70.     if(a[cur].s>=0){  
  71.         for(int i=a[cur].left;i<=a[cur].right;i++)  
  72.             printf("%d ",a[cur].s);  
  73.         return ;  
  74.     }  
  75.     output(cur<<1);  
  76.     output(cur<<1|1);  
  77. }  
  78.   
  79. int main(){  
  80.     int Q,T; scanf("%d",&T);  
  81.     while(T--){  
  82.         int n; scanf("%d",&n);  
  83.         built(1,1,n);  
  84.         scanf("%d",&Q);  
  85.         while(Q--){  
  86.             int t,l,r;  
  87.             int x;              //这里要是long long的话就会RE   
  88.             scanf("%d%d%d%d",&t,&l,&r,&x);  
  89.             if(t==1){  
  90.                 insert1(1,l,r,x);  
  91.             }  
  92.             else{  
  93.                 insert2(1,l,r,x);  
  94.             }  
  95.         }  
  96.         output(1);  
  97.         printf("\n");  
  98.     }  
  99.     return 0;  
  100. }  
0 0
原创粉丝点击