Codechef Chef and church u

Source: Internet
Author: User

Codechef Chef and church u
Description There is an array of n numbers a and there are n functions. Each function returns the sum of [li, ri] There are two types of operations: 1 x y: Change the x element value of the array to y2 m n: Ask the sum of the [m, n] Function N, q ≤ 105 Solution we can consider dividing functions into blocks N ?? √ Block , Pre-process the number of times each function and each number in each function, and then use a tree array to obtain and modify the number of times the number of x appears in each function to update the answer, tree array single-point modification. You just need to Code it when asking.

#include 
  
   using namespace std;typedef long long LL;const int N = 100010;int n, q, a[N], L[N], R[N], cnt[1500][N], num[1500][N];LL c[N], sum[N];inline int read(int &t) {    int f = 1;char c;    while (c = getchar(), c < '0' || c > '9') if (c == '-') f = -1;    t = c - '0';    while (c = getchar(), c >= '0' && c <= '9') t = t * 10 + c - '0';    t *= f;}void add(int i, int x) {    for (;i <= n; i += i & -i)  c[i] += x;}void change(int i, int x) {    add(i, x - a[i]);    a[i] = x;}LL ask(int i) {    LL t = 0;    for (; i; i -= i & -i)  t += c[i];    return t;}int main() {    read(n);    int bl = (int)sqrt(n + 0.5);    int S = n / bl + ((n % bl) ? 1 : 0);    for (int i = 1; i <= n; ++i)    read(a[i]);    for (int i = 1; i <= n; ++i)    add(i, a[i]);    for (int i = 0; i < n; ++i) {        read(L[i]), read(R[i]);        ++cnt[i / bl][L[i]];        --cnt[i / bl][R[i] + 1];    }    for (int i = 0; i < S; ++i)         for (int j = 1; j <= n; ++j) {            num[i][j] = cnt[i][j] + num[i][j - 1];            sum[i] += 1ull * num[i][j] * a[j];        }    read(q);    while (q--) {        int op, l, r;        read(op), read(l), read(r);        if (op == 1) {            for (int i = 0; i < S; ++i) sum[i] += 1ull * num[i][l] * (r - a[l]);            change(l, r);        }        else {            --l, --r;            LL ans = 0;            int x = l / bl, y = r / bl;            if (x == y) for (int i = l; i <= r; ++i)    ans += ask(R[i]) - ask(L[i] - 1);            else{                x = (l % bl ? x + 1 : x), y = (r + 1) % bl ? y - 1 : y;                for (int i = x; i <= y; ++i)    ans += sum[i];                while (l % bl)  ans += ask(R[l]) - ask(L[l++] - 1);                while ((r + 1) % bl)    ans += ask(R[r]) - ask(L[r--] - 1);            }            printf("%llu\n", ans);        }    }    return 0;}
  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.