The Child and Sequence CodeForces - 438D(線段樹區間取模)

At the children’s day, the child came to Picks’s house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks.

Fortunately, Picks remembers how to repair the sequence. Initially he should create an integer array a[1], a[2], …, a[n]. Then he should perform a sequence of m operations. An operation can be one of the following:

Print operation l, r. Picks should write down the value of .
Modulo operation l, r, x. Picks should perform assignment a[i] = a[i] mod x for each i (l ≤ i ≤ r).
Set operation k, x. Picks should set the value of a[k] to x (in other words perform an assignment a[k] = x).
Can you help Picks to perform the whole sequence of operations?

Input
The first line of input contains two integer: n, m (1 ≤ n, m ≤ 105). The second line contains n integers, separated by space: a[1], a[2], …, a[n] (1 ≤ a[i] ≤ 109) — initial value of array elements.

Each of the next m lines begins with a number type .

If type = 1, there will be two integers more in the line: l, r (1 ≤ l ≤ r ≤ n), which correspond the operation 1.
If type = 2, there will be three integers more in the line: l, r, x (1 ≤ l ≤ r ≤ n; 1 ≤ x ≤ 109), which correspond the operation 2.
If type = 3, there will be two integers more in the line: k, x (1 ≤ k ≤ n; 1 ≤ x ≤ 109), which correspond the operation 3.
Output
For each operation 1, please print a line containing the answer. Notice that the answer may exceed the 32-bit integer.

Examples
Input
5 5
1 2 3 4 5
2 3 5 4
3 3 5
1 2 5
2 1 3 3
1 1 3
Output
8
5
Input
10 10
6 9 6 7 6 1 10 10 9 5
1 3 9
2 7 10 9
2 5 10 8
1 4 7
3 3 7
2 7 9 9
1 2 4
1 6 6
1 5 9
3 1 10
Output
49
15
23
1
9
Note
Consider the first testcase:

At first, a = {1, 2, 3, 4, 5}.
After operation 1, a = {1, 2, 3, 0, 1}.
After operation 2, a = {1, 2, 5, 0, 1}.
At operation 3, 2 + 5 + 0 + 1 = 8.
After operation 4, a = {1, 2, 2, 0, 1}.
At operation 5, 1 + 2 + 2 = 5.
Sponsor

題意:
操作爲區間和,區間取模,單點修改。

思路:
每次取模判斷一下模數是否大於最大值,如果大於的話取模沒意義,除此沒有什麼有效的優化方法了。但這樣還是要遍歷到點,複雜度玄學。

關鍵在於,每次取模完,數字至少減少一半。那麼我們只需要一直算下去,每個數字取模不會超過30次。

讓我想起了這題:區間開平方,也是直接暴力算,因爲一個數最多開logn次平方。
BZOJ3038. 上帝造題的七分鐘2(線段樹)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <iostream>

using namespace std;

typedef long long ll;
const int maxn = 1e5 + 7;
const int INF = 0x3f3f3f3f;

struct Tree {
    int l,r;
    ll mx,sum;
}t[maxn << 2];

void pushup(int i) {
    t[i].mx = max(t[i * 2].mx,t[i * 2 + 1].mx);
    t[i].sum = t[i * 2].sum + t[i * 2 + 1].sum;
}

void build(int i,int l,int r) {
    t[i].l = l;t[i].r = r;
    if(l == r) {
        scanf("%lld",&t[i].mx);
        t[i].sum = t[i].mx;
        return;
    }
    int m = (l + r) >> 1;
    build(i * 2,l,m);
    build(i * 2 + 1,m + 1,r);
    pushup(i);
}

ll query(int i,int x,int y) {
    if(x <= t[i].l && t[i].r <= y) {
        return t[i].sum;
    }
    int m = (t[i].l + t[i].r) >> 1;
    ll res = 0;
    if(x <= m) {
        res += query(i * 2,x,y);
    }
    if(y > m) {
        res += query(i * 2 + 1,x,y);
    }
    return res;
}


void update_mod(int i,int x,int y,int v) {
    if(t[i].mx < v) return;
    if(t[i].l == t[i].r) {
        t[i].sum %= v;
        t[i].mx %= v;
        return;
    }
    int m = (t[i].l + t[i].r) >> 1;
    if(x <= m) {
        update_mod(i * 2,x,y,v);
    }
    if(y > m) {
        update_mod(i * 2 + 1,x,y,v);
    }
    pushup(i);
}

void update(int i,int x,int v) {
    if(t[i].l == t[i].r) {
        t[i].sum = t[i].mx = v;
        return;
    }
    int m = (t[i].l + t[i].r) >> 1;
    if(x <= m) {
        update(i * 2,x,v);
    } else {
        update(i * 2 + 1,x,v);
    }
    pushup(i);
}

int main() {
    int n,m;scanf("%d%d",&n,&m);
    
    build(1,1,n);
    for(int i = 1;i <= m;i++) {
        int op;scanf("%d",&op);
        if(op == 1) {
            int x,y;scanf("%d%d",&x,&y);
            printf("%lld\n",query(1,x,y));
        }
        else if(op == 2) {
            int x,y,v;scanf("%d%d%d",&x,&y,&v);
            update_mod(1,x,y,v);
        }
        else {
            int x,v;scanf("%d%d",&x,&v);
            update(1,x,v);
        }
    }
    
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章