1299 切水果

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 大师 Master
 查看运行结果
 
 
题目描述 Description

简单的说,一共N个水果排成一排,切M次,每次切[L,R]区间的所有水果(可能有的水果被重复切),每切完一次输出剩下水果数量

数据已重新装配,不会出现OLE错误

时限和数据范围适当修改,避免数据包过大而浪费空间资源

输入描述 Input Description

第1行共包括2个正整数,分别为N,M。

接下来m行每行两个正整数L,R

输出描述 Output Description

一共输出M行,每行输出切完之后剩下水果数量

样例输入 Sample Input

10 3

3 5

2 8

1 5

样例输出 Sample Output

7

3

2

数据范围及提示 Data Size & Hint

30%的数据满足N,M<=5,000

60%的数据满足N,M<=100,000

100% 的数据满足1<=L<=R<=N<=500,000,1<=M<=500,000

题意:n个水果排成一排 m次查询 每次删除[L,R]区间内的水果 并输出剩下的水果的数量

题解:线段树处理 题目数据有问题 存在L<0   R>N的数据

 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<queue>
#define ll __int64
using namespace std;
struct node
{
int l,r;
int w;
int add;
}tree[];
void buildtree(int root,int left,int right)
{
tree[root].l=left;
tree[root].r=right;
tree[root].add=;
if(left==right)
{
tree[root].w=;
return ;
}
int mid=(left+right)>>;
buildtree(root<<,left,mid);
buildtree(root<<|,mid+,right);
tree[root].w=tree[root<<].w+tree[root<<|].w;
}
void pushdown(int root)
{
if(tree[root].add==) return;
tree[root<<].add=;
tree[root<<|].add=;
tree[root].add=;
tree[root<<].w=;
tree[root<<|].w=;
}
void updata(int root,int left,int right,int c)
{
if(tree[root].l==left&&tree[root].r==right)
{
tree[root].add=c;
tree[root].w=;
return ;
}
pushdown(root);
int mid=(tree[root].l+tree[root].r)>>;
if(right<=mid)
{
updata(root<<,left,right,c);
}
else
{
if(left>mid)
updata(root<<|,left,right,c);
else
{
updata(root<<,left,mid,c);
updata(root<<|,mid+,right,c);
}
}
tree[root].w=tree[root<<].w+tree[root<<|].w;
}
int query(int root ,int left,int right)
{
if(tree[root].l==left&&tree[root].r==right)
{
return tree[root].w;
}
pushdown(root);
int mid=(tree[root].l+tree[root].r)>>;
if(mid<=right)
return query(root<<,left,right);
else
{
if(left>mid)
return query(root<<|,left,right);
else
return query(root<<,left,mid)+query(root<<|,mid+,right);
}
}
int n,m;
int le,ri;
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
buildtree(,,n);
for(int i=;i<=m;i++)
{
scanf("%d %d",&le,&ri);
updata(,max(,le),min(n,ri),);
printf("%d\n",query(,,n));
}
}
return ;
}

codevs 1299 线段树 区间更新查询的更多相关文章

  1. poj 3468 线段树区间更新/查询

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  2. POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】

    任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  3. HDU 1698 Just a Hook(线段树区间更新查询)

    描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...

  4. codevs 1191 线段树 区间更新(水)

    题目描述 Description 在一条数轴上有N个点,分别是1-N.一开始所有的点都被染成黑色.接着我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色.请输出每个操作执行后剩余黑色点的个数. ...

  5. 2017 Wuhan University Programming Contest (Online Round) D. Events,线段树区间更新+最值查询!

    D. Events 线段树区间更新查询区间历史最小值,看似很简单的题意写了两天才写出来. 题意:n个数,Q次操作,每次操作对一个区间[l,r]的数同时加上C,然后输出这段区间的历史最小值. 思路:在线 ...

  6. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  7. ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)

    1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况 ...

  8. 【POJ 2777】 Count Color(线段树区间更新与查询)

    [POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4094 ...

  9. codevs 1690 开关灯 线段树区间更新 区间查询Lazy

    题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...

随机推荐

  1. Oracle连接查询内容整理

    --内连接--select t.*,b.bumenmc from T_HQ_RYXX t,t_hq_bm b where t.bum = b.bumenbm--select * from t_hq_r ...

  2. 2.4.2电子书fb.c文件

    显示层面头文件 定义结构体,为显示统一标准 int (*DeviceInit)(void); 显示类驱动初始化 int (*ShowPixel)(int iPenX, int iPenY, unsig ...

  3. https://docs.mongodb.org/manual/reference/operator/aggregation/unwind/#examples

    https://docs.mongodb.org/manual/reference/operator/aggregation/unwind/#examples http://www.clusterdb ...

  4. WP8.1 Study3:WP8.1中Animation应用

    WP8.1上的Animation动画的API和WIN8/WIN8.1上的差不多,网上可以找到很多资料,同时可以去MSDN看官方文档. 下面是我参考一些资料,写出来的例子,希望以后有用. xaml代码如 ...

  5. 【转】 linux 安装nginx及编译参数详解

    版权声明:本文为博主原创文章,未经博主允许不得转载. 从官网下载一个nginx 的tar.gz 版. 安装方式使用make 安装 第一步:解压 tar -zxvf  nginx-1.7.4.tar.g ...

  6. 谁说固态硬盘没容量?4TB诞生、明年8TB!

    固态硬盘已经逐渐取代机械硬盘成为很多用户的首选,但唯一欠缺的就是容量,或者说单位容量的价格,但是如今,机械硬盘的容量提升举步维艰,固态硬盘却在突飞猛进. 近日,SanDisk就宣布了全球第一款容量高达 ...

  7. Swift语言中如何使用JSON数据教程

    这是一篇翻译文章,原文出处:http://www.raywenderlich.com/82706/working-with-json-in-swift-tutorial Swift语言中如何使用JSO ...

  8. Java选择结构、循环结构

    1:switch语句(掌握) (1)格式: switch(表达式) { case 值1: 语句体1; break; case 值2: 语句体2; break; ... default: 语句体n+1; ...

  9. android 单选、多选弹出菜单

    菜单单选窗口: import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInte ...

  10. hdoj-2021

    #include "stdio.h"void calculate(int number,int &a,int &b,int &c,int &d,in ...