CodeForces 228D. Zigzag(线段树暴力)

原创 2015年07月09日 15:30:10

D. Zigzag
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The court wizard Zigzag wants to become a famous mathematician. For that, he needs his own theorem, like the Cauchy theorem, or his sum, like the Minkowski sum. But most of all he wants to have his sequence, like the Fibonacci sequence, and his function, like the Euler's totient function.

The Zigag's sequence with the zigzag factor z is an infinite sequence Siz (i ≥ 1; z ≥ 2), that is determined as follows:

  • Siz = 2, when ;
  • , when ;
  • , when .

Operation  means taking the remainder from dividing number x by number y. For example, the beginning of sequence Si3(zigzag factor 3) looks as follows: 1, 2, 3, 2, 1, 2, 3, 2, 1.

Let's assume that we are given an array a, consisting of n integers. Let's define element number i (1 ≤ i ≤ n) of the array as ai. The Zigzag function is function , where l, r, z satisfy the inequalities 1 ≤ l ≤ r ≤ nz ≥ 2.

To become better acquainted with the Zigzag sequence and the Zigzag function, the wizard offers you to implement the following operations on the given array a.

  1. The assignment operation. The operation parameters are (p, v). The operation denotes assigning value v to the p-th array element. After the operation is applied, the value of the array element ap equals v.
  2. The Zigzag operation. The operation parameters are (l, r, z). The operation denotes calculating the Zigzag function Z(l, r, z).

Explore the magical powers of zigzags, implement the described operations.

Input

The first line contains integer n (1 ≤ n ≤ 105) — The number of elements in array a. The second line contains n space-separated integers: a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array.

The third line contains integer m (1 ≤ m ≤ 105) — the number of operations. Next m lines contain the operations' descriptions. An operation's description starts with integer ti (1 ≤ ti ≤ 2) — the operation type.

  • If ti = 1 (assignment operation), then on the line follow two space-separated integers: pi, vi (1 ≤ pi ≤ n; 1 ≤ vi ≤ 109) — the parameters of the assigning operation.
  • If ti = 2 (Zigzag operation), then on the line follow three space-separated integers: li, ri, zi (1 ≤ li ≤ ri ≤ n; 2 ≤ zi ≤ 6) — the parameters of the Zigzag operation.

You should execute the operations in the order, in which they are given in the input.

Output

For each Zigzag operation print the calculated value of the Zigzag function on a single line. Print the values for Zigzag functions in the order, in which they are given in the input.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cincout streams or the %I64dspecifier.

Sample test(s)
input
5
2 3 1 5 5
4
2 2 3 2
2 1 5 3
1 3 5
2 1 5 3
output
5
26
38
Note

Explanation of the sample test:

  • Result of the first operation is Z(2, 3, 2) = 3·1 + 1·2 = 5.
  • Result of the second operation is Z(1, 5, 3) = 2·1 + 3·2 + 1·3 + 5·2 + 5·1 = 26.
  • After the third operation array a is equal to 2, 3, 5, 5, 5.
  • Result of the forth operation is Z(1, 5, 3) = 2·1 + 3·2 + 5·3 + 5·2 + 5·1 = 38.


大致思路:Z从2到6,按循环节分别维护一棵线段树,一共维护30棵线段树,查询复杂度logn的,修改的复杂度是30*logn

时限3s,毕竟codeforces可过


//137924k	1372ms	GNU G++ 4.9.2 2761B
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
using namespace std;
typedef long long ll;

#define root 1,n,1
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int N = 1e5+100;
ll sum[35][N<<2];
ll val[N],mul[35][N];
int n;

inline void pushup(int rt,ll *sum)
{
    sum[rt] = sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt,ll sum[],ll mul[])
{
    if(l == r)
    {
        sum[rt] = val[l]*mul[l];
        return ;
    }
    int m = (l+r)>>1;
    build(lson,sum,mul);
    build(rson,sum,mul);
    pushup(rt,sum);
}
void update(int pos,ll x,int l,int r,int rt,ll sum[],ll mul[])
{
    if(l == r)
    {
        sum[rt] = x*mul[l];
        return ;
    }
    int m = (l+r)>>1;
    if(pos <= m) update(pos,x,lson,sum,mul);
    else update(pos,x,rson,sum,mul);
    pushup(rt,sum);
}
ll query(int L,int R,int l,int r,int rt,ll sum[])
{
    if(L <= l && r <= R)
    {
        return sum[rt];
    }
    ll ans = 0;
    int m = (l+r)>>1;
    if(L <= m) ans += query(L,R,lson,sum);
    if(R > m) ans += query(L,R,rson,sum);
    return ans;
}

inline int getid(int l,int z)
{
    int buf = 0;
    REP(i,z-2) buf += 2*i;
    int k = 2*(z-1);
    l = (l-1)%k+1;
    if(l == 1) return l+buf;
    return k-l+2+buf;
}

int main()
{
//......................
    ll a[15];
    a[1] = 1,a[2] = 2;
    REP(i,2)REP(j,N-10) mul[i][j] = a[(j-1+i-1)%2+1];
    a[3] = 3,a[4] = 2;
    REP(i,4)REP(j,N-10) mul[2+i][j] = a[(j-1+i-1)%4+1];
    a[4] = 4,a[5] = 3,a[6] = 2;
    REP(i,6)REP(j,N-10) mul[6+i][j] = a[(j-1+i-1)%6+1];
    a[5] = 5,a[6] = 4,a[7] = 3,a[8] = 2;
    REP(i,8)REP(j,N-10) mul[12+i][j] = a[(j-1+i-1)%8+1];
    a[6] = 6,a[7] = 5,a[8] = 4,a[9] = 3,a[10] = 2;
    REP(i,10)REP(j,N-10) mul[20+i][j] = a[(j-1+i-1)%10+1];
//.......................

    cin>>n;
    REP(i,n) scanf("%I64d",&val[i]);
    REP(i,30) build(root,sum[i],mul[i]);
    int Q;
    cin>>Q;
    REP(i,Q)
    {
        int op;
        scanf("%d",&op);
        if(op == 1)
        {
            int pos;ll x;
            scanf("%d%I64d",&pos,&x);
            REP(i,30) update(pos,x,root,sum[i],mul[i]);
        }
        else
        {
            int l,r,z;
            scanf("%d%d%d",&l,&r,&z);
            int id = getid(l,z);
            printf("%I64d\n",query(l,r,root,sum[id]));
        }
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

codeforces 482B. Interesting Array【线段树区间更新】

题目:codeforces 482B. Interesting Array 题意:给你一个值n和m中操作,每种操作就是三个数 l ,r,val。就是区间l---r上的与的值为val,最后问你...
  • y990041769
  • y990041769
  • 2014年10月29日 19:34
  • 2044

Codeforces 650D. Zip-line (动态LIS) (可持久化线段树 或 离线+树状数组)

题意: 给定一个长度为n的数列,和m个询问,每个询问的格式是:将原数组的第a个数改成b之后,数组的最长上升子序列(LIS)的长度。 做法:可持久化线段树 令 h[ ] 为原数组,LIS_L[ i ...
  • u012891242
  • u012891242
  • 2016年03月11日 17:38
  • 1348

[可持久化线段树] codeforces 707D. Persistent Bookcase

D. Persistent Bookcase题意:一个n∗mn*m矩阵AA,维护4种操作: 1 i j:把第A[i][j]A[i][j]赋值为1 2 i j:把第A[i][j]A[i][j]赋值为0 ...
  • kg20006
  • kg20006
  • 2016年08月21日 15:22
  • 579

codeforces851C. Five Dimensional Points(暴力/思维)

题意: 给你几个五维空间上的点,问你有几个好点, 好点定义:任意其他两个点与该点的夹角不为锐角。 思路: 1.只要判断为坏点后马上break,暴力能过。。(我天,,我当时没敢写,看来以后遇事不...
  • exp1ore
  • exp1ore
  • 2017年09月05日 16:48
  • 230

CodeForces 343D Water Tree dfs序 + 线段树

题目:http://codeforces.com/problemset/problem/343/D 题意:给定一个树,树上有n个点,每个点是一个蓄水池,初始全为空。首先输入一个n,然后输入n - ...
  • discreeter
  • discreeter
  • 2016年05月24日 12:14
  • 840

Codeforces 724B 暴力模拟

题意: 你可以将每行中的两个元素交换一次,还有整列交换两列的机会一次。 思路: 先判断是否要交换两列,如果每行最多2个不在相应位置上的数,就直接YES 否则, 暴力枚举交换两列后,看每行是否...
  • naipp
  • naipp
  • 2016年10月09日 00:28
  • 396

CodeForces 13A【暴力】

题意: 给你的一个十进制数n,计算对于2~n-1进制下的每个位相加和与数n-2的比值。 思路: n是1000,所以直接暴力一发?#include #include #include #inclu...
  • KEYboarderQQ
  • KEYboarderQQ
  • 2016年08月19日 22:34
  • 346

Codeforces 691F. Couple Cover (暴力)

题目链接简单题意给出一个序列,和一些询问,每个询问包含一个整数q,问序列中乘积不小于q的数对有多少对思路(代码更容易看懂) 因为q最多是3e6,所以只要统计一下序列中哪些数出现过,出现过多少次,然后...
  • baidu_19306071
  • baidu_19306071
  • 2016年07月21日 11:05
  • 586

Codeforces 725E Too much money (暴力模拟)

思路: 首先明确一点,为什么按照从大到小依次取,会得不到指定值: 因为,当取了某个位置的某个值(因为相同的值可以有多个)以后,之后所有能取的加起来都不够达到指定值了,所以,放一个值肯定比放多个值加...
  • naipp
  • naipp
  • 2016年10月24日 21:30
  • 405

Codeforces-834D The Bakery 线段树优化DP

给n个数,让你分成k个子段,让每个子段中不同数字个数的和最大 dp[i][j]:前i个元素分成j段的最大和 考虑第j段,他的开头为k,那么dp[i][j]=max{dp[k][j-1]+sum[k+1...
  • qq_34927456
  • qq_34927456
  • 2017年08月14日 21:15
  • 114
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:CodeForces 228D. Zigzag(线段树暴力)
举报原因:
原因补充:

(最多只允许输入30个字)