[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个)覆盖的次数 查询时求前缀和,对于 ...
随机推荐
- 毕业2年 Summary
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/135 看了下去年写毕业一周年总结的时间:2017-6-16,今天 ...
- (数据科学学习手札34)多层感知机原理详解&Python与R实现
一.简介 机器学习分为很多个领域,其中的连接主义指的就是以神经元(neuron)为基本结构的各式各样的神经网络,规范的定义是:由具有适应性的简单单元组成的广泛并行互连的网络,它的组织能够模拟生物神经系 ...
- 2018徐州网络赛H. Ryuji doesn't want to study
题目链接: https://nanti.jisuanke.com/t/31458 题解: 建立两个树状数组,第一个是,a[1]*n+a[2]*(n-1)....+a[n]*1;第二个是正常的a[1], ...
- [Cracking the Coding Interview] 4.3 List of Depths
Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth. ...
- 4 class类 web服务器
1.换行符 2.pycharm 连接Ubuntu 1)添加环境变量 2)查看ip 3)配置目录 4)上传或者下载 3.面向对象抽象web服务器 1)版本1:类 class HttpServer(obj ...
- 在编程的时候,NotePad++ 中闪烁的光标突然有竖着闪烁的编程蓝色下划线闪烁的--小技巧告诉你-费元星
当在写代码时出现的光标闪烁(横线闪烁) 在键盘上找 Insert ,按这个Insert就可以把横向闪烁光标( _ )修改成竖向闪烁光标样式( | ),横向光标会在你写代码的时候修改前面的代码,把光标移 ...
- Ubuntu 手机 app开发学习0
# 相关网址 http://developer.ubuntu.com/zh-cn/apps/sdk/ 0. 环境搭建 首选需要一个Ubuntu 14.04操作系统.没啥好讲的,直接安装了一个虚拟机. ...
- springmvc基础篇—使用注解方式为前台提供数据
一.新建一个Controller package cn.cfs.springmvc.service; import java.util.ArrayList; import java.util.Hash ...
- javaX邮件发送
/** * * * @param mailServerHost 邮件服务器 * @param mailServerPort 端口 * @param validate 是否需要身份验证 * @para ...
- [leetcode-648-Replace Words]
In English, we have a concept called root, which can be followed by some other words to form another ...