[codechef FNCS]分块处理+树状数组
题目链接:https://vjudge.net/problem/CodeChef-FNCS
在一个地方卡了一晚上,就是我本来以为用根号n分组,就会分成根号n个。事实上并不是。。。。因为用的是根号n下取整分组,得到的组数要用n/floor(sqrt(n))具体计算。
另外还有各种奇怪的bug……包括unsigned long long什么的……orz
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int maxn=;
int a[maxn];
int fl[maxn],fr[maxn];
int cnt[maxn][];
ull res[];
ull tree[maxn];
int N,block_size; int lowbit(int x)
{
return x&-x;
} void add(int k,int x)
{
while (k<=N)
{
tree[k]+=x;
k+=lowbit(k);
}
} ull query(int k)
{
ull res=0ull;
while (k)
{
res+=tree[k];
k-=lowbit(k);
}
return res;
} void init(int n)
{
N=n;
for (int i=;i<=N;i++) tree[i]=0ull;
} ull query2(int k)
{
if (k<) return ;
ull r=0ull;
int block_id=(k-)/block_size+;
for (int i=;i<block_id;i++) r+=res[i];
for (int i=(block_id-)*block_size+;i<=k;i++)
{
r+=query(fr[i])-query(fl[i]-);
}
return r;
} int main()
{
int n;
scanf("%d",&n);
for (int i=;i<=n;i++) scanf("%d",&a[i]);
for (int i=;i<=n;i++) scanf("%d%d",&fl[i],&fr[i]);
block_size=sqrt(n);
int block=n/block_size;
for (int i=;i<=block;i++)
{
for (int j=(i-)*block_size+;j<=i*block_size;j++)
{
cnt[fr[j]+][i]--;
cnt[fl[j]][i]++;
}
res[i]=;
for (int j=;j<=n;j++)
{
cnt[j][i]+=cnt[j-][i];
res[i]+=1ull*a[j]*cnt[j][i];
}
}
init(n);
for (int i=;i<=n;i++) add(i,a[i]);
int q;
scanf("%d",&q);
while (q--)
{
int op,x,y;
scanf("%d%d%d",&op,&x,&y);
if (op==)
{
ull r=query2(y)-query2(x-);
printf("%llu\n",r);
}
else
{
for (int i=;i<=block;i++)
{
res[i]+=1ull*cnt[x][i]*(y-a[x]);
}
add(x,y-a[x]);
a[x]=y;
}
}
return ;
}
[codechef FNCS]分块处理+树状数组的更多相关文章
- 【BZOJ 3529】 [Sdoi2014]数表 (莫比乌斯+分块+离线+树状数组)
3529: [Sdoi2014]数表 Description 有一张N×m的数表,其第i行第j列(1 < =i < =礼,1 < =j < =m)的数值为能同时整除i和j的所有 ...
- NOIp 数据结构专题总结 (2):分块、树状数组、线段树
系列索引: NOIp 数据结构专题总结 (1) NOIp 数据结构专题总结 (2) 分块 阅:<「分块」数列分块入门 1-9 by hzwer> 树状数组 Binary Indexed T ...
- Codeforces 785 E. Anton and Permutation(分块,树状数组)
Codeforces 785 E. Anton and Permutation 题目大意:给出n,q.n代表有一个元素从1到n的数组(对应索引1~n),q表示有q个查询.每次查询给出两个数l,r,要求 ...
- Bzoj 3295: [Cqoi2011]动态逆序对 分块,树状数组,逆序对
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2886 Solved: 924[Submit][Stat ...
- Bzoj 2141: 排队 分块,逆序对,树状数组
2141: 排队 Time Limit: 4 Sec Memory Limit: 259 MBSubmit: 1310 Solved: 517[Submit][Status][Discuss] D ...
- BZOJ_4765_普通计算姬_分块+dfs序+树状数组
BZOJ_4765_普通计算姬_分块 Description "奋战三星期,造台计算机".小G响应号召,花了三小时造了台普通计算姬.普通计算姬比普通计算机要厉害一些 .普通计算机能 ...
- BZOJ2141:排队(分块,树状数组)
Description 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家 乐和和.红星幼儿园的小朋友们排起了长长地队伍,准备吃果果.不过因为小朋友们 ...
- HDU4947GCD Array(莫比乌斯反演+树状数组)
题面 传送门 题解 orz ljz 相当于每一个数要加上 \[v\times [\gcd(i,n)=d]=v\times [\gcd(i/d,n/d)=1]=v\times \sum_{p|{i\ov ...
- 【分块+树状数组】codechef November Challenge 2014 .Chef and Churu
https://www.codechef.com/problems/FNCS [题意] [思路] 把n个函数分成√n块,预处理出每块中各个点(n个)被块中函数(√n个)覆盖的次数 查询时求前缀和,对于 ...
随机推荐
- 生产Web架构优化方案(动态转静态)
Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.门户新闻业务: 1. 特点:网页一旦发布,再次改动网页内容的几率很低,新闻业务内容的静态化相对比较简单 2. ...
- Go语言获取本地IP地址
最近要做一个向局域网内的所有设备广播发送信息,并接受设备的回复信息,回复信息包括设备的版本号,IP地址,运行工程名等信息.发现一个局域网内是可以有不同的网段的,但UDP广播只能是同一个网段的广播.又发 ...
- R语言学习笔记(五):零碎知识点(11-15)
11--which.min(), which.max()和which() which(x, arr.ind = FALSE, useNames = TRUE) x 是一个向量或者数组,可以是NA,但会 ...
- ABAP CDS ON HANA-(4)ヘッダー行編集
Explicit Name List use in CDS We create a simple CDS View like below. @AbapCatalog.sqlViewName: ‘ZS ...
- os模块3
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cd os.curd ...
- 《python核心编程第二版》第2章习题
2-1 略 2-1 略 2-2 (a)打印 结果是9 (b)9 (c)一样 (d)略 (e)略 2-3 略 2-4 (a) # /usr/bin/pythonraw_input() (b) # /us ...
- cocos2d-x的坐标和节点层级
- (转) Unreal Engine 4 Custom Shaders Tutorial
说明: 1.这里的Custom Shaders 为且仅为 Custom Node的使用和USF的包含.并非全局Shader和Material Shader. 2.原文来源:https://www.ra ...
- ZooKeeper完全分布式安装与配置
Apache ZooKeeper是一个为分布式应用所设计开源协调服务,其设计目是为了减轻分布式应用程序所承担的协调任务.可以为用户提供同步.配置管理.分组和命名服务. 1.环境说明 在三台装有cent ...
- HDU 4744 Starloop System(最小费用最大流)(2013 ACM/ICPC Asia Regional Hangzhou Online)
Description At the end of the 200013 th year of the Galaxy era, the war between Carbon-based lives a ...