https://vjudge.net/problem/CodeChef-FNCS

题意:

思路:

用分块的方法,对每个函数进行分块,计算出该分块里每个数的个数,这样的话也就能很方便的计算出这个分块里所有数的和。

用树状数组维护数组的话可以很方便的计算出某个区间内所有数的和以及修改某个数。

每次查询时,如果在中间块的函数,我们直接加上sum[i](sum[i]为预处理的每一块的和),对于两边的函数,就用树状数组快速求一下和即可。

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstring>
  4. #include<cstdio>
  5. #include<vector>
  6. #include<stack>
  7. #include<queue>
  8. #include<cmath>
  9. #include<map>
  10. #include<set>
  11. using namespace std;
  12. typedef long long ll;
  13. typedef pair<int,int> pll;
  14. const int INF = 0x3f3f3f3f;
  15. const int maxn = 1e5+;
  16.  
  17. int n , m, unit, num;
  18. int a[maxn],l[maxn],r[maxn];
  19. int cnt[][maxn];
  20. ll c[maxn],sum[maxn];
  21.  
  22. int lowbit(int x)
  23. {
  24. return x&-x;
  25. }
  26.  
  27. ll getsum(int x)
  28. {
  29. ll ret = ;
  30. while(x>)
  31. {
  32. ret+=c[x];
  33. x-=lowbit(x);
  34. }
  35. return ret;
  36. }
  37.  
  38. int add(int x, int d)
  39. {
  40. while(x<=n)
  41. {
  42. c[x]+=d;
  43. x+=lowbit(x);
  44. }
  45. }
  46.  
  47. void update(int x, int y)
  48. {
  49. add(x,y-a[x]);
  50. for(int i=;i<=num;i++) sum[i]+=(ll)cnt[i][x]*(y-a[x]);
  51. a[x]=y;
  52. }
  53.  
  54. ll query(int left, int right)
  55. {
  56. ll ans = ;
  57. int l_unit=(left-)/unit+,r_unit=(right-)/unit+;
  58. if(l_unit==r_unit)
  59. {
  60. for(int i=left;i<=right;i++)
  61. ans+=getsum(r[i])-getsum(l[i]-);
  62. }
  63. else
  64. {
  65. for(int i=l_unit+;i<r_unit;i++) ans+=sum[i];
  66. for(int i=left;i<=(l_unit)*unit;i++) ans+=getsum(r[i])-getsum(l[i]-);
  67. for(int i=(r_unit-)*unit+;i<=right;i++) ans+=getsum(r[i])-getsum(l[i]-);
  68. }
  69. return ans;
  70. }
  71.  
  72. int main()
  73. {
  74. //freopen("in.txt","r",stdin);
  75. scanf("%d",&n);
  76. memset(c,,sizeof(c));
  77. for(int i=;i<=n;i++) {scanf("%d",&a[i]);add(i,a[i]);}
  78. for(int i=;i<=n;i++) scanf("%d%d",&l[i],&r[i]);
  79.  
  80. unit = (int)sqrt(n+0.5);
  81. num = unit + (unit*unit!=n);
  82. int now = ;
  83. for(int i=;i<=n;i++)
  84. {
  85. if(i%unit==) now++;
  86. cnt[now][l[i]]++;
  87. cnt[now][r[i]+]--;
  88. }
  89.  
  90. for(int i=;i<=num;i++)
  91. {
  92. for(int j=;j<=n;j++)
  93. {
  94. cnt[i][j]+=cnt[i][j-];
  95. sum[i]=sum[i]+(ll)cnt[i][j]*a[j];
  96. }
  97. }
  98.  
  99. scanf("%d",&m);
  100. while(m--)
  101. {
  102. int op, x, y;
  103. scanf("%d%d%d",&op,&x,&y);
  104. if(op==) update(x,y);
  105. else printf("%lld\n",query(x, y));
  106. }
  107. return ;
  108. }

CodeChef - FNCS Chef and Churu(分块)的更多相关文章

  1. Codechef FNCS Chef and Churu

    Disciption Chef has recently learnt Function and Addition. He is too exited to teach this to his fri ...

  2. CodeChef Chef and Churu [分块]

    题意: 单点修改$a$ 询问$a$的区间和$f$的区间和 原来普通计算机是这道题改编的吧... 对$f$分块,预处理$c[i][j]$为块i中$a_j$出现几次,$O(NH(N))$,只要每个块差分加 ...

  3. 【Codechef-Hard】Chef and Churu 分块

    题目链接: https://www.codechef.com/problems/FNCS Solution 大力分块.. 对序列分块,维护块内前缀和.块的前缀和,修改时暴力维护两个前缀和,询问单点答案 ...

  4. 【xsy2111】 【CODECHEF】Chef and Churus 分块+树状数组

    题目大意:给你一个长度为$n$的数列$a_i$,定义$f_i=\sum_{j=l_i}^{r_i} num_j$. 有$m$个操作: 操作1:询问一个区间$l,r$请你求出$\sum_{i=l}^{r ...

  5. chef and churu 分块 好题

    题目大意 有一个长度为n的数组A 有n个函数,第i个函数 \[f(l[i],r[i])=\sum_{k=l[i]}^{r[i]}A_k\] 有两种操作: 1)修改A[i] 2)询问第x-y个函数值的和 ...

  6. [CC-FNCS]Chef and Churu

    [CC-FNCS]Chef and Churu 题目大意: 一个长度为\(n(n\le10^5)\)的数列\(A_{1\sim n}\),另有\(n\)个函数,第\(i\)个函数会返回数组中标号在\( ...

  7. [Codechef CHSTR] Chef and String - 后缀数组

    [Codechef CHSTR] Chef and String Description 每次询问 \(S\) 的子串中,选出 \(k\) 个相同子串的方案有多少种. Solution 本题要求不是很 ...

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

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

  9. CodeChef FNCS (分块+树状数组)

    题目:https://www.codechef.com/problems/FNCS 题解: 我们知道要求区间和的时候,我们用前缀和去优化.这里也是一样,我们要求第 l 个函数到第 r 个函数 [l, ...

随机推荐

  1. super和this关键字

    super关键字: this关键字: 栈内存和堆内存和方法区内存分析: 其中,new出来的即对象都在堆内存区: main方法先进栈: 方法区中 [[ super_class ]]是编译器生成,代码表现 ...

  2. WebStorm: The Smartest JavaScript IDE by JetBrains

    WebStorm: The Smartest JavaScript IDE by JetBrains https://www.jetbrains.com/webstorm/?fromMenu

  3. 阿里巴巴json fastjson String转javaBean

    private Entity getEntity(String resp){        JSONObject jsonObj = (JSONObject) JSON.parse(resp);    ...

  4. emmm

    #include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; st ...

  5. LCA 最近公共祖先 (模板)

    #include <iostream> #include <stdio.h> #include <cstring> #include <vector> ...

  6. pat 团体赛练习题集 L2-008. 最长对称子串

    对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定"Is PAT&TAP symmetric?",最长对称子串为"s PAT&TAP s&quo ...

  7. kivy __init__() got an unexpected keyword argument '__no_builder' Kivy

    from kivy.lang.builder import Builder from kivy.app import App, runTouchApp from kivy.uix.boxlayout ...

  8. mysql启动、关闭与登录

    按照上述三篇随笔中的方法安装mysql,其启动.关闭和登录方法如下. mysql启动基本原理:/etc/init.d/mysqld是一个shell启动脚本,启动后最终会调用mysql\bin\mysq ...

  9. 20145326蔡馨熤《网络对抗》—— Web基础

    20145326蔡馨熤<网络对抗>—— Web基础 1.实验后回答问题 (1)什么是表单. 表单是一个包含表单元素的区域,表单元素是允许用户在表单中输入信息的元素,表单在网页中主要负责数据 ...

  10. HTML <frame> 标签的 src 属性

    HTML <frame> 标签 实例 src 属性规定在框架中显示的文档的位置: <html> <frameset cols="50%,50%"> ...