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 ...
随机推荐
- docker-网桥
使用桥接网络 在网络方面,桥接网络是链路层设备,它在网络段之间转发流量. 网桥可以是硬件设备或在主机内核中运行的软件设备. Docker而言,桥接网络使用软件桥接器,该软件桥接器允许连接到同一桥接网络 ...
- C#反射与特性(七):自定义特性以及应用
目录 1,属性字段的赋值和读值 2,自定义特性和特性查找 2.1 特性规范和自定义特性 2.2 检索特性 3,设计一个数据验证工具 3.1 定义抽象验证特性类 3.2 实现多个自定义验证特性 3.3 ...
- MySQL快速回顾:计算字段与函数
9.1 计算字段 存储在数据库表中的数据一般不是应用程序所需要的格式.比如: 如果想要在一个字段中既显示公司名,又显示公式的地址,但这两个信息一般包含在不同的表列中. 城市.州和邮政编码存储在不同的列 ...
- .Net Core Web Api实践(四)填坑连接Redis时Timeout performing EVAL
前言:前两篇文章.net core+Redis+IIS+nginx实现Session共享中,介绍了使用Microsoft.Extensions.Caching.Redis实现Session共享的方法, ...
- Ubuntu18.04 安装配置mongodb
一.安装 # 1. 更新 sudo apt-get update # 2. 安装 sudo apt-get install -y mongodb # 3. 查看是否安装成功 # a. 服务状态 sud ...
- Egret学习-初次创建项目
最近无聊,好久没有写游戏了,决定学习下egret,主要原因:egret是h5框架,相比android和iPhone或cocos2dx来说不需要安装可以直接运行. 下面进入正题,开始学习egret 简单 ...
- [洛谷P4585] [FJOI2015] 火星商店问题
Description 火星上的一条商业街里按照商店的编号 \(1\),\(2\) ,-,\(n\) ,依次排列着 \(n\) 个商店.商店里出售的琳琅满目的商品中,每种商品都用一个非负整数 \(va ...
- set(待整理)
set集合容器:实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,以保证每个子树根节点键值大于左子树所有节点的键值,小于右子树所有节点的键值:另外,还 ...
- 半夜了我来发张图 睡觉 ControllerDescriptor 与 ActionDescriptor 之间 的 关系
- Qt Installer Framework翻译(7-6)
工具 Qt Installer Framework包含以下工具: > installerbase > binarycreator > repogen > archivegen ...