Detailed block

hzwer的9题
https://loj.ac/problem/6277
https://loj.ac/problem/6278
https://loj.ac/problem/6279
https://loj.ac/problem/6280
https://loj.ac/problem/6281
https://loj.ac/problem/6282
https://loj.ac/problem/6283
https://loj.ac/problem/6284
https://loj.ac/problem/6285

\ (\ Text {block is an elegant violence} \)

\ (\ Text {probably thinking something like this: Maintenance sqrt (n) time blocks within the block query is a query interval, if any part of the dissatisfaction with a block on a small enumeration constant} \)

\ (\ Text {personal feel than the tree line but the code is simple and easy thinking long (softly than tree line} \)

\ (\ Text {block and then sent a segment tree Generally O2 (constant because of the large segment tree} \)

\ (\ Text {preprocessing each block is a block into a digital complexity is O (N)} \)

\ (\ text {section and then modify the query is no more than what} \) \ (2 \ \ sqrt (n-) \)

\ (\ text {proved: that the size of one block} \) \ (\ sqrt (n-) \) \ (\ text {so much out of the left section and the right section is the worst case ($ \ sqrt (n) $) it is easy to prove} \)

\ (\ text {time complexity of the block is probably} \) \ (\ Theta (N + q * \ sqrt (N)) \)
(n-refers to the sequence length q refers query modification of the number of operations)
\ (\ text {the above is a basic block block thinking simply, easily realized than the segment tree} \)

\ (\ text {} block 1 \)
block modifying section 1 is a single point of the query

We use an array to maintain the block
if modifying the interval when the block contains the value you can add this block to be modified if it is more out of the direct violence changed

Then, when the query output block where the modified value and the value itself

// Isaunoya
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std ;
const int N = 50000 + 5 ;
const int Bl = 300 + 5 ;
struct node {
    int l , r ;
    int add ;
} ;
int n ;
int a[N] ;
node atag[Bl] ;
int bl[N] ; int unt ;
inline void change(int l , int r , int c) {
    for(register int i = l ; i <= min(bl[l] * unt , r) ; i ++)
        a[i] += c ;
    if(bl[l] != bl[r])
        for(register int i = (bl[r] - 1) * unt + 1 ; i <= r ; i ++)
            a[i] += c ;
    for(register int i = bl[l] + 1 ; i <= bl[r] - 1 ; i ++)
        atag[i].add += c ;
}
signed main() {
    ios::sync_with_stdio(false) ;
    cin >> n ;
    for(register int i = 1 ; i <= n ; i ++) cin >> a[i] ;
    unt = sqrt(n) ;
    for(register int i = 1 ; i <= n ; i ++) {
        bl[i] = (i - 1) / unt + 1 ;
    }
    for(register int i = 1 ; i <= n ; i ++) {
        int opt ;
        cin >> opt ;
        if(opt == 0) {
            int l , r , c ;
            cin >> l >> r >> c ;
            change(l , r , c) ;
        }
        else {
            int l , r , c ;
            cin >> l >> r >> c ;
            cout << a[r] + atag[bl[r]].add << endl ;
        }
    }
    return 0 ;
}

\ (\ Text {} block 2 \)

Two block sections is seeking to modify the query interval less than \ (c ^ 2 \) number of
these problems with the maintenance bit set (the set automatic sorting

#pragma GCC diagnostic error "-std=c++11"
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
inline int read() {
    register int x = 0;
    register int f = 1;
    register char c;
#define gc c = getchar()
    while (isspace(gc))
        ;
    c == '-' ? gc, f = -1 : 0;
    while (x = (x << 3) + (x << 1) + (c & 15), isdigit(gc))
        ;
    return x * f;
}
const int N = 100000 + 5;
const int Bl = 400 + 5;
int n;
int a[N];
struct node {
    int add;
};
node atag[Bl];
int unt;
int bl[N];
set<int> st[Bl];
inline void change(int l, int r, int c) {
    for (register int i = l ; i <= min(bl[l] * unt, r); i++) {
        st[bl[i]].erase(a[i]);
        a[i] += c;
        st[bl[i]].insert(a[i]);
    }
    if (bl[l] != bl[r]) {
        for (register int i = (bl[r] - 1) * unt + 1; i <= r; i++) {
            st[bl[i]].erase(a[i]);
            a[i] += c;
            st[bl[i]].insert(a[i]);
        }
    }
    for (register int i = bl[l] + 1; i <= bl[r] - 1; i++) atag[i].add += c;
}
inline int query(int l, int r, int c) {
    int ans = INT_MIN ;
    for (register int i = l; i <= min(bl[l] * unt, r); i++)
        if (a[i] + atag[bl[l]].add < c)
            ans = max(a[i] + atag[bl[l]].add, ans);
    if (bl[l] != bl[r]) {
        for (register int i = (bl[r] - 1) * unt + 1; i <= r; i++)
            if (a[i] + atag[bl[r]].add < c)
                ans = max(a[i] + atag[bl[r]].add, ans);
    }
    for (register int i = bl[l] + 1; i <= bl[r] - 1; i++) {
        int s = c - atag[i].add;
        auto find = st[i].lower_bound(s);
        if (find == st[i].begin()) continue ;
        find--;
        ans = max(ans, *find + atag[i].add);
    }
    return ans == INT_MIN ? -1 : ans ;
}
signed main() {
    n = read();
    unt = 500 + 5 ; 
    for (register int i = 1; i <= n; i++) a[i] = read();
    for (register int i = 1; i <= n; i++) bl[i] = (i - 1) / unt + 1;
    for (register int i = 1; i <= n; i++) {
        st[bl[i]].insert(a[i]);
    }
    for (register int i = 1; i <= n; i++) {
        int opt = read(), L = read(), R = read(), c = read();
        if (opt)
            printf("%d\n", query(L, R, c));
        else
            change(L, R, c);
    }
    return 0;
}

\ (\ Text {} block 3 \)

Block 3 within interrogation zone modification intervals of less than a value smaller than x x precursors i.e. the maximum number of

Also used to maintain a set
followed by \ (lower _ \ bound \) half
(part C ++ content 11

// Isaunoya
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC diagnostic error "-std=c++11"
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
inline int read() {
    register int x = 0;
    register int f = 1;
    register char c;
#define gc c = getchar()
    while (isspace(gc))
        ;
    c == '-' ? gc, f = -1 : 0;
    while (x = (x << 3) + (x << 1) + (c & 15), isdigit(gc))
        ;
    return x * f;
}
const int N = 100000 + 5;
const int Bl = 400 + 5;
int n;
int a[N];
struct node {
    int add;
};
node atag[Bl];
int unt;
int bl[N];
set<int> st[Bl];
inline void change(int l, int r, int c) {
    for (register int i = l; i <= min(bl[l] * unt, r); i++) {
        st[bl[i]].erase(a[i]);
        a[i] += c;
        st[bl[i]].insert(a[i]);
    }
    if (bl[l] != bl[r]) {
        for (register int i = (bl[r] - 1) * unt + 1; i <= r; i++) {
            st[bl[i]].erase(a[i]);
            a[i] += c;
            st[bl[i]].insert(a[i]);
        }
    }
    for (register int i = bl[l] + 1; i <= bl[r] - 1; i++) atag[i].add += c;
}
inline int query(int l, int r, int c) {
    int ans = -1;
    for (register int i = l; i <= min(bl[l] * unt, r); i++)
        if (a[i] + atag[bl[l]].add < c)
            ans = max(a[i] + atag[bl[l]].add, ans);
    if (bl[l] != bl[r]) {
        for (register int i = (bl[r] - 1) * unt + 1; i <= r; i++)
            if (a[i] + atag[bl[r]].add < c)
                ans = max(a[i] + atag[bl[l]].add, ans);
    }
    for (register int i = bl[l] + 1; i <= bl[r] - 1; i++) {
        int s = c - atag[i].add;
        auto find = st[i].lower_bound(s);
        if (find == st[i].begin()) continue ;
        find--;
        ans = max(ans, *find + atag[i].add);
    }
    return ans;
}
signed main() {
    n = read();
    unt = sqrt(n);
    for (register int i = 1; i <= n; i++) a[i] = read();
    for (register int i = 1; i <= n; i++) bl[i] = (i - 1) / unt + 1;
    for (register int i = 1; i <= n; i++) {
        st[bl[i]].insert(a[i]);
    }
    for (register int i = 1; i <= n; i++) {
        int opt = read(), L = read(), R = read(), c = read();
        if (opt)
            printf("%d\n", query(L, R, c));
        else
            change(L, R, c);
    }
    return 0;
}

\ ({\ Text {4}} block \)

4 is a block summation interval modification interval
and interval and% \ (. 1 + C \)

We consider the use of an array of maintenance intervals and then modify the maintenance of the entire block with an array

Maintenance intervals and refers to the violent modified part

// Isaunoya
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>

using namespace std ;
#define int long long
inline int read() { register int x = 0 ; register int f = 1 ; register char c ;
#define gc c = getchar()
    while(isspace(gc)) ;
    c == '-' ? gc , f = -1 : 0 ;
    while(x = (x << 1) + (x << 3) + (c ^ 48) , isdigit(gc)) ;
    return x * f ;
}

int n ;
const int N = 50000 + 5 ;
int a[N] ;
int bl[N] ;
int sum[N] ;
int atag[N] ;
int unt ;
inline void change(int l , int r , int c) {
    for(register int i = l ; i <= min(r , bl[l] * unt) ; i ++) {
        a[i] += c ;
        sum[bl[l]] += c ;
    }
    if(bl[l] != bl[r])
        for(register int i = (bl[r] - 1) * unt + 1 ; i <= r ; i ++) {
            a[i] += c ;
            sum[bl[r]] += c ;
        }
    for(register int i = bl[l] + 1 ; i <= bl[r] - 1 ; i ++) atag[i] += c ;
    return ;
}
inline int query(int l , int r , int c) { int ans = 0 ;
    for(register int i = l ; i <= min(r , bl[l] * unt) ; i ++) {
        ans += a[i] + atag[bl[l]] ;
        ans %= c ;
    }
    if(bl[l] != bl[r])
        for(register int i = (bl[r] - 1) * unt + 1 ; i <= r ; i ++) {
            ans += a[i] + atag[bl[r]] ;
            ans %= c ;
        }
    for(register int i = bl[l] + 1 ; i <= bl[r] - 1 ; i ++)
        ans = (ans + sum[i] + atag[i] * unt) % c ;
    return ans ;
}
signed main() {
    n = read() ; unt = sqrt(n) ;
    for(register int i = 1 ; i <= n ; i ++) a[i] = read() ;
    for(register int i = 1 ; i <= n ; i ++) bl[i] = (i - 1) / unt + 1 ;
    for(register int i = 1 ; i <= n ; i ++) {
        sum[bl[i]] += a[i] ;
    }
    for(register int i = 1 ;i <= n ; i ++) {
        int opt = read() ;
        if(opt == 0) {
            int l = read() , r = read() , c = read() ;
            change(l , r , c) ;
        }
        if(opt == 1) {
            int l = read() , r = read() , c = read() ;
            printf("%lld\n" , query(l , r , c + 1)) ;
        }
    }
    return 0 ;
}

Guess you like

Origin www.cnblogs.com/qf-breeze/p/11361618.html