Cogs 1695. Sleepwalking Wonderland (chunking)

Source: Internet
Author: User
Tags printf time limit
Sleepwalking in Wonderland
★☆ input file: xttmyxj.in output file: xttmyxj.out simple comparison
time limit: 5 sMemory Limit: MB
"Title description"
Asm.def still in the hard struggle with artificial intelligence, Snow sweet Princess still in Atlantisri amuse herself, she accidentally broke into the Mario World.
She felt very interesting, she reached the level of a row has n small block on the ground (can be regarded as a number), there are many on the ground, if the starting point of the snow Sweet is L, the end of R, jumping ability for jump, from left to right jumping
Ask L,r,jump for the Q group given by the Royal Princess of Snow, you need to calculate the number of silly coins he gets
For example, the following scenario
Gold Coin Series on the ground:
2 1 4 7 4 1 2 5 1
W[1] w[2] w[3] w[4] w[5] w[6] w[7] w[8] w[9]
If l=2,r=7,jump=3, then total silly currency number is w[2]+w[5]=5 (w[8] not count, because snow sweet jump not to get)
If l=3,r=4,jump=2, then total silly currency number is w[3]=4 (cannot jump, can only stay in situ)
"Input Format"
First behavior two integers n,q
Second row n number, indicating w[i]
Next Q line three numbers per line L,r,jump
"Output Format"
A total of Q lines, one answer per line ans
"Sample Input"
10 5
2 1 4 7 4 8 3 6 4 7
1 10 233333
4 7 666666
2 10 2
1 9 4
3 5 3
"Sample Output"
2
7
29
10
4
Prompted
For 30% of data, n<=2000
For 100% of data, n<=100000,q<=500000
/* This konjac konjac the first piece of the puzzle.
I didn't think of it at first.
It's probably about the jump value >sqrt (n) asking for violent handling.
A preprocessing is done for the jump value <=sqrt (n) query.
SUM[I][J] represents the prefix answer contribution that jumps I times, numbering (renumbering) for J points.
When querying, we guarantee that the number of points with the same jumping starting point is continuous and that the two endpoints that the query [L,r] contribute to must be jumping from the same jumping starting point.
So prefix and direct subtraction can be. 
Complexity O (q√n). */#include <iostream> #include <cstdio> #include <cmath> #define MAXN 100001 #define MAXM #define LL L
Ong long using namespace std;
int N,M,K,A[MAXN],S[MAXM][MAXN],G[MAXM][MAXN];
LL SUM[MAXM][MAXN];
    int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | |
    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}
    while (ch>= ' 0 ' &&ch<= ' 9 ') X=x*10+ch-48,ch=getchar ();
return x*f;
    } void Pre () {int pos; 
    for (int i=1;i<=k;i++)//Enumerates the length of jumps.
      {pos=0; 
        for (int j=1;j<=i;j++)//enumeration Jumping starting point (obviously there is a case of i).
        for (int k=j;k<=n;k+=i)//jump.
            {G[i][k]=++pos;
        S[I][POS]=A[K];
    } for (int j=1;j<=n;j++) sum[i][j]=sum[i][j-1]+ (LL) s[i][j];
   }} ll slove1 (int l,int r,int jump) {ll tot=0; for (int i=l;i<=r;i+=jump) tot+=a[i];
return tot;
    } LL slove2 (int l,int r,int jump) {int ll=l,rr=r-(r-l)%jump; if (!jump| |
    LL&GT;=RR) return A[LL];
    LL=G[JUMP][LL],RR=G[JUMP][RR];
return SUM[JUMP][RR]-SUM[JUMP][LL-1];
    } int main () {freopen ("xttmyxj.in", "R", stdin);
    Freopen ("Xttmyxj.out", "w", stdout);
    int x, y, Z; N=read (), M=read ();
    K=SQRT (n);
    for (int i=1;i<=n;i++) a[i]=read ();
    Pre ();
        while (m--) {x=read (), Y=read (), Z=read ();
        if (z>k) printf ("%lld\n", slove1 (x, Y, z));
    else printf ("%lld\n", Slove2 (x, Y, z));
} 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.