2014 Multi-University Training Contest 4

1006 

hdu4902

2014 Multi-University Training Contest 4

  1 #include <iostream>

  2 #include<stdio.h>

  3 #include<vector>

  4 #include<queue>

  5 #include<stack>

  6 #include<string.h>

  7 #include<algorithm>

  8 using namespace std;

  9 #define LL long long

 10 #define N 100010

 11 #define lcm(a,b) (a*b/gcd(a,b))

 12 int gcd(int a,int b)

 13 {

 14     if(b>a)swap(a,b);

 15     return b==0?a:gcd(b,a%b);

 16 }

 17 int s[N<<2],lz[N<<2],a[N],f[N<<2];

 18 void up(int w)

 19 {

 20     s[w] = max(s[w<<1],s[w<<1|1]);

 21     if(s[w<<1]==s[w<<1|1]&&f[w<<1]&&f[w<<1|1])

 22         f[w] = 1;

 23     else f[w] = 0;

 24 }

 25 void build(int l,int r,int w)

 26 {

 27     lz[w] = -1;

 28     f[w] = 0;

 29     if(l==r)

 30     {

 31         s[w] =  a[l];

 32         f[w] = 1;

 33         return ;

 34     }

 35     int m = (l+r)>>1;

 36     build(l,m,w<<1);

 37     build(m+1,r,w<<1|1);

 38     up(w);

 39 }

 40 void down(int w,int m)

 41 {

 42     if(lz[w]!=-1)

 43     {

 44         s[w<<1] = s[w<<1|1] = lz[w<<1] = lz[w<<1|1] = lz[w];

 45         lz[w] = -1;

 46     }

 47 }

 48 void update(int a,int b,int d,int l,int r,int w)

 49 {

 50     if(a<=l&&b>=r)

 51     {

 52         if(s[w]>d)

 53         {

 54             if(f[w]==1)

 55             {

 56                 s[w] = lz[w] = gcd(s[w],d);

 57                 return ;

 58             }

 59             if(l==r)

 60             {

 61                 s[w] = gcd(s[w],d);

 62                 return ;

 63             }

 64             down(w,r-l+1);

 65             int m = (l+r)>>1;

 66             update(a,b,d,l,m,w<<1);

 67             update(a,b,d,m+1,r,w<<1|1);

 68             up(w);

 69         }

 70         return ;

 71     }

 72     down(w,r-l+1);

 73     int m = (l+r)>>1;

 74     if(a<=m)

 75         update(a,b,d,l,m,w<<1);

 76     if(b>m)

 77         update(a,b,d,m+1,r,w<<1|1);

 78     up(w);

 79 }

 80 void change(int a,int b,int d,int l,int r,int w)

 81 {

 82     if(a<=l&&b>=r)

 83     {

 84         lz[w] = d;

 85         s[w] = d;

 86         f[w] = 1;

 87         return ;

 88     }

 89     down(w,r-l+1);

 90     int m = (l+r)>>1;

 91     if(a<=m)

 92         change(a,b,d,l,m,w<<1);

 93     if(b>m)

 94         change(a,b,d,m+1,r,w<<1|1);

 95     up(w);

 96 }

 97 int query(int p,int l,int r,int w)

 98 {

 99     if(l==r)

100     {

101         return s[w];

102     }

103     down(w,r-l+1);

104     int m = (l+r)>>1;

105     if(p<=m) return query(p,l,m,w<<1);

106     else return query(p,m+1,r,w<<1|1);

107 }

108 int main()

109 {

110     int n,i;

111     int t;

112     cin>>t;

113     while(t--)

114     {

115         scanf("%d",&n);

116         for(i = 1; i<= n ;i++)

117             scanf("%d",&a[i]);

118         build(1,n,1);

119         int m;

120         scanf("%d",&m);

121         while(m--)

122         {

123             int x,y,z,d;

124             scanf("%d%d%d%d",&d,&x,&y,&z);

125             if(d==1)

126             {

127                 change(x,y,z,1,n,1);

128             }

129             else if(d==2)

130                 update(x,y,z,1,n,1);

131         }

132         for(i = 1 ; i<=n; i++)

133             printf("%d ",query(i,1,n,1));

134         puts("");

135     }

136     return 0;

137 }
View Code

 

你可能感兴趣的:(test)