codecomb 2098【stone】
题目描述
Description
n个石堆围成一圈,提供两种操作:
1、每次将[L,R]堆的石子数量+k,其中,1<=L,R<=n,k>=0。
2、询问有最多石子的那一堆有多少石子。
现在,要求在线解决该问题。
Input
第一行两个整数n和m,n表示石子圈的长度,m表示操作数量。
以下m行,首先一个是整数t,t=1或2,表示是哪种操作。 如果t=1,则后面跟三个整数l,r,k,表示区间[l,r]的所有石子堆石子数量+k,如果t=2表示上述询问操作。
Output
对于每一个询问,输出石子数量。
Sample Input
3 3
1 1 2 1
1 3 1 2
2
Sample Output
3
Hint
数据范围:
10%的数据m<=1000
40%的数据n<=100000
100%的数据n<2^32,m<=100000
答案保证在longint范围内
离散完线段树维护区间最大值
我会说原来我一直不会线段树维护区间最大最小?
不过黄巨大说正解其实是类似可持久化线段树的搞法
一开始直接搞出l=1 r=2^32的第一条线段,然后修改时照样修改,遇到完全覆盖的时候直接更新tag退出,否则递归,没有左右节点的就插入一条边,这样每次最坏插入一条链,只有32个
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 2147483647
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
#define mod 100007
#define N 300010
using namespace std;
struct hashing{
int next,rnk;
LL v;
}hash[N];
bool operator <(const hashing &a,const hashing &b){return a.v<b.v;}
struct oprations{
int l,r,x;
bool opr;//0:work 1:query
}w[N];
int rnk[N];
int head[mod];
int n,m,cnt;
LL chafen[N];
LL sum,ans,toadd;
inline int ins(LL w)
{
LL u=w%mod;
for (int i=head[u];i;i=hash[i].next)
if (hash[i].v==w) return i;
hash[++cnt].v=w;
hash[cnt].next=head[u];
hash[cnt].rnk=cnt;
head[u]=cnt;
return cnt;
}
inline LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct segtree{
int l,r,mx,tag;
}tree[1000010];
int treesize;
inline void pushdown(int k)
{
int t=tree[k].tag;
tree[k].tag=0;
tree[k<<1].tag+=t;
tree[k<<1|1].tag+=t;
tree[k<<1].mx+=t;
tree[k<<1|1].mx+=t;
}
inline void update(int k)
{
if (tree[k].l==tree[k].r)return;
tree[k].mx=max(tree[k<<1].mx,tree[k<<1|1].mx);
}
inline void buildtree(int now,int l,int r)
{
tree[now].l=l;tree[now].r=r;
if (l==r)return;
int mid=(l+r)>>1;
buildtree(now<<1,l,mid);
buildtree(now<<1|1,mid+1,r);
}
inline void add(int now,int l,int r,int dat)
{
pushdown(now);
int x=tree[now].l,y=tree[now].r;
if (x==l&&y==r)
{
tree[now].tag+=dat;
tree[now].mx+=dat;
return;
}
int mid=(x+y)>>1;
if (r<=mid)add(now<<1,l,r,dat);
else if(l>mid)add(now<<1|1,l,r,dat);
else
{
add(now<<1,l,mid,dat);
add(now<<1|1,mid+1,r,dat);
}
update(now);
}
int main()
{
n=read();m=read();
for (int i=1;i<=m;i++)
{
w[i].opr=(read()==2);
if (!w[i].opr)
{
w[i].l=ins(read());
w[i].r=ins(read());
w[i].x=read();
}
}
sort(hash+1,hash+cnt+1);
for (int i=1;i<=cnt;i++)
rnk[hash[i].rnk]=i;
for (int i=1;i<=m;i++)
if (!w[i].opr)
{
w[i].l=rnk[w[i].l];
w[i].r=rnk[w[i].r];
}
buildtree(1,1,cnt);
for (int i=1;i<=m;i++)
{
if(w[i].opr)printf("%d\n",tree[1].mx);
else
{
if (w[i].l>w[i].r)
{
add(1,1,w[i].r,w[i].x);
add(1,w[i].l,cnt,w[i].x);
}else add(1,w[i].l,w[i].r,w[i].x);
}
}
}
codecomb 2098【stone】的更多相关文章
- codecomb 2086【滑板鞋】
题目背景 我的滑板鞋时尚时尚最时尚 回家的路上我情不自禁 摩擦 摩擦 在这光滑的地上摩擦 月光下我看到自己的身影有时很远有时很近 感到一种力量驱使我的脚步 有了滑板鞋天黑都不怕 题目描述 你在魅力之都 ...
- 【UVA1378】A Funny Stone Game (博弈-求SG值-输出方案)
[题目] Description The funny stone game is coming. There are n piles of stones, numbered with 0, 1, 2, ...
- 【BZOJ2138】stone Hall定理+线段树
[BZOJ2138]stone Description 话说Nan在海边等人,预计还要等上M分钟.为了打发时间,他玩起了石子.Nan搬来了N堆石子,编号为1到N,每堆包含Ai颗石子.每1分钟,Nan会 ...
- 【BZOJ2138】stone(线段树,Hall定理)
[BZOJ2138]stone(线段树,Hall定理) 题面 BZOJ 题解 考虑一个暴力. 我们对于每堆石子和每个询问,显然是匹配的操作. 所以可以把石子拆成\(a_i\)个,询问点拆成\(K_i\ ...
- codecomb 2100【警察叔叔就是这个人!】
题目背景 十个地方十人十色 全部都是猥琐大叔 这里也是那里也是 行踪可疑 现如今hentai横行,警察叔叔们不得不采取特♂殊手段惩戒这些家伙 题目描述 魅力之都是一个有N个路口,M条双向道路连接的城市 ...
- HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)
Friends and Enemies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- 【POJ1113】Wall(凸包)
[题目] Description Once upon a time there was a greedy King who ordered his chief Architect to build a ...
- 第一阶段——CentOS6_Python3.6.1笔记(尚学堂-Python基础快速入门)+ 【补充】麦子-Python程序入门与进阶
虚拟机环境: 设置网络 .修改网络地址 .设置网卡为nat模式 .确保物理机启动dhcp.net服务 .编辑文件:vim /etc/sysconfig/network-scripts/ifcfg-et ...
- Fruits【水果】
Fruits Many of us love July because it's the month when nature's berries and stone fruits are in abu ...
随机推荐
- POJ 2752 Seek the Name, Seek the Fame (KMP next 数组 变形)
题意:给一个字符串S,判断在什么下标的时候,前缀和后缀相等,输出前缀和后缀相等的点. 分析:next数组的一种很巧妙的用法 next数组表示的意义是当前下标前面k字符和开头的前面k个字符相等 所以就会 ...
- 精通find命令
一.前言 find命令是linux使用过程中经常用到的命令,但可能大家只会如下使用find find ./ 或者这样使用 find ./ | grep str 上述命令等同于 find ./ -nam ...
- poj 3684 Physics Experiment(数学,物理)
Description Simon ), the first ball is released and falls down due to the gravity. After that, the b ...
- hdu 3853 LOOPS(概率 dp 期望)
Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help ...
- 面试时如何优雅的谈论OC
在面试中,我们经常会遇到一些原理性的问题,很常识但很难用通俗的语言解释清楚,这也是大部分业务级程序员经常失误的地方.虽然写了多年代码,但是核心思想不清,导致自己的后续发展受限,这是一个优秀的程序员和普 ...
- python之路-模块安装 paramiko
paramiko介绍(全是洋文,看不懂啊,赶紧有道翻译吧,等有朝一日,我去报个华尔街): "Paramiko" is a combination of the esperanto ...
- hdu 1728 逃离迷宫(dFS+优先队列)
求转弯最少的走路方式!!!! #include<stdio.h> #include<string.h> #include<queue> using namespac ...
- AngularJS Directive - 开场小介绍(转)
Directive其实就是让html变得更强大的一种方法.它可以根据需求对dom变形,或注入行为. 觉得它很神秘么,其实一点儿也不神秘,只要开始使用AngularJS了,就一定在使用着Directiv ...
- 为什么报错说req未定义,createServer只接受匿名函数吗?
var http = require('http');var server = new http.createServer(handlerRequest(req,res));server.listen ...
- QQ浏览器不支持JS问题
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...