CF1217E Sum Queries? (线段树)

完了,前几天才说 edu 的 DEF 都不会,现在打脸了吧 qwq

其实在刚说完这句话 1min 就会了 D,3min 就会了 E

发现,对于大小 \(\ge 3\) 的不平衡集合,它至少有一个大小为 \(2\) 的子集是不平衡的。

证明,发现对于大小为 \(2\) 的集合,平衡当且仅当两数的数位交为空(对于任意一位,至多一个数在这一位上不是 \(0\))。

反证一波,如果大集合没有大小为 \(2\) 的不平衡集合,那么任意两数的数位交都为空,那么大集合也是平衡的,矛盾了。

所以,只需要考虑大小为 \(2\) 的集合。

这个就能线段树简单做了,每个线段树维护 \(ans\) 表示这个区间中的答案,\(mn_i\) 表示这个区间中第 \(i\) 位有值的数的最小值。pushup 具体看代码。

时间复杂度 \(O((n+q\log n)\log a_i)\)。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=200020,pw[9]={1,10,100,1000,10000,100000,1000000,10000000,100000000};
#define MP make_pair
#define PB push_back
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline ll read(){
    char ch=getchar();ll x=0,f=0;
    while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
    while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
    return f?-x:x;
}
int n,m,a[maxn];
struct node{
    ll ans,mn[9];
    node operator+(const node &nd)const{
        node s;
        s.ans=min(ans,nd.ans);
        FOR(i,0,8) s.ans=min(s.ans,mn[i]+nd.mn[i]),s.mn[i]=min(mn[i],nd.mn[i]);
        return s;
    }
}seg[maxn*4];
inline int get(int x,int id){return x/pw[id]%10;}
void build(int o,int l,int r){
    if(l==r){
        seg[o].ans=1e18;
        FOR(i,0,8) seg[o].mn[i]=get(a[l],i)?a[l]:1e18;
        return;
    }
    int mid=(l+r)>>1;
    build(lson);build(rson);
    seg[o]=seg[o<<1]+seg[o<<1|1];
}
void update(int o,int l,int r,int p,int v){
    if(l==r){
        seg[o].ans=1e18;
        FOR(i,0,8) seg[o].mn[i]=get(v,i)?v:1e18;
        return;
    }
    int mid=(l+r)>>1;
    if(mid>=p) update(lson,p,v);
    else update(rson,p,v);
    seg[o]=seg[o<<1]+seg[o<<1|1];
}
node query(int o,int l,int r,int ql,int qr){
    if(l>=ql && r<=qr) return seg[o];
    int mid=(l+r)>>1;
    if(mid<ql) return query(rson,ql,qr);
    if(mid>=qr) return query(lson,ql,qr);
    return query(lson,ql,qr)+query(rson,ql,qr);
}
int main(){
    n=read();m=read();
    FOR(i,1,n) a[i]=read();
    build(1,1,n);
    while(m--){
        int op=read(),x=read(),y=read();
        if(op==1) update(1,1,n,x,y);
        else{
            ll ans=query(1,1,n,x,y).ans;
            printf("%lld\n",ans>=2e9?-1ll:ans);
        }
    }
}

原文地址:https://www.cnblogs.com/1000Suns/p/11515585.html

时间: 2024-08-28 09:48:23

CF1217E Sum Queries? (线段树)的相关文章

HDU4027 Can you answer these queries 线段树区间求和+剪枝

给了你n,然后n个数字在一个数组中,接下来m个询问,每个询问三个数字 t,x,y,若t==0,那么修改区间[x,y]的每一个值,变为原来每个位置上的数 开根号取整,若t==1,那么对区间[x,y]求和 由于n,m,很大,所以树状数组铁定超时,若直接用线段树来做区间修改,那么也是超时,这类题目没别的方法了,静心剪枝,发现题目给的数据范围为2^63,有没有发现,2^63开根号 绝对不需要开10次,就能到1,到1以后就不需要再开了,意思就是若有某个区间[x,y]每一个点的值都为1时,这一段区间事实上是

HDU 4027 Can you answer these queries? (线段树+区间点修改)

题意:给你 n 个数,m个询问(c,x,y) c==0 把x,y区间的值变为原来的平方根(向下取整) c==1 计算x,y区间的和. 利用1的开方永远为1剪枝.. #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> //#include<map> #include<cmath> #include<iostream> #include

HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)

题目 线段树 简单题意: 区间(单点?)更新,区间求和 更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都是开平方,同一个数更新有限次数就一直是1了,所以可以这样优化 #include <stdio.h> #include<math.h> #define N 100010 #define LL __int64 #define lson l,m,rt<<1 #define rson

hdu4027-Can you answer these queries?(线段树)

Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 8330    Accepted Submission(s): 1904 Problem Description A lot of battleships of evil are arranged in a line before

HDU 4027 Can you answer these queries? 线段树裸题

题意: 给定2个操作 0.把区间的每个数sqrt 2.求和 因为每个数的sqrt次数很少,所以直接更新到底,用个标记表示是否更新完全(即区间内的数字只有0,1就不用再更新了) #include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<cmath> #include<queue> #include<set> #incl

HDU 4027 Can you answer these queries?(线段树)

Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 9216    Accepted Submission(s): 2106 Problem Description A lot of battleships of evil are arranged in a line before

hdu 4027 Can you answer these queries? 线段树区间开根号,区间求和

Can you answer these queries? Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5195 Description A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapo

Can you answer these queries?(线段树之单点更新)

萌萌哒的传送门 因为一个long long 范围内的数开方不会超过10次,所以题目就转化为线段树的单点更新问题. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <set> #include <queue> #include <vector> #include <cstdlib> #includ

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

链接: https://codeforces.com/contest/1234/problem/D 题意: You are given a string s consisting of lowercase Latin letters and q queries for this string. Recall that the substring s[l;r] of the string s is the string slsl+1-sr. For example, the substrings