HDU5239 Doom (segment tree, interval update, Interval square)

Source: Internet
Author: User
Tags modulus printf
problem Description

The END is comingggggg!

Mike has got stuck in a mystery machine. If He cannot solve this
Problem, he'll go to his doom.

This machine is consist of n cells, and a screens. The I-th cell
Contains a number AI (1≤i≤n). The screen also contains a number s,
Which is initially 0.

There is a button on each cell. When the i-th is pushed, Mike observes
That, the number on the screen would be changed to S+ai, where S is the
Original number. And the number on the I-th cell is changed to
A2i.

Mike observes that the number was stored in radix p, where
p=9223372034707292160. In and words, the operation is under modulo
P.

And now, Mike has got a list of operations. One operation is to push
Buttons between from l-th to R-th (both included), and record the
Number on the screen. He is tired of the stupid work, so he asks for
Your help. Can tell him, what is the numbers recorded. Input

The first line contains an integer T (t≤5), denoting the number of test
Cases.

For each test case, the first line contains the integers
N,m (1≤n,m≤105).

The next line contains n integers ai (0≤ai Output

For each test case, output ' case #t: ' to represent the t-th
Case. And then output the answer for each query operation, one answer
In a line.

For more details, you can take a look at the example. Sample Input

2
4 4
2 3 4 5
1 2 2
3
3 4
1 4
1 3
2
1 1
1 1
1 1
Sample Output
Case #1:
5
-
405 case
#2:
2
6
22
Ideas

First say test instructions, give the number of N, have M inquiry, define a and for sum=0, each time ask an interval [a, b] and +sum, in the output of this interval and after, the interval of each number into its own square, and then to 9223372034707292160 this number modulo

Because the modulus is too large, so in the time of the square to use the fast multiplication, with unsigned long long to do the input and output, we have a rule, a number has been squared, in the square to a certain number of times, the number of modulo modulus of the value will not change, So we use a flag array to record the value of this interval is also changed, when the flag=1 is not changed, it does not have to update the
Submit Select g++ code

#include <cstdio> #include <cstring> #include <cctype> #include <stdlib.h> #include <string&
Gt #include <set> #include <iostream> #include <stack> #include <cmath> #include <queue> # Include <vector> #include <algorithm> #define MEM (A, B) memset (A,b,sizeof (a)) #define INF 0x3f3f3f3f using Nam
Espace std;
typedef unsigned long long ll;
#define Lson l,m,rt<<1 #define Rson m+1,r,rt<<1|1 const LL n=1e5+20;
const LL Mod=9223372034707292160ull;
ll sum[n<<2];
int flag[n<<2];
    void Pushup (ll RT) {sum[rt]= (sum[rt<<1]+sum[rt<<1|1])%mod;
flag[rt]=flag[rt<<1]&flag[rt<<1|1];
        } void Build (LL l,ll R,ll RT) {if (l==r) {scanf ("%llu", &sum[rt]);
        flag[rt]=0;
    Return
    } ll m= (l+r) >>1;
    Build (Lson);
    Build (Rson);
Pushup (RT);
    } ll Q_mul (ll A,ll b) {ll ans=0; while (b) {if (b&1) {ANs= (ans+a)%mod;
        } a= (A+a)%mod;
    b>>=1;
} return ans;
    } void Update (LL a,ll B,LL l,ll r,ll RT) {if (Flag[rt]) return;
        if (l==r) {ll tmp=q_mul (Sum[rt],sum[rt]);
        if (Tmp==sum[rt]) flag[rt]=1;
        sum[rt]=tmp;
    Return
    } ll m= (l+r) >>1;
    if (a<=m) update (A,b,lson);
    if (b>m) update (A,b,rson);
Pushup (RT);
    } ll query (ll l,ll R,LL l,ll r,ll RT) {if (l<=l&&r<=r) return SUM[RT];
    ll m= (l+r) >>1;
    ll Ans=0;
    if (l<=m) ans= (Ans+query (L,r,lson))%mod;
    if (r>m) ans= (Ans+query (L,r,rson))%mod;
return ans;
    } int main () {ll t,n,m,q=1,x;
    scanf ("%llu", &t);
        while (t--) {scanf ("%llu%llu", &n,&m);
        Build (1,n,1);
        ll Sum=0,a,b;
        printf ("Case #%llu:\n", q++);
            while (m--) {scanf ("%llu%llu", &a,&b); Sum= (Sum+query (A, B, 1,n,1))%mod;
            printf ("%llu\n", sum);
        Update (a,b,1,n,1);
}} return 0;
 }

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.