@(XSY)[分塊]

Hint: 題目原文是英文的, 寫得很難看, 因此翻譯為中文.

Input Format

First Line is the size of the array i.e. \(N\)

Next Line contains N space separated numbers \(A_i\) denoting the array

Next N line follows denoting \(Li\) and \(Ri\) for each functions.

Next Line contains an integer \(Q\) , number of queries to follow.

Next \(Q\) line follows , each line containing a query of Type 1 or Type 2.

1 x y : denotes a type 1 query,where x and y are integers

2 m n : denotes a type 2 query where m and n are integers

Output Format

For each query of type 2 , output as asked above.

Constraints

\(1 ≤ N ≤ 10^5\)

\(1 ≤ A i ≤ 10^9\)

\(1 ≤ L i ≤ N\)

$L i ≤ R i ≤ N $

\(1 ≤ Q ≤ 10^5\)

$1 ≤ x ≤ N $

\(1 ≤ y ≤ 10^9\)

$1 ≤ m ≤ N \(
\)m ≤ n ≤ N$

Subtask

Subtask \(1\): \(N ≤ 1000 , Q ≤ 1000\) , \(10\) points

Subtask \(2\): \(R-L ≤ 10\) , all x will be distinct ,\(10\) points

Subtask \(3\): Refer to constraints above , \(80\) points

Sample Input

5
1 2 3 4 5
1 3
2 5
4 5
3 5
1 2
4
2 1 4
1 3 7
2 1 4
2 3 5

Sample Output

41
53
28

Explanation

Functions values initially :

$F[1] = 1+ 2 + 3 = 6 \(
\)F[2] = 2 + 3 + 4 + 5 = 14 \(
\)F[3] = 4+5 = 9 \(
\)F[4] = 3+4+5 = 12 \(
\)F[5] = 1+2 = 3 $

Query \(1\): $F[1] + F[2] + F[3] + F[4] = 41 \(
After Update , the Functions are :
\)F[1] = 10 , F[2] = 18 , F[3] = 9 , F[4] = 16 , F[5] = 3 $

Query \(3\): $F[1] + F[2] + F[3] + F[4] = 53 $

Query \(4\): \(F[3]+F[4]+F[5] = 28\)

Solution

对\(a\)数组建立树状数组维护前缀和;

对函数进行分块处理. 维护两个数组, 其中\(sum[i]\)表示第\(i\)个块中的函数值的总和; \(cnt[i][j]\)表示第\(i\)个块中\(a[j]\)被累加的次数. \(cnt\)数组在预处理时可以通过累加前缀和的方法, \(O \left( n * sqrt(n) \right)\)完成. 而对于每次修改\(a[i]\)的值, 也可以在\(O \left(sqrt(n) * log(n) \right)\)的时间复杂度内完成维护.

#include<cstdio>
#include<cctype>
#include<cstring>
#include<cmath>
using namespace std; inline long long read()
{
long long x = 0, flag = 1;
char c;
while(! isdigit(c = getchar()))
if(c == '-')
flag *= - 1;
while(isdigit(c))
x = x * 10 + c - '0', c = getchar();
return x * flag;
} void println(long long x)
{
if(x < 0)
putchar('-'), x *= - 1;
if(x == 0)
putchar('0');
long long ans[1 << 5], top = 0;
while(x)
ans[top ++] = x % 10, x /= 10;
for(; top; top --)
putchar(ans[top - 1] + '0');
putchar('\n');
} const long long N = 1 << 17; long long n; long long a[N];
long long L[N], R[N];
long long T[N]; inline void modify(long long u, long long x)
{
for(; u <= n; u += u & - u)
T[u] += (long long)x;
} long long unit, num; long long cnt[1 << 9][N];
long long sum[1 << 9]; void update(long long x, long long y)
{
for(long long i = 0; i < num; i ++)
sum[i] += (long long)cnt[i][x] * (y - a[x]); modify(x, y - a[x]);
a[x] = y;
} inline long long query(long long u)
{
long long ret = 0; for(; u; u -= u & - u)
ret += T[u]; return ret;
} long long ask(long long _L, long long _R)
{
long long lBlock = _L / unit, rBlock = _R / unit;
long long ret = 0; if(lBlock == rBlock)
for(long long i = _L; i <= _R; i ++)
ret += query(R[i]) - query(L[i] - 1);
else
{
for(long long i = lBlock + 1; i < rBlock; i ++)
ret += sum[i]; for(long long i = _L; i < (lBlock + 1) * unit; i ++)
ret += query(R[i]) - query(L[i] - 1); for(long long i = unit * rBlock; i <= _R; i ++)
ret += query(R[i]) - query(L[i] - 1);
} return ret;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("chefAndChurus.in", "r", stdin);
freopen("chefAndChurus.out", "w", stdout);
#endif n = read();
memset(T, 0, sizeof(T)); for(long long i = 1; i <= n; i ++)
modify(i, a[i] = read()); for(long long i = 0; i < n; i ++)
L[i] = read(), R[i] = read(); unit = (long long)sqrt(n);
long long cur = - 1; memset(cnt, 0, sizeof(cnt)); for(long long i = 0; i < n; i ++)
{
if(i % unit == 0)
cur ++; cnt[cur][L[i]] ++, cnt[cur][R[i] + 1] --;
} num = cur + 1; memset(sum, 0, sizeof(sum)); for(long long i = 0; i < num; i ++)
for(long long j = 1; j <= n; j ++)
{
cnt[i][j] += cnt[i][j - 1];
sum[i] += (long long)cnt[i][j] * a[j];
} long long m = read(); for(long long i = 0; i < m; i ++)
{
long long opt = read(), x = read(), y = read(); if(opt == 1)
update(x, y);
else
println(ask(x - 1, y - 1));
}
}

CODECHEF Nov. Challenge 2014 Chef & Churu的更多相关文章

  1. 【分块+树状数组】codechef November Challenge 2014 .Chef and Churu

    https://www.codechef.com/problems/FNCS [题意] [思路] 把n个函数分成√n块,预处理出每块中各个点(n个)被块中函数(√n个)覆盖的次数 查询时求前缀和,对于 ...

  2. Codechef December Challenge 2014 Chef and Apple Trees 水题

    Chef and Apple Trees Chef loves to prepare delicious dishes. This time, Chef has decided to prepare ...

  3. CodeChef November Challenge 2014

    重点回忆下我觉得比较有意义的题目吧.水题就只贴代码了. Distinct Characters Subsequence 水. 代码: #include <cstdio> #include ...

  4. Codechef March Challenge 2014——The Street

    The Street Problem Code: STREETTA https://www.codechef.com/problems/STREETTA Submit Tweet All submis ...

  5. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries

    https://www.codechef.com/DEC17/problems/CHEFEXQ 题意: 位置i的数改为k 询问区间[1,i]内有多少个前缀的异或和为k 分块 sum[i][j] 表示第 ...

  6. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays

    https://www.codechef.com/DEC17/problems/CHEFHAM #include<cstdio> #include<cstring> #incl ...

  7. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And his Cake

    https://www.codechef.com/DEC17/problems/GIT01 #include<cstdio> #include<algorithm> using ...

  8. codechef January Challenge 2014 Sereja and Graph

    题目链接:http://www.codechef.com/JAN14/problems/SEAGRP [题意] 给n个点,m条边的无向图,判断是否有一种删边方案使得每个点的度恰好为1. [分析] 从结 ...

  9. 刷漆(Codechef October Challenge 2014:Remy paints the fence)

    [问题描述] Czy做完了所有的回答出了所有的询问,结果是,他因为脑力消耗过大而变得更虚了:).帮助Czy恢复身材的艰巨任务落到了你的肩上. 正巧,你的花园里有一个由N块排成一条直线的木板组成的栅栏, ...

随机推荐

  1. UVa 12235 状压DP Help Bubu

    题解戳这 一开始没看懂题解,后来想明白以后,d(i, j, s, x)是考虑第i本书的时候,前面已经拿走了j本书,剩下的书的种类的二进制状态为s,剩下的最后一本书的编号为x,所能得到的最小混乱度. 这 ...

  2. Python并发(一)

    假设我们要从一个网站用Python程序下载5张图片,最传统的思路就是写个for循环挨个挨个下载,但是这样做有个缺点,就是除了第一张,每张图片都必须等待前一张图片下载完毕后,才可以开始下载.由于网络有很 ...

  3. 00018_流程控制语句switch

    1.选择结构switch switch 条件语句也是一种很常用的选择语句,它和if条件语句不同,它只能针对某个表达式的值作出判断,从而决定程序执行哪一段代码. 2.switch语句的语法格式 swit ...

  4. 【NOIP2016】愤怒的小鸟 搜索

    题目描述 Kiana 最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于 (0,0)(0,0) 处,每次 Kiana 可以用它向第一象限发射一只红色的小鸟,小 ...

  5. TensorFlow笔记——

    主要依赖包 protocal buffer 处理结构化数据的工具:序列化(结构化数据->数据流) + 还原(数据流->结构化数据) protocol buffer与XML和JSON的区别: ...

  6. 【整理】python中re的match、search、findall、finditer区别

    match 从首字母开始开始匹配,string如果包含pattern子串,则匹配成功,返回Match对象,失败则返回None,若要完全匹配,pattern要以$结尾. search 若string中包 ...

  7. 零基础自学用Python 3开发网络爬虫

    原文出处: Jecvay Notes (@Jecvay) 由于本学期好多神都选了Cisco网络课, 而我这等弱渣没选, 去蹭了一节发现讲的内容虽然我不懂但是还是无爱. 我想既然都本科就出来工作还是按照 ...

  8. 【Luogu】P3396哈希冲突(根号算法)

    题目链接 根号算法真的是博大精深啊……明明是暴力但复杂度就是能过 这也太强了吧!!! 预处理出p<=sqrt(n)的所有情况,耗时n根n 查询: 如果p<=根n,O1查表 如果p>= ...

  9. [图论训练]BZOJ 2118: 墨墨的等式 【最短路】

    Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在 ...

  10. [图论训练]BZOJ 3245: 最快路线【最短路】

    Description 精 明的小R每每开车出行总是喜欢走最快路线,而不是最短路线.很明显,每条道路的限速是小R需要考虑的关键问题.不过有一些限速标志丢失了,于是小R将不知 道能开多快.不过有一个合理 ...