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 ...
随机推荐
- UVA 10474 - Where is the Marble?--vector
https://vjudge.net/problem/UVA-10474 https://blog.csdn.net/xiyaozhe/article/details/81081344 简单用法 so ...
- python 爬虫与数据可视化--爬虫基础知识
一.python中的模块 模块的安装:pip install 模块名 导入模块与函数:import requests . from pymongo import MongoClient json模块的 ...
- JS this用法详解
随着对js的深入学习和使用,你会发现它里面包含了很多令人困惑的机制,比如对象.闭包.原型链继承等等 1.this是啥? 简言之,this是JavaScript语言中定义的众多关键字之一,它的特 ...
- linux 文件属性(转)
1. 文件类型 - 普通文件 d 目录文件 l 链接文件 b 块设备文件 c 字符型设备文件 s socket文件 p 管道类型文件 块设备文件主要是指慢速设备,比如hd硬盘,数据主要是分块存储,所 ...
- Java_泛型
转自博客HappyCorn https://www.cnblogs.com/lwbqqyumidi/p/3837629.html 什么是泛型? 泛型,即“参数化类型”.一提到参数,最熟悉的就是定义方法 ...
- (62)Wangdao.com第十天_JavaScript 变量的作用域
在 js 中有两种作用域:全局作用域,局部作用域. 全局作用域 直接写在 <script> 标签中的变量和方法. 在网页打开时创建,在网页关闭时销毁. 全局作用域有一个全局对象 windo ...
- BOM 浏览器对象模型_window 对象的常见 window.属性_window.方法
1. 常用属性 window.devicePixelRatio 像素比 = css / 物理像素 window.scrollX,window.scrollY 滚动条 卷曲距离 if ...
- react_app 项目开发 (5)_前后端分离_后台管理系统_开始
项目描述 技术选型 react API 接口 接口文档,url,请求方式,参数类型, 根据文档描述的方法,进行 postman 测试,看是否能够得到理想的结果 collections - 创建文件取项 ...
- 重构file_get_contents实现一个带超时链接访问的函数
function wp_file_get_contents($url, $timeout = 30) { $context = stream_context_create(array( 'http' ...
- vue组件创建学习总结
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...