HDU 1556 BIT区间修改+单点查询(fread读入优化)
BIT区间修改+单点查询
【题目链接】BIT区间修改+单点查询
&题解:
BIT区间修改+单点查询和求和的bit是一模一样的(包括add,sum) 只不过是你使用函数的方式不一样:
使用区间的时候,比如[a,b]区间+1,就是add(a,1); add(b+1,-1); 之后sum(i)查的是i点的值,是一个i点的值,不是区间!!
另外,主函数中fread()必须调用2句话,因为它是缓冲的,所以不可以边输入边测试,只能用freopen测试, 另外附上时间比较,第一个是用的fread,第二个是没用


我还测了一下getchar()的读入挂,结果是780ms,比scanf还慢点,总感觉那个挂没什么用哦

&代码:
#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define fo(i,a,b) for(int i=(a);i<=(b);i++)
#define fd(i,a,b) for(int i=(a);i>=(b);i--)
#define cle(a,v) memset(a,(v),sizeof(a))
const int maxn = 100000 + 7;
//Reading Plugin
const int BUF = 20000000;
char Buf[BUF], *buf = Buf;
const int OUT = 10000000;
char Out[OUT], *ou = Out; int Outn[30], Outcnt;
inline void write(int x) {
if(!x)*ou++ = 48;
else {
for(Outcnt = 0; x; x /= 10)Outn[++Outcnt] = x % 10 + 48;
while(Outcnt)*ou++ = Outn[Outcnt--];
}
}
inline void writell(ll x) {
if(!x)*ou++ = 48;
else {
for(Outcnt = 0; x; x /= 10)Outn[++Outcnt] = x % 10 + 48;
while(Outcnt)*ou++ = Outn[Outcnt--];
}
}
inline void writechar(char x) {*ou++ = x;}
inline void writeln() {*ou++ = '\n';}
inline void read(int&a) {for(a = 0; *buf < 48; buf++); while(*buf > 47)a = a * 10 + *buf++ -48;}
int n, a, b;
struct BIT {
int n, bit[maxn];
void ori(int _n) {
n = _n;
cle(bit, 0);
}
int lowb(int x) { return x & -x; }
void add(int i, int x) {
for(; i <= n; i += lowb(i))
bit[i] += x;
}
int sum(int i) {
int s = 0;
for(; i; i -= lowb(i))
s += bit[i];
return s;
}
} bt;
int main() {
#ifndef ONLINE_JUDGE
freopen("E:1.in", "r", stdin);
#endif
fread(Buf, 1, BUF, stdin);
while(1) {
read(n);
if(n == 0) break;
bt.ori(n);
for(int i = 0; i < n; i++) {
read(a); read(b);
bt.add(a, 1);
bt.add(b + 1, -1);
}
for(int i = 1; i <= n; i++) {
write(bt.sum(i));
if(i == n) writechar('\n');
else writechar(' ');
}
}
fwrite(Out, 1, ou - Out, stdout);
return 0;
}
HDU 1556 BIT区间修改+单点查询(fread读入优化)的更多相关文章
- 【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers
http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状 ...
- hdu-1556 Color the ball---树状数组+区间修改单点查询
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1556 题目大意: Problem Description N个气球排成一排,从左到右依次编号为1,2 ...
- D - Mayor's posters POJ - 2528 离散化+线段树 区间修改单点查询
题意 贴海报 最后可以看到多少海报 思路 :离散化大区间 其中[1,4] [5,6]不能离散化成[1,2] [2,3]因为这样破坏了他们的非相邻关系 每次离散化区间 [x,y]时 把y+1点也加入 ...
- BZOJ4999:This Problem Is Too Simple!(DFS序&树上差分&线段树动态开点:区间修改单点查询)
Description 给您一颗树,每个节点有个初始值. 现在支持以下两种操作: 1. C i x(0<=x<2^31) 表示将i节点的值改为x. 2. Q i j x(0<=x&l ...
- POJ2155 Matrix(二维树状数组||区间修改单点查询)
Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row an ...
- 【树状数组区间修改单点查询】HDU 4031 Attack
http://acm.hdu.edu.cn/showproblem.php?pid=4031 [题意] 有一个长为n的长城,进行q次操作,d为防护罩的冷却时间,Attack表示区间a-b的墙将在1秒后 ...
- hdu 3966 Aragorn's Story(树链剖分+区间修改+单点查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:给一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路径上 ...
- HDU 5861 Road(线段树 区间修改 单点查询)
Road Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- cogs 1316. 数列操作B 区间修改 单点查询
1316. 数列操作B ★★ 输入文件:shulieb.in 输出文件:shulieb.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 假设有一个大小为 n(n ...
随机推荐
- Linux 包管理
1 文集 <Linux 包管理基础:apt.yum.dnf 和 pkg>,由Snapcrafter翻译,英文原创(作者Brennen)地址在这里. 这是一篇不错的概括性的文章,系统介绍了D ...
- 普通用户登陆系统显示 -bash-4.1$
如题,今天上午用户登陆系统之后显示-bash-4.1$ 造成这样的原因: 与这个用户有关环境变量没了,有关的文件被删除.也就是用户的家目录下面 .bash_profile .bashrc 被删除. 解 ...
- 3.基于梯度的攻击——PGD
PGD攻击原论文地址——https://arxiv.org/pdf/1706.06083.pdf 1.PGD攻击的原理 PGD(Project Gradient Descent)攻击是一种迭代攻击,可 ...
- 手机号流量统计---Mapreduce项目分析
文档显示: 每行依次是 ~手机号~上行流量~下行流量 需求分析: 需要统计各自的手机号,及上行.下行.总流量 具体做法: 1.定义map输入输出类型 通常情况下map的输入的key-value就是lo ...
- 网络编程-Python高级语法-property属性
知识点: 一.什么是property属性? 一种用起来像是使用的实例属性一样的特殊属性,可以对应于某个方法,Python的property属性的功能是:property属性内部进行一系列的逻辑计算,最 ...
- 001.Ansible部署RHCS存储集群
一 前期准备 1.1 前置条件 至少有三个不同的主机运行monitor (MON)节点: 至少三个直接存储(非外部SAN硬件)的OSD节点主: 至少两个不同的manager (MGR)节点: 如果使用 ...
- GCD Counting-树形DP
GCD Counting 思路: 预处理 每个权值的素因子.问题转化为 以同一个素数作为因子 最长的链, 树形DP求解,ans 由 此点的 最长子链 + 次长子链 相加得到, 然后再更新最长子链 ...
- 洛谷P1379 八数码难题
传送门 1.先用dfs枚举9!的全排列,存到hash数组里(类似离散化),因为顺序枚举,就不需要排序了 2.朴素bfs,判重就用二分找hash:如果发现当前状态=要求状态,输出步数结束程序 上代码 # ...
- maven与eclipse集成
https://www.cnblogs.com/teach/p/5906425.html
- JS功能函数
1.整数每三位增加一个, function toThousands(num) { return (num || 0).toString().replace(/(\d)(?=(?:\d{3}) ...