2019icpc徐州现场赛 H Yuuki and a problem (树状数组套主席树)
题意
2e5的数组,q个操作
1.将\(a[x]\)改为y
2.求下标l到r内所有的\(a[i]\)通过加法不能构成的最小的值
思路
通过二操作可以知道需要提取l到r内的值及其数量,而提取下标为l到r内的元素是一定要用主席树的
而用树状数组套上主席树即可实现修改操作
剩下需要解决的就是二操作:
首先只有有至少一个1,才能构成1
假设已经可以构成[1,x],设当前区间内值为[1,x+1]的和为sum
那显然我们就能构成[1,sum]了,如果sum==x,那么答案就是x+1
这个过程可以直接暴力,最坏情况下当前区间里的数是{1,2,3,5,8,13...},在第27项就到到2e5了,所以最多跑27次
那么复杂度O(\(27qlog^2n\))
这题其实比较友好,不需要离散化,也不需要优化空间,赞
比赛的时候其实也很可写,可惜
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>
#define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
using namespace std;
typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL;
typedef pair<ll,int> PIL;
const db eps = 1e-2;
const int mod = 1e9+7;
const int maxn = 2e5+100;
const int maxm = maxn*150;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0);
int n,m;
inline int read(){
int num;
char ch;
while((ch=getchar())<'0' || ch>'9');
num=ch-'0';
while((ch=getchar())>='0' && ch<='9'){
num=num*10+ch-'0';
}
return num;
}
struct qst{
int op;
int x,y;
}prb[maxn];
ll a[maxn];
int totn,tot;
int rrot[maxn];
int ls[maxm],rs[maxm];
ll dat[maxm];
int root[maxn];
inline void insert(int &now, int l, int r, int x, int val){
if(!now)now=++tot;
dat[now]+=val;
if(l==r)return;
int mid = (l+r)>>1;
if(x<=mid)insert(ls[now],l,mid,x,val);
else insert(rs[now],mid+1,r,x,val);
}
int totL,totR;
int L[maxn],R[maxn];
inline ll ask(int l, int r, int k){
if(r==k){
ll ans=0;
for(int i = 1; i <= totL; i++)ans-=dat[L[i]];
for(int i = 1; i <= totR; i++)ans+=dat[R[i]];
return ans;
}
int mid=(l+r)>>1;
if(k<=mid){
for(int i = 1; i <= totL; i++)L[i]=ls[L[i]];
for(int i = 1; i <= totR; i++)R[i]=ls[R[i]];
return ask(l,mid,k);
}
else{
ll ans = 0;
for(int i = 1; i <= totL; i++)ans-=dat[ls[L[i]]];
for(int i = 1; i <= totR; i++)ans+=dat[ls[R[i]]];
for(int i = 1; i <= totL; i++)L[i]=rs[L[i]];
for(int i = 1; i <= totR; i++)R[i]=rs[R[i]];
return ans+ask(mid+1,r,k);
}
}
inline int lowbit(int x){return x&-x;}
int main() {
int q;
scanf("%d %d",&n, &q);
for(int i = 1; i <= n; i++){
a[i]=1ll*read();
}
totn=200000;
for(int i = 1; i <= n; i++){
int t = a[i];
for(int j = i; j <= n; j+=lowbit(j)){
insert(root[j],1,totn,t,a[i]);
}
}
for(int i = 1; i <= q; i++){
prb[i].op=read();prb[i].x=read();prb[i].y=read();
if(prb[i].op==1){
ll t = a[prb[i].x];
int tt = prb[i].y;
a[prb[i].x]=prb[i].y;
for(int j = prb[i].x; j<=n; j+=lowbit(j)){
insert(root[j],1,totn,t,-t);
insert(root[j],1,totn,tt,tt);
}
}
else{
prb[i].x--;
ll now = 1;
ll sum = 0;
while(1){
totL=totR=0;
for(int j = prb[i].x; j; j-=lowbit(j))L[++totL]=root[j];
for(int j = prb[i].y; j; j-=lowbit(j))R[++totR]=root[j];
int t = min(now,200000ll);
ll tmp = ask(1,totn,t);
if(tmp==sum){
printf("%lld\n",now);
break;
}
sum=tmp;now=sum+1;
}
}
}
return 0;
}
2019icpc徐州现场赛 H Yuuki and a problem (树状数组套主席树)的更多相关文章
- 2019南昌网络赛I:Yukino With Subinterval(CDQ) (树状数组套主席树)
题意:询问区间有多少个连续的段,而且这段的颜色在[L,R]才算贡献,每段贡献是1. 有单点修改和区间查询. 思路:46min交了第一发树套树,T了. 稍加优化多交几次就过了. 不难想到,除了L这个点, ...
- 2019南昌网络赛 I. Yukino With Subinterval 树状数组套线段树
I. Yukino With Subinterval 题目链接: Problem Descripe Yukino has an array \(a_1, a_2 \cdots a_n\). As a ...
- 13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 2224: Boring Counting Time Limit: 3 Sec ...
- HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Tota ...
- 2019~2020icpc亚洲区域赛徐州站H. Yuuki and a problem
2019~2020icpc亚洲区域赛徐州站H. Yuuki and a problem 题意: 给定一个长度为\(n\)的序列,有两种操作: 1:单点修改. 2:查询区间\([L,R]\)范围内所有子 ...
- 2018徐州网络赛H. Ryuji doesn't want to study
题目链接: https://nanti.jisuanke.com/t/31458 题解: 建立两个树状数组,第一个是,a[1]*n+a[2]*(n-1)....+a[n]*1;第二个是正常的a[1], ...
- 2019icpc徐州网络赛_I_query
题意 给定一个序列,多次询问区间\([l,r]\)中满足\(min(a[i],a[j])==gcd(a[i],a[j])\)的数对\((i,j)\)数. 分析 其实就是求区间有倍数关系的数对数. 由于 ...
- 计蒜客 41391.query-二维偏序+树状数组(预处理出来满足情况的gcd) (The Preliminary Contest for ICPC Asia Xuzhou 2019 I.) 2019年徐州网络赛)
query Given a permutation pp of length nn, you are asked to answer mm queries, each query can be rep ...
- Trace 2018徐州icpc网络赛 (二分)(树状数组)
Trace There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx ...
随机推荐
- FTP服务器虚拟用户配置
FTP服务配置问题及解决方案 使用被动模式,设置云主机IP为被动模式数据传输地址:在配置文件内添加 pasv_enable=YES pasv_promiscuous=YES pasv_address= ...
- 一条SQL注入引出的惊天大案2:无限战争
前情回顾: 经过黑衣人和老周的合作,终于清除了入侵Linux帝国的网页病毒,并修复了漏洞.不曾想激怒了幕后的黑手,一场新的风雨即将来临. 详情参见:一条SQL注入引出的惊天大案 风云再起 小Q是L ...
- 与drawable的较量(一)
前言 在android ui中drawable 是一个非常关键的要点,因为我们现在使用的花里胡哨的APP,都在胡里花哨的drawable 构建的. drawable,英文翻译为可拖拽的.说白了就是自己 ...
- python中各种文件打开模式
在python中,总的来说有三种大的模式打开文件,分别是:a, w, r 当以a模式打开时,只能写文件,而且是在文件末尾添加内容. 当以a+模式打开时,可以写文件,也可读文件,可是在读文件的时候,会发 ...
- .Net Core2.*学习手册
1.net core 基础知识解析(创建一个.net core网站)(视频录制) 1.1 Startup解析(没写) 1.2 目录结构分析(没写) 1.3 使用静态文件(没写) 1.4 Control ...
- Error connecting to the Service Control Manager: 拒绝访问 Mongodb问题-解决
原文地址:https://blog.csdn.net/carrot5032/article/details/74742888 发现在mongodb.log里出现 2017-07-07T17:01:5 ...
- 编程基础--XML约束
2020年新年第一天,不写一篇博客纪念一下都感觉对不起这个跨年 为什么会写一篇关于xml的博客呢?xml在编程中用的又不多,再多也用不着自己写约束文件,只要能看懂就行了不是吗?嗯,没别的原因,就是想研 ...
- 线性最长cover(无讲解)
#include<bits/stdc++.h> using namespace std; ; int n,f[maxn],cover[maxn],R[maxn]; char str[max ...
- Postwoman-接口测试工具
地址是:https://postwoman.io/ ,不过只能使用它们自己提供的测试接口,如果你调试自己的API接口的话,你需要自己部署一套代码. 自己搭建一套Postwomen环境的话,只需要安装了 ...
- 字符串(String)的创建,以及字符串的属性及方法
1.String对象的创建 方法1: var txt = new String("i am String"); console.log(txt); // 结果为:i am Stri ...