luogu5012 水の数列 (并查集+线段树)
如果我们能求出来每个区间个数的最大分值,那就可以用线段树维护这个东西 然后出答案了
然后这个的求法和(luogu4269)Snow Boots G非常类似,就是我们把数大小排个序,每次都拿<=x的位置去合并那个并查集,同时维护个数和大小
#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define pa pair<double,int>
#define CLR(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=1e6+; inline char gc(){
return getchar();
static const int maxs=<<;static char buf[maxs],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,maxs,stdin),p1==p2)?EOF:*p1++;
}
inline ll rd(){
ll x=;char c=gc();bool neg=;
while(c<''||c>''){if(c=='-') neg=;c=gc();}
while(c>=''&&c<='') x=(x<<)+(x<<)+c-'',c=gc();
return neg?(~x+):x;
} struct Node{
int v,i;
}p[maxn];
int fa[maxn],siz[maxn],N,T;
pa ma[maxn<<];
bool flag[maxn];
int nowcnt;
ll nowsum; inline bool cmp(Node a,Node b){return a.v<b.v;} inline int getf(int x){return x==fa[x]?x:getf(fa[x]);} inline void uni(int x){
flag[x]=;siz[x]=;
nowcnt++;
if(flag[x-]){
int a=getf(x-);
nowsum-=1ll*siz[a]*siz[a],siz[x]+=siz[a],fa[a]=x;
nowcnt--;
}if(flag[x+]){
int b=getf(x+);
nowsum-=1ll*siz[b]*siz[b],siz[x]+=siz[b],fa[b]=x;
nowcnt--;
}nowsum+=1ll*siz[x]*siz[x];
} inline void update(int p){ma[p]=max(ma[p<<],ma[p<<|]);} inline void change(int p,int l,int r,int x,pa y){
if(l==r) ma[p]=max(ma[p],y);
else{
int m=l+r>>;
if(x<=m) change(p<<,l,m,x,y);
else change(p<<|,m+,r,x,y);
update(p);
}
} inline pa query(int p,int l,int r,int x,int y){
if(x<=l&&r<=y) return ma[p];
int m=l+r>>;pa re=make_pair(,);
if(x<=m) re=query(p<<,l,m,x,y);
if(y>=m+) re=max(re,query(p<<|,m+,r,x,y));
return re;
} int main(){
//freopen("","r",stdin);
int i,j,k;
N=rd(),T=rd();
for(i=;i<=N;i++){
p[i].v=rd(),p[i].i=i;
}sort(p+,p+N+,cmp);
for(i=;i<=N;i++) fa[i]=i;
for(i=;i<=N;i++){
uni(p[i].i);
if(p[i].v!=p[i+].v) change(,,N,nowcnt,make_pair(1.0*nowsum/p[i].v,p[i].v));
}
ll lastans=;
for(i=;i<=T;i++){
ll a=rd(),b=rd(),x=rd(),y=rd();
int l=(a*lastans+x-)%N+,r=(b*lastans+y-)%N+;
if(l>r) swap(l,r);
pa re=query(,,N,l,r);
if(re.first==){
printf("-1 -1\n");
}else
printf("%lld %d\n",(ll)(re.first*re.second+0.5),re.second);
printf("%d %d %d\n",l,r,lastans);
if(re.first==) lastans=;
else lastans=((ll)(re.first*re.second+0.5))%N*re.second%N;
}
return ;
}
luogu5012 水の数列 (并查集+线段树)的更多相关文章
- UVA1455 - Kingdom(并查集 + 线段树)
UVA1455 - Kingdom(并查集 + 线段树) 题目链接 题目大意:一个平面内,给你n个整数点,两种类型的操作:road x y 把city x 和city y连接起来,line fnum ...
- 并查集&线段树&树状数组&排序二叉树
超级无敌巨牛逼并查集(带权并查集)https://vjudge.net/problem/UVALive-4487 带删点的加权并查集 https://vjudge.net/problem/UVA-11 ...
- 【Codeforces576E_CF576E】Painting Edges(可撤销并查集+线段树分治)
题目 CF576E 分析: 从前天早上肝到明天早上qwq其实颓了一上午MC ,自己瞎yy然后1A,写篇博客庆祝一下. 首先做这题之前推荐一道很相似的题:[BZOJ4025]二分图(可撤销并查集+线段树 ...
- BZOJ 3910 并查集+线段树合并
思路: 1. 并查集+线段树合并 记得f[LCA]==LCA的时候 f[LCA]=fa[LCA] 2.LCT(并不会写啊...) //By SiriusRen #include <cstdio& ...
- bzoj 3237 连通图 - 并查集 - 线段树
Input Output Sample Input 4 5 1 2 2 3 3 4 4 1 2 4 3 1 5 2 2 3 2 1 2 Sample Output Connected Disconne ...
- [SCOI2011]棘手的操作(可并堆/并查集/线段树)
我懒死了 过于棘手 但这题真的很水的说 毕竟写啥都能过 常见思路: ①:由于不强制在线,所以重新编号之后线段树维护 ②:用各种可以高速合并的数据结构,比如可并堆,可并平衡树啥的 讲一种无脑算法: 对于 ...
- 并查集 + 线段树 LA 4730 Kingdom
题目传送门 题意:训练指南P248 分析:第一个操作可以用并查集实现,保存某集合的最小高度和最大高度以及城市个数.运用线段树成端更新来统计一个区间高度的个数,此时高度需要离散化.这题两种数据结构一起使 ...
- YYHS-猜数字(并查集/线段树维护)
题目描述 LYK在玩猜数字游戏. 总共有n个互不相同的正整数,LYK每次猜一段区间的最小值.形如[li,ri]这段区间的数字的最小值一定等于xi. 我们总能构造出一种方案使得LY ...
- 【CF471E】MUH and Lots and Lots of Segments 扫描线+并查集+线段树+set
[CF471E]MUH and Lots and Lots of Segments 题意:给你平面上n条水平或竖直的,端点在整点处的线段.你需要去掉一些线段的一些部分,使得剩下的图形:1.连通,2.无 ...
随机推荐
- vue 开发依赖安装
安装element-ui yarn add element-ui --save 使用element-ui main.js import Vue from 'vue'; import ElementUI ...
- asp.net core Api配置swagger
这个很简单的一篇文章用来记录以下使用swagger的过程,以后有用. 1.nuget 下载install-package Swashbuckle.AspNetCore 2.startup里面confi ...
- Oracle 条件判断函数decode和case when then案例
--decode条件判断函数 ,,,,,) from dual --需求:不通过连表查询,显示业主类型名称列的值 ,,,'商业','其他') from t_owners --case when the ...
- MySQL5.5 安装配置方法教程
MySQL下载地址:http://dev.mysql.com/downloads/installer/ 1.首先进入的是安装引导界面 2.然后进入的是类型选择界面,这里有3个类型:Typical(典型 ...
- SpringBoot 4.SpringBoot 整合 devtools 实现热部署
一.添加 devtools 依赖 <!-- Spring boot 热部署 : 此热部署会遇到 java.lang.ClassCastException 异常 --> <!-- op ...
- Android常用的技术框架与博客社区
技术框架 图片加载 Glide Fresco Volley Picasso Universal Image Loader 网络请求 okhttp retrofit Volley android-asy ...
- python之路-字符串
一.类型转换 a = 10 print(type(a)) # <class 'int'> d = str(a) # 把数字转换成str print(type(d)) # <class ...
- systemd取消对服务重启的限制
默认情况下,一个服务在10秒内最多允许启动5次.当超过5次后,会报如下错误: Job for xx.service failed because start of the service was at ...
- 关于Select2下拉框组件
文档如下: https://select2.org/configuration/options-api
- initial
摘自http://blog.csdn.net/liming0931/article/details/7039680 首先说说结构化过程语句,在verilog中有两种结构化的过程语句:initial语句 ...