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.无 ...
随机推荐
- CI框架在控制器中切换读写库和读写库
CodeIgniter框架版本:3.1.7 ,php版本:5.6.* ,mysql版本:5.6 在Ci框架中,可以在application/config/database.php中配置多个group, ...
- vue 开发依赖安装
安装element-ui yarn add element-ui --save 使用element-ui main.js import Vue from 'vue'; import ElementUI ...
- PHP的优化建议(仅借鉴)
转载: https://www.awaimai.com/1050.html 1 字符串 1.1 少用正则表达式 能用PHP内部字符串操作函数的情况下,尽量用他们,不要用正则表达式, 因为其效率高于正则 ...
- Java中有关Null的9件事(转)
对于Java程序员来说,null是令人头痛的东西.时常会受到空指针异常(NPE)的骚扰.连Java的发明者都承认这是他的一项巨大失误.Java为什么要保留null呢?null出现有一段时间了,并且我认 ...
- vue二次实战(二)
https://www.cnblogs.com/jellify/p/9522477.html install的弹出框中输入sublimeTmpl,找到sublimeTmpl这个插件后回车 Vue路由 ...
- flutter图片铺满父框
正常我们需要显示一张图片,会用到Image这个控件. 打个比方,我们加载一张本地的图片, 先看一下这个Image.asset的源码: Image.asset(String name, { Key ke ...
- Python 构建工具 buildout 的介绍与使用
来到了新公司上班,首先就是得把自己的环境给搭起来.知乎使用了buildout作为python项目的构建工具. 那么什么是 buildout ? buildout的是一款自动化构建工具.由Zope团队开 ...
- Chrome 75 & lazy-loading
Chrome 75 & lazy-loading https://addyosmani.com/blog/lazy-loading/ https://chromestatus.com/feat ...
- php1
正则表达式 $p = '/name:(\w+\s?\w+)/'; $str = "name:steven jobs"; preg_match($p, $str, $match); ...
- CF 1041 1042整理
终于回来整理了,这两场比赛我也是醉了,第一场-1分,第二场被skip,还是太菜啊qaq CF1041 T1T2过水忽略直接看后面 T3大意:给你一个长度为n的序列a1,a2,a3···an,你需要把这 ...