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学习(三十九)—— Django之Form组件
一.构建一个表单 假设你想在你的网站上创建一个简单的表单,以获得用户的名字.你需要类似这样的模板: <form action="/your-name/" method=&qu ...
- fastadmin模态框(弹出框)
用法: 在html页面新建一个按钮用来触发事件 <a href="javascript:;" class="btn btn-success btn-add &quo ...
- MD5加密工具类
简单的md5加密: package com.util; import java.security.MessageDigest; import java.security.NoSuchAlgorithm ...
- angularJs实现数据双向绑定的原理
angular1.x在指定的事件触发时(比如dom事件,xhr响应事件,浏览器定位变更事件,定时器事件),通过脏值检测的方式比对数据是否有变更,来决定是否更新视图. angular2使用了zone.j ...
- IE下CSS3伪类的支持
当css3.0出现以后,着实让我兴奋了好久,因为出现了很多选择器,我们在也不用靠js做复杂判断了.比如:nth-child,很容易就可以判断奇偶对象 “:nth-child(2n)和:nth-chil ...
- [LeetCode] To Lower Case 转为小写
Implement function ToLowerCase() that has a string parameter str, and returns the same string in low ...
- CF_2018-2019 Russia Open High School Programming Contest (Unrated, Online Mirror, ICPC Rules, Teams Preferred)
只做了两个就去上课去啦... A. Company Merging time limit per test 1 second memory limit per test 512 megabytes i ...
- Dev_VGridControl的使用
这个控件适合GridControl 控件相对应的,用来竖排显示用户数据,具体效果如下图: 实现这个效果用户可以参见官方帮助文档中的如下图所示的内容: 这里我对我用到的Category Rows(分类显 ...
- python模拟---注册登陆查看个人信息
需求:1.模拟注册: 2.模拟登陆: 3.模拟登陆成功显示登陆成功的用户账号: 一.注册 代码如下: def regetist(): ''' :param username: 注册的账号 :param ...
- Hibernate--Day01
Hibernate是一个面向对象的持久化框架 持久化: 1,把内存中的Java对象保存到存储设备上面: 2,最好的解诀方案:把对象持久化到数据库里面: 3, 在Java里面,把对象持久化到数据库只能使 ...