问题 D: Segment Balls

时间限制: 1 Sec  内存限制: 128 MB 提交: 253  解决: 37

题目描述

Small K has recently earn money in stock market , so he want to make balls to celebrate it.

Now he buys so many balls , and he want to put it into the boxes , he will have two operators to do it.

There are n boxes , which number is in range of (1 , n)

operator 1: PUT B C   he can put C balls to boxes whose number is multiple of B

operator 2: QUERY D E  he want to know the total number of balls in boxes number D to boxes number E.

We guarantee that 1 <= B ,C <= 10 1 <= D <= E <= 1e6

输入

The first number CASE is the case number.Then will be CASE cases.

At every case , the first number is n(1<=n<=10^6) ,means that we have n boxes.

the second number is q , which means the q operators will be give.

And then will be q lines ,which means q operators.

(1 <= q <= 100000)

输出

Every case ,  print the total balls for every question . A question a line.

样例输入

1
4
8
PUT 1 3
QUERY 2 4
PUT 2 3
QUERY 1 4
QEURY 1 1
QEURY 2 2
QUERY 3 3
QUERY 4 4

样例输出

9
18
3
6
3
6 题意:有n个空盒子,进行两种操作:
1)PUT x y 给标号为x的倍数的盒子全加上y个球。
2)QUERY x y 求标号从x到y的盒子里总球数。
分析:维护每次给线段加的倍数x及加数y,那么给每段加上y*(该段上x的倍数点总数(r-l)/x)即可维护每段的总和,x最多有
10种,因此刚开始直接来用第二维为10的lazy数组标志,但爆内存了,后来改用vector,代码各种乱,还好最后几分钟AC了。
这题有更简便的方法,直接开个大小为11的数组记录好整段每种的倍数的put值,最后加上所有的put值乘以线段长度/倍数x即可。
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 1000010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int n;
struct node
{
int x,y;
};
LL sum[N<<];
vector<node>col[N<<];
void Pushup(int rt)
{
int ls=rt<<,rs=ls|;
sum[rt]=sum[ls]+sum[rs];
}
void build(int l,int r,int rt)
{
if(l==r)
{
sum[rt]=;
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
Pushup(rt);
}
int cal(int i,int l,int r)//计算该段倍数为i的点总数
{
int len=,num=;
for(int j=l;j<=r&&num<=;j++)
{
num++;
if(j%i==)
{
l=j;len++;
break;
}
}
len+=(r-l)/i;
return len;
}
void Pushdown(int rt,int l,int r)
{
int ls=rt<<,rs=ls|;
int m=(l+r)>>;
for(int i=,sz=col[rt].size();i<sz;i++)
if(col[rt][i].y)
{
int flag=;
for(int j=,z=col[ls].size();j<z;j++)//维护10种x值即可
{
if(col[ls][j].x==col[rt][i].x)
{
col[ls][j].y+=col[rt][i].y;flag=;break;
}
}
if(!flag)col[ls].push_back(col[rt][i]);
flag=;
for(int j=,z=col[rs].size();j<z;j++)
{
if(col[rs][j].x==col[rt][i].x)
{
col[rs][j].y+=col[rt][i].y;flag=;break;
}
}
if(!flag)col[rs].push_back(col[rt][i]);
sum[ls]+=1LL*col[rt][i].y*cal(col[rt][i].x,l,m);//同样维护好sum值
sum[rs]+=1LL*col[rt][i].y*cal(col[rt][i].x,m+,r);
}
col[rt].clear();
}
void update(int L,int R,int x,int y,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
int flag=;
for(int i=,sz=col[rt].size();i<sz;i++)//维护10种x值即可
{
if(col[rt][i].x==x)
{
col[rt][i].y+=y;flag=;break;
}
}
node now;
now.x=x;now.y=y;
if(!flag)col[rt].push_back(now);
sum[rt]+=1LL*y*cal(x,l,r);//加上y*(x的倍数点和)
return;
}
}
LL query(int L,int R,int l,int r,int rt)//求区间和
{
if(L<=l&&r<=R)return sum[rt];
Pushdown(rt,l,r);
int m=(l+r)>>;
LL res=;
if(L<=m)res+=query(L,R,lson);
if(m<R)res+=query(L,R,rson);
return res;
}
int main()
{
int T,q;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)col[i].clear();
build(,n,);
while(q--)
{
char op[];
int x,y;
scanf("%s%d%d",op,&x,&y);
if(op[]=='P')
{
update(,n,x,y,,n,);
}
else
{
if(x>n)
{
puts("");continue;
}
if(y>n)y=n;
printf("%lld\n",query(x,y,,n,));
}
}
}
}
												

NEU月赛Segment Balls(线段树)的更多相关文章

  1. 【BZOJ-3165】Segment 李超线段树(标记永久化)

    3165: [Heoi2013]Segment Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 368  Solved: 148[Submit][Sta ...

  2. codeforces 242E - XOR on Segment (线段树 按位数建树)

    E. XOR on Segment time limit per test 4 seconds memory limit per test 256 megabytes input standard i ...

  3. 【BZOJ4919】[Lydsy六月月赛]大根堆 线段树合并

    [BZOJ4919][Lydsy六月月赛]大根堆 Description 给定一棵n个节点的有根树,编号依次为1到n,其中1号点为根节点.每个点有一个权值v_i. 你需要将这棵树转化成一个大根堆.确切 ...

  4. HDU 5125 magic balls(线段树+DP)

    magic balls Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. HDU 4107 Gangster Segment Tree线段树

    这道题也有点新意,就是须要记录最小值段和最大值段,然后成段更新这个段,而不用没点去更新,达到提快速度的目的. 本题过的人非常少,由于大部分都超时了,我严格依照线段树的方法去写.一開始竟然也超时. 然后 ...

  6. Luogu P4097 [HEOI2013]Segment 李超线段树

    题目链接 \(Click\) \(Here\) 李超线段树的模板.但是因为我实在太\(Naive\)了,想象不到实现方法. 看代码就能懂的东西,放在这里用于复习. #include <bits/ ...

  7. 2019.02.11 bzoj3165: [Heoi2013]Segment(线段树)

    传送门 题意简述:要求支持两种操作: 插入一条线段. 询问与直线x=kx=kx=k相交的线段中,交点最靠上的线段的编号. 思路: 直接上李超线段树即可. 代码: #include<bits/st ...

  8. Segment 李超线段树

    题目大意: 要求在平面直角坐标系下维护两个操作: 1.在平面上加入一条线段.记第 i 条被插入的线段的标号为 i 2.给定一个数 k,询问与直线 x = k 相交的线段中,交点最靠上的线段的编号. 若 ...

  9. 【洛谷P4097】Segment 李超线段树

    题目大意:维护一个二维平面,给定若干条线段,支持询问任意整数横坐标处对应的纵坐标最靠上的线段的 id,相同高度取 id 值较小的,强制在线. 题解:初步学习了李超线段树.李超线段树的核心思想在于通过标 ...

随机推荐

  1. Cocos2d-x教程第(11)讲-利用遮罩(蒙版)CCLayerColor制作新手引导界面(上)

    欢迎转载,转载时请注明原文出处:http://blog.csdn.net/u012945598/article/details/17280019 源码下载地址:http://download.csdn ...

  2. Cookie例子

    马士兵老师的~~ cookie是服务器将信息保存在客户端的一个表示方式 名-值 服务器只能写入文档文件 每个浏览器都有唯一的标识号 且每个浏览器只允许访问与自身相关的cookie的内容 cookie分 ...

  3. How to decompile class file in Java and Eclipse - Javap command example(转)

    Ability to decompile a Java class file is quite helpful for any Java developer who wants to look int ...

  4. [SVN] 分支同步、合入主干操作分享

    冲突的解决原则 不是自己修改的地方就使用主干的. 需要特别注意的是: 分支同步主干时,远端(theirs)是主干,本地(mine/working)的是分支: 分支合入主干时,本地(mine/worki ...

  5. 基础知识(10)- 部署应用程序和applet

    10.1 JAR文件  10.1.1 清单文件  10.1.2 可运行JAR文件  10.1.3 资源  10.1.4 密封 10.2 Java Web Start  10.2.1 沙箱  10.2. ...

  6. ORACLE 安装Oracle12遇到的问题

    0.全然卸载Oracle10(Windows) 在Windows下多次安装Oracle会造成混乱.重装Oracle的话一定先要干净卸载曾经的Oracle. 一.有必要时先备份 二.卸载步骤 1.用DB ...

  7. android 请求网络 和 httpclient的使用上传下载

    访问网络最主要的也就是 http协议了. http协议很简单,但是很重要. 直接上代码了,里面都是1个代码块 代码块的,用哪一部分直接拷出去用就好了. 1.访问网络用 get 和 post  自己组拼 ...

  8. csdn的登录框好难看

    不好意思说实话了,新的登陆框样式挺难看的,那种橙不明朗,介于黄和橙之间,跟整个网站主色调红和黑很不搭.不过,倒是有点跟风Win8平实的style,但是比Win8更简陋了点. tooltip要不加都不加 ...

  9. 同事的Excel中的图片突然不能选择

    今天上午,同事突然说自己用的Excel不能编辑了,发来一看原来是其中做的图片不能编辑,鼠标放上去后显示个圆圈选不中. 在“视图”中调出“控件工具箱”工具栏,上面有一个三角板与直尺样子的按钮叫“设计模式 ...

  10. HTML 页面载入 Flash 插件的几种方法

    前言 之所以写这篇文章,主要是由于组长给提的一个新的需求--使用浏览器调用电脑的摄像头,来实现即时拍照的功能.在网上查了非常多资料,由于这样那样的原因,终于选择了使用flash插件来调用pc的摄像头. ...