Cattle passenger - a small positive shells - range gcd (segment tree)

Links: https://ac.nowcoder.com/acm/contest/949/H
Source: Cattle-off network

Title Description

The hands of a small positive there are n shells, each shell has a color, and the initial i-th color shells coli  . There is now a small positive three operation:

. 1 LRX: to [l, r]  interval, where color values of all shell plus X  .

2 lr: Inquiry [l, r]  interval in all the adjacent shell difference (absolute value) of the color value of the maximum (if l = r  output 0).

3 lr: Inquiry [l, r]  the common denominator in all sections of the shells color values.

Enter a description:

The first line of the input two positive integers n-, m , respectively, the number of operations and the number of shells. 
The second row input n number coli , represents the initial color of each shell.
Third to m + 2 rows, each row is the first number opt , showing operation number. The following variables corresponding to the input operation number.

Output Description:

A total of m rows, for each challenge (operation 2 and operation 3) outputs a corresponding result.
Example 1

Entry

copy
5 6
2 2 3 3 3
1 2 3 3
2 2 4
3 3 5
1 1 4 2
3 2 3
2 3 5

Export

copy
3
3
1
3

analysis:

  This question segment tree, the tree are used in an array of friends

       Core knowledge gcd (a, b) == gcd (a, ba), so we maintain a differential array of modifications to the range of influence can be transformed into the greatest common divisor changes to the points

       a []: save the original data

       b []: Save differential data b [i] = a [i] -a [i-1]

       c []: When the interval [l, r] increases, c [l] + = x; c [r + 1] - = x; i.e. by maintaining a tree array [] array section changes; new_ai = ai + sum {c [x]} x ranges from 1 to i

  Finally, the greatest common divisor segment tree maintenance Jiuhaola

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,m,rt*2
 4 #define rson m+1,r,rt*2+1
 5 const int N=1e5+7;
 6 int a[N],b[N],c[N];
 7 int tg[4*N],tb[4*N];
 8 int n,m;
 9 inline int gcd (int x, int y) { return y ? gcd(y,x%y) : x; }
10 void pushup(int rt) {
11     tg[rt]=gcd(tg[rt*2], tg[rt*2+1]);
12     tb[rt]=max(abs(tb[rt*2]), abs(tb[rt*2+1]));
13 }
14 void build(int l, int r, int rt) {
15     if (l==r) {
16         tg[rt]=tb[rt]=b[l];
17         return ;
18     }
19     int m=(l+r)/2;
20     build(lson);
21     build(rson);
22     pushup(rt);
23     return ;
24 }
25 void update(int k, int val, int l, int r, int rt) {
26     if(l==r) {
27         tg[rt]+=val;
28         tb[rt]+=val;
29         return ;
30     }
31     int m=(l+r)/2;
32     if (k<=m) update(k,val,lson);
33     else      update(k,val,rson);
34     pushup(rt);
35     return ;
36 }
37 int q1 (int L, int R, int l, int r, int rt) {
38     if (r<L||l>R) return 0;
39     if (l>=L&&r<=R) return abs(tb[rt]);
40     int m=(l+r)/2;
41     return max(q1(L,R,lson),q1(L,R,rson));
42 }
43 int q2(int L, int R, int l, int r, int rt) {
44     if (r<L||l>R) return 0;
45     if (l>=L&&r<=R) return abs(tg[rt]);
46     int m=(l+r)/2;
47     int ans1=abs(q2(L,R,lson));
48     int ans2=abs(q2(L,R,rson));
49     return gcd(ans1,ans2);
50 }
51 void add(int k, int x) {
52     while (k<=n) {
53         c[k]+=x;
54         k+=k&(-k);
55     }
56 }
57 int get_sum(int k) {
58     int ans=0;
59     while(k) {
60         ans+=c[k];
61         k-=k&(-k);
62     }
63     return ans;
64 }
65 int main()
66 {
67     scanf("%d %d",&n,&m);
68     for (int i=1;i<=n;i++) {
69         scanf("%d",&a[i]);
70         b[i]=a[i]-a[i-1];
71     }
72     build(1,n,1);
73     while (m--) {
74         int op,l,r; scanf("%d %d %d",&op,&l,&r);
75         if (op==1) {
76             int x; scanf("%d",&x);
77             add(l,x); add(r+1,-x);
78             update(l,x,1,n,1);
79             if (r+1<=n) update(r+1,-x,1,n,1);
80         }
81         else if (op==2) {
82             if (l==r) printf("0\n");
83             else      printf("%d\n",q1(l+1,r,1,n,1));
84         }
85         else {
86             int now=a[l]+get_sum(l);
87             if (l==r) printf("%d\n",now);
88             else      printf("%d\n",gcd(now, q2(l+1,r,1,n,1)));
89         }
90     }
91     return 0;
92 }

 

Guess you like

Origin www.cnblogs.com/xidian-mao/p/11408023.html