codeforces 1217E E. Sum Queries? (线段树

传送门:https://codeforces.com/contest/1217/problem/E

题意:

n个数,m次询问

单点修改

询问区间内最小的unbalanced number

balanced number定义是,区间内选取数字的和sum

sum上的每一位都对应着选取的数上的一位

否则就是unbalanced number

题解:

根据题意

如果区间存在unbalance number,那么一定存在两个数就可以组成unbalance number

为什么?

因为根据balanced number的定义来说,只要在和的一位上 有两个数字在这个位置上的数都不为0的话那么这两个数字一定可以组成unbalanced number,所以我们需要对每一位非0的数进行操作

值域最多为2e9,所以我们建立十颗线段树,保存每一位上的 如果当前位不为0,保存这个位置上所在数的最小值

记录一个保存答案的线段树,答案就为 当前位的两个数都不为0的两数之和的最小值

代码:

/**
*        ┏┓    ┏┓
*        ┏┛┗━━━━━━━┛┗━━━┓
*        ┃       ┃  
*        ┃   ━    ┃
*        ┃ >   < ┃
*        ┃       ┃
*        ┃... ⌒ ...  ┃
*        ┃       ┃
*        ┗━┓   ┏━┛
*          ┃   ┃ Code is far away from bug with the animal protecting          
*          ┃   ┃ 神兽保佑,代码无bug
*          ┃   ┃           
*          ┃   ┃       
*          ┃   ┃
*          ┃   ┃           
*          ┃   ┗━━━┓
*          ┃       ┣┓
*          ┃       ┏┛
*          ┗┓┓┏━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 佛祖保佑 永无BUG
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e5 + 5;
const int INF = 2e9+7;
const int mod = 1e9 + 7;
const double Pi = acos(-1);
LL gcd(LL a, LL b) {
return b ? gcd(b, a % b) : a;
}
LL lcm(LL a, LL b) {
return a / gcd(a, b) * b;
}
double dpow(double a, LL b) {
double ans = 1.0;
while(b) {
if(b % 2)ans = ans * a;
a = a * a;
b /= 2;
} return ans;
}
LL quick_pow(LL x, LL y) {
LL ans = 1;
while(y) {
if(y & 1) {
ans = ans * x % mod;
} x = x * x % mod;
y >>= 1;
} return ans;
}
LL a[maxn];
LL Min[maxn << 2][15];
LL ANS[maxn << 2];
void push_up(int rt) {
ANS[rt] = INF;
for(int i = 0; i <= 12; i++) {
if(Min[ls][i] != INF && Min[rs][i] != INF) {
ANS[rt] = min(ANS[rt], Min[ls][i] + Min[rs][i]);
}
Min[rt][i] = min(Min[ls][i], Min[rs][i]);
}
ANS[rt] = min(ANS[rt], min(ANS[ls], ANS[rs]));
}
void build(int l, int r, int rt) {
ANS[rt] = INF;
if(l == r) {
int tmp = a[l];
// debug1(tmp);
for(int i = 0; i <= 12; i++) {
int val = tmp % 10;
if(val == 0) Min[rt][i] = INF;
else Min[rt][i] = a[l];
tmp /= 10;
}
return;
}
int mid = (l + r) >> 1;
build(lson);
build(rson);
push_up(rt);
}
void update(int pos, int x, int l, int r, int rt) {
if(l == r) {
int tmp = x;
ANS[rt] = INF;
// debug1(tmp);
for(int i = 0; i <= 12; i++) {
int val = tmp % 10;
if(val == 0) Min[rt][i] = INF;
else Min[rt][i] = x;
// Min[rt][i] = val;
// debug2(val,Min[rt][i]);
tmp /= 10;
}
return;
}
int mid = (l + r) >> 1;
if(pos <= mid) update(pos, x, lson);
else update(pos, x, rson);
push_up(rt);
}
LL res[15];
LL ans = INF;
void query(int L, int R, LL &ans, int l, int r, int rt) {
// if(l > R || r < L) return;
if(L <= l && r <= R) {
for(int i = 0; i <= 12; i++) {
if(res[i] != INF && Min[rt][i] != INF) {
ans = min(ans, res[i] + Min[rt][i]);
}
}
for(int i = 0; i <= 10; i++) {
res[i] = min(res[i], Min[rt][i]);
}
ans = min(ANS[rt], ans);
return;
}
int mid = (l + r) >> 1;
if(L <= mid) query(L, R, ans, lson);
if(R > mid) query(L, R, ans, rson);
}
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
build(1, n, 1);
while(m--) {
int op, l, r, pos, x;
scanf("%d", &op);
if(op == 1) {
scanf("%d%d", &pos, &x);
update(pos, x, 1, n, 1);
} else {
scanf("%d%d", &l, &r);
ans = INF;
for(int i = 0; i <= 12; i++) res[i] = INF;
query(l, r, ans, 1, n, 1);
if(ans == INF) ans = -1;
printf("%lld\n", ans);
}
}
return 0;
}

codeforces 1217E E. Sum Queries? (线段树的更多相关文章

  1. [CodeForces - 678F] Lena and Queries 线段树维护凸包

    大致题意: 给出三种操作 1.往平面点集中添加一个点 2.删除第i次添加的点 3.给出一个q,询问平面点集中的q*x+y的最大值 首先对于每个询问,可将z=q*x+y转化为y=z-q*x,即过点(x, ...

  2. [Codeforces 280D]k-Maximum Subsequence Sum(线段树)

    [Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...

  3. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  4. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树

    题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test ...

  5. Codeforces 1117G Recursive Queries [线段树]

    Codeforces 洛谷:咕咕咕 思路 设\(L_i,R_i\)为\(i\)左右第一个大于它的位置. 对于每一个询问\(l,r\),考虑区间每一个位置的贡献就是\(\min(r,R_i-1)-\ma ...

  6. Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树, 位运算)

    链接: https://codeforces.com/contest/1234/problem/D 题意: You are given a string s consisting of lowerca ...

  7. codeforces 22E XOR on Segment 线段树

    题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...

  8. CodeForces - 369E Valera and Queries(树状数组)

    CodeForces - 369E Valera and Queries 题目大意:给出n个线段(线段的左端点和右端点坐标)和m个查询,每个查询有cnt个点,要求给出有多少条线段包含至少其中一个点. ...

  9. Codeforces 588E. A Simple Task (线段树+计数排序思想)

    题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...

随机推荐

  1. hdu2516 博弈

    找规律,发现时斐波那契数列:打表上. #include<stdio.h> #include<string.h> #define maxn 2147483647 __int64 ...

  2. CoreData遇见iCloud的那些坑

    尽管苹果把iCloud与CoreData之间的完美配合吹的天花乱坠,但在iOS7之前,想用iCloud同步CoreData数据简直就是噩梦,苹果自己也承认了之前的诸多bug和不稳定性,这让苹果不得不重 ...

  3. framework7 上拉加载一些ajax问题

    1.请求第一组数据后如果不能产生上拉进度条,则无法进行上拉加载. 解决办法:首次加载的数据量设置合理即可. 2.同一组数据请求多次,原因是异步刷新时间差,请求参数未更新,多次触发了上拉加载. 解决办法 ...

  4. c50决策树借款风险

    Decision Trees/ Machine Learning Durga Gaddam August 29, 2016 Objective: The objective of the articl ...

  5. C++大体概况 标签: c++总结 2015-01-31 20:41 792人阅读 评论(15) 收藏

    今年又一次报名了二级的C++考试,现在再来把C++总结一下,也不能算是总结,大体提炼了一下需要注意的地方,考试之前打算把这些东西好好看一看,今年一定要过啊! 前两天才知道,unix是用C语言编写的,这 ...

  6. Python基础:09函数式编程

    Python支持一些函数式编程的特性.比如lambda. map().reduce().filter()函数. 一:匿名函数与lambda Python可以用lambda 关键字创造匿名函数.匿名函数 ...

  7. 阿里云:面向5G时代的物联网无线连接服务

    在4月24日落幕的2019中国联通合作伙伴大会“5G+物联网(IoT)论坛”上,阿里云高级运营专家李茁出席圆桌对话,分享了5G时代物联网如何更好地推动行业完成生产.管理和商业模式的创新,阿里云又会以何 ...

  8. 2019-8-31-dotnet-通过-WMI-获取系统补丁

    title author date CreateTime categories dotnet 通过 WMI 获取系统补丁 lindexi 2019-08-31 16:55:59 +0800 2019- ...

  9. oracle trunc(d1[,c1])

    [功能]:返回日期d1所在期间(参数c1)的第一天日期 [参数]:d1日期型,c1为字符型(参数),c1默认为j(即当前日期) [参数表]:c1对应的参数表: 最近0点日期: 取消参数c1或j 最近的 ...

  10. ViewPager封装工具类: 轻松实现APP导航或APP中的广告栏

    相信做app应用开发的,绝对都接触过ViewPager,毕竟ViewPager的应用可以说无处不在:APP第一次启动时的新手导航页,APP中结合Fragment实现页面滑动,APP中常见的广告栏的自动 ...