CDOJ 1059 秋实大哥与小朋友 STL(set)+离散化+BIT区间更新单点查询
Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu
System Crawler (2016-04-23)
Description
秋实大哥以周济天下,锄强扶弱为己任,他常对天长叹:安得广厦千万间,大庇天下寒士俱欢颜。
所以今天他又在给一群小朋友发糖吃。
他让所有的小朋友排成一行,从左到右标号。在接下去的时间中,他有时会给一段区间的小朋友每人$v$颗糖,有时会问第$x$个小朋友手里有几颗糖。
这对于没上过学的孩子来说实在太困难了,所以你看不下去了,请你帮助小朋友回答所有的询问。
Input
第一行包含两个整数$n$,$m$,表示小朋友的个数,以及接下来你要处理的操作数。
接下来的$m$行,每一行表示下面两种操作之一:
0 l r v : 表示秋实大哥给[l,r]这个区间内的小朋友每人v颗糖
1 x : 表示秋实大哥想知道第x个小朋友手里现在有几颗糖
$1\leq m, v\leq 100000$,$1\leq l\leq r\leq n$,$1\leq x\leq n$,$1\leq n\leq 100000000$。
Output
对于每一个$1$ $x$操作,输出一个整数,表示第$x$个小朋友手里现在的糖果数目。
Sample Input
3 4
0 1 3 1
1 2
0 2 3 3
1 3
Sample Output
1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const double pi=acos(-1);
const int maxn=100000;
ll tri[2*maxn+10];
int n,m;
set<int> judge;
vector<int> point; struct Ope{
int op,l,r,v,pos;
}; int lowbit(int i)
{
return i&(-i);
} ll query(int pos)
{
ll sum=0;
while(pos<=point.size())
{
sum+=tri[pos];
pos+=lowbit(pos);
}
return sum;
}//区间更新中的单点查询 void update(int l,int r,int v)
{
while(r>0)
{
tri[r]+=v;
r-=lowbit(r);
}
int k=l-1;
while(k>0)
{
tri[k]-=v;
k-=lowbit(k);
}
}//区间更新 int main()
{
while(~scanf("%d %d",&n,&m))
{
queue<Ope> opq;
judge.clear();
point.clear();
MM(tri,0); for(int k=1;k<=m;k++)
{
int w,l,r,v,pos;
scanf("%d",&w);
if(w==0)
{
scanf("%d %d %d",&l,&r,&v);
if(!judge.count(l))
{
judge.insert(l);
point.push_back(l);
}
if(!judge.count(r))
{
judge.insert(r);
point.push_back(r);
}
opq.push((Ope){0,l,r,v,0});
}
else
{
scanf("%d",&pos);
if(!judge.count(pos))
{
judge.insert(pos);
point.push_back(pos);
}
opq.push((Ope){1,0,0,0,pos});
}
} sort(point.begin(),point.end());//用point来离散化
while(opq.size())
{
Ope cur=opq.front();opq.pop();
if(cur.op==1)
{
int pos=lower_bound(point.begin(),point.end(),cur.pos)-point.begin()+1;
printf("%lld\n",query(pos));
}//pos代表离散化后在BIT中的位置
else
{
int l=lower_bound(point.begin(),point.end(),cur.l)-point.begin()+1;
int r=lower_bound(point.begin(),point.end(),cur.r)-point.begin()+1;
update(l,r,cur.v);
}
}
}
return 0;
}
1:set可以用来判重judge.count(判断元素的值),但是不能对其进行迭代器的加减法;
2:对要进行操作的点进行离散化;
3:BIT区间更新+单点查询
CDOJ 1059 秋实大哥与小朋友 STL(set)+离散化+BIT区间更新单点查询的更多相关文章
- UESTC 1059 - 秋实大哥与小朋友
题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 Time Limit: 3000/1000MS (Java/Others) Memory Li ...
- UESTC_秋实大哥与小朋友 2015 UESTC Training for Data Structures<Problem A>
A - 秋实大哥与小朋友 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
- 2015 UESTC 数据结构专题A题 秋实大哥与小朋友 线段树 区间更新,单点查询,离散化
秋实大哥与小朋友 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Desc ...
- UESTC-1059 秋实大哥与小朋友(离散化+线段树)
秋实大哥与小朋友 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...
- CDOJ 1061 C - 秋实大哥与战争 STL set 迭代器
题目链接: C - 秋实大哥与战争 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Sub ...
- CDOJ 1069 秋实大哥去打工 单调栈 下标处理
E - 秋实大哥去打工 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit St ...
- CDOJ 1070 秋实大哥打游戏 带权并查集
链接 F - 秋实大哥打游戏 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit ...
- CDOJ 1060 秋实大哥与快餐店 字典树 水题
题目链接 B - 秋实大哥与快餐店 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Sub ...
- CDOJ 1057 秋实大哥与花 线段树 区间更新+区间查询
链接: I - 秋实大哥与花 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit ...
随机推荐
- 线程间通信共享变量和queue
在多线程中,数据是共享,如何在多线程安全的通信,是首先要可虑的问题的 #线程间的通信 import timeimport threadingfrom threading import RLock de ...
- XSS绕过WAF的姿势
初始测试 1.使用无害的payload,类似<b>,<i>,<u> 观察响应,判断应用程序是否被HTML编码,是否标签被过滤,是否过滤<>等等: 2.如 ...
- HDU 2094 产生冠军(STL map)
产生冠军 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- mysql for update 高并发 死锁研究
mysql for update语句 https://www.cnblogs.com/jtlgb/p/8359266.html For update带来的思考 http://www.cnblo ...
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题解
题面 这道题是一道标准的01分数规划: 但是有一些细节可以优化: 不难想到要二分一个mid然后判定图上是否存在一个环S,该环是否满足∑i=1t(Fun[vi]−mid∗Tim[ei])>0 但是 ...
- C++中的类型识别
1,为什么会提出类型识别概念呢? 1,为什么在 C 语言中没有提出这个概念呢,就是因为在 C++ 中引入了面向对象的特性,面向对象里面有一个非常重要的原则就是赋值兼容性原则: 2,在面向对象中可能出现 ...
- python3的base64编解码
使用python3的base64编解码实现字符串的简易加密解密 引言: 在一些项目中,接口的报文是通过base64加密传输的,所以在进行接口自动化时,需要对所传的参数进行base64编码,对拿到的响应 ...
- vscode如何使用?常用插件有哪些?
vscode下载 官网下载:https://code.visualstudio.com/ 一.汉化中文(官方下载默认为英文,英文好的小伙伴可直接跳过这步) 点击插件按钮搜索 Chinese, 在弹出的 ...
- vue项目进行时,script标签中,methods事件中函数使用的async/await
用 async/await 来处理异步 await关键字只能放到async函数里面,通过await得到就是Promise返回的内容:当然也能通过then()去获取,若通过then()获取了则就无Pro ...
- ALV打印不显示打印界面的问题
用OO的方式screen0 不画屏幕会产生这个问题,解决办法就是不用screen0 要自己画一个区域