High-dimensional prefixes and

Source: Internet
Author: User

We often use prefixes and.

One-dimensional:

 for (int i=1; i<=n;i++) b[i]=b[i-1]+a[i];

Two-dimensional:

 for (int i=1; i<=n;i++)      for (int j=1; j<=m;j++)         b[i][j]=b[i-1][j]+b[i][j-1]-b[i- 1] [j1]+a[i][j];

What if it's three-dimensional?

 for(intI=1; i<=n;i++)     for(intj=1; j<=m;j++)         for(intk=1; k<=p;k++) B[i][j][k]=b[i-1][j][k]+b[i][j-1][k]+b[i][j][k-1]                            -b[i-1][j-1][k]-b[i-1][j][k-1]-b[i][j-1][j-1]                            +b[i-1][j-1][k-1]

is actually a tolerant.

However, as the dimension T becomes higher, the complexity of the repulsion is 2^t, and the total complexity O (n^t*2^t cannot withstand.

We also have one way:

One-dimensional:

 for (int i=1; i<=n;i++)     a[i]+=a[i-1];

Two-dimensional:

 for (int i=1; i<=n;i++)      for (int j=1; j<=m;j++)        a[i][j]+=a[i][j-1];  for (int i=1; i<=n;i++)      for (int j=1; j<=m;j++)        a[i][j]+=a[i-1][j];

This means that the first pass prefix and, each position a[i][j] is, I line before J's and.

The second time, all the lines in front and added.

Two times to achieve the goal. Looks like trouble.

What about the three dimensions?

 for(intI=1; i<=n;i++)     for(intj=1; j<=m;j++)         for(intp=1;p <=k;p++) A[i][j][k]+=a[i-1][j][k]; for(intI=1; i<=n;i++)     for(intj=1; j<=m;j++)         for(intp=1;p <=k;p++) A[i][j][k]+=a[i][j-1][k]; for(intI=1; i<=n;i++)     for(intj=1; j<=m;j++)         for(intp=1;p <=k;p++) A[i][j][k]+=a[i][j][k-1];

In fact, the two-dimensional understanding is the same. Once again, add the third dimension and the past.

However, this three-dimensional only 3 times, that is, for t-dimensional, in fact, as long as O (n^t*t) complexity is very low.

In fact, we actually solve the problem, often use is the situation of n=2.

Like what

Examples:

1. Section and (Cattle network Noip pre-match Camp-popularization Group (fourth))

Enter an array of length n A[i], subscript starting from 0 (0 to n-1)
ensure that N is a power of 2 for the entire number of times,
for each i (0 <= i < n)
all satisfies ((I & j) = = j) The sum of a[j]. n<=2^20that is, the subset of each I and. If n=2^6, if the binary representation of I: 10101 as a 5-dimensional coordinate,so, the subset of I is the high-dimensional prefix of this coordinate and. It can be found that the N of each dimension is 2,this is better handled. if it is General: W represents the highest dimension:
 for (int i=0; i<w;i++) {    for (int j=0;j< (1< <W) (j + +){        if(j& (1<<i)) f[j]+=f[j^ (1<<W)] ;     }}

Beside

#include <bits/stdc++.h>using namespaceStd;typedefLong Longll;Const intN= (1<< +); ll A[n];intN;intMain () {scanf ("%d", &n);intp=0;  for(intI=0; i<n;i++) scanf ("%lld",&A[i]);  for(intI=1; i<n;i<<=1) {p++;  for(intj=0; j<n;j++){            if((j& (1<<p-1))) a[j]+=a[(j^ (1<<p-1))]; }    } for(intI=0; i<n;i++) printf ("%lld\n", A[i]);return 0;}

complexity and high-dimensional prefixes are the same; O (2^t*t)

High-dimensional prefixes and

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.