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 ...
随机推荐
- python全栈开发day103-python垃圾回收机制、mro和c3算法解析、跨域jsonp\CORS、Content-Type组件
Python垃圾回收 -- 引用计数 -- Python为每个对象维护一个引用计数 -- 当引用计数为0的 代表这个对象为垃圾 -- 标记清除 -- 解决孤立的循环引用 -- 标记根节点和可达对象 - ...
- java.net.NoRouteToHostException: No route to host解决方法
当访问192.168.10.98上的tomcat时候,tomcat日志异常 然后查看tomcat服务器上的防火墙 查看service层的防火墙: 至此问题解决: 永久关闭防火墙: 1.首先查看防火墙状 ...
- ubantu中执行docker免sudo方法
1.添加用户组,如果已存在则不用设置. sudo groupadd docker 2.将用户加入该 group (docker)内 sudo gpasswd -a ${USER} docker 3.重 ...
- Android数据存储引擎---SQLite数据库
目标:是否可以在PC端桌面上使用SQLite数据库制作一个财务文件? 目录: 来源: 实践: 总结和比较: SQLite数据简介 是什么,内部结构是怎样的,数据库和表的关系是什么 有什么用 常用的操作 ...
- thinkphp框架,数据动态缓存后,或数据已读取出来,想分页怎么办
//读取缓存后赋值到数组,通过array_slice函数处理,如: $blog = S('blogname'); //赋值 $count = count($blog); //条数统计 $page = ...
- ApiUser
using System; using System.Runtime.InteropServices; using System.Text; using HANDLE = System.IntPtr; ...
- C博客作业01--分支,顺序结构
本章学习总结(2分) 1.1思维导图 1.2本章学习体会及代码量学习体会 1.2.1学习体会 本周初次接触C语言,一开始难度较大,很多代码都看不懂,书里面的章节要看很多遍.开始编写代码时也遇到很多困难 ...
- PHP算法学习(8) 环形链表 解决约瑟夫问题
2019年2月25日17:29:17 Josephus有过的故事:39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓.于是决定了自杀方式,41个人排成一个圆圈 ...
- webservice接口测试wsdl,参数是xml格式。python,入参转化成str,返回值转化成dict调用
1.用SoapUI测试webservice接口,传入参数是xml格式时.xml格式需要将xml的外围增加<![CDATA[xml]]> 2.但是用python去做webservice测试, ...
- eclipse maven工程打包失败
报错如下: Maven install失败 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:comp ...