Educational Codeforces Round 72 (Rated for Div. 2)E. Sum Queries?(线段树区间合并)

https://codeforc.es/contest/1217/problem/E

建立9棵数位线段树维护区间最小值和次小值,建议用struct建树方便进行区间合并

 1 #define bug(x) cout<<#x<<" is "<<x<<endl
 2 #define IO std::ios::sync_with_stdio(0)
 3 #include <bits/stdc++.h>
 4 #define iter ::iterator
 5 #define pa pair<int,int>
 6 using namespace  std;
 7 #define ll long long
 8 #define mk make_pair
 9 #define pb push_back
10 #define se second
11 #define fi first
12 #define ls o<<1
13 #define rs o<<1|1
14 ll mod=998244353;
15 const int N=2e5+5,h=2e9+5;
16 int n,q;
17 struct node{
18     int mi[10];
19     int mmi[10];
20     node operator +(const node &t)const{
21         node tmp;
22         for(int i=0;i<9;i++){
23             tmp.mi[i]=min(mi[i],t.mi[i]);
24
25             if(mi[i]<t.mi[i]){
26                 tmp.mmi[i]=min(mmi[i],t.mi[i]);
27             }
28             else{
29                 tmp.mmi[i]=min(mi[i],t.mmi[i]);
30             }
31         }
32         return tmp;
33     }
34 }a[N*4];
35 void up(int o,int l,int r,int k,int v){
36     if(l==r){
37         for(int i=0;i<9;i++){
38             a[o].mi[i]=a[o].mmi[i]=h;
39         }
40         int t=v;
41         for(int i=0;i<9;i++){
42             int x=t%10;
43             if(x){
44                 a[o].mi[i]=min(a[o].mi[i],v);
45             }
46             t/=10;
47         }
48         return;
49     }
50     int m=(l+r)/2;
51     if(k<=m)up(ls,l,m,k,v);
52     else up(rs,m+1,r,k,v);
53     a[o]=a[ls]+a[rs];
54 }
55 node qu(int o,int l,int r,int ql,int qr){
56     if(l>=ql&&r<=qr){
57         return a[o];
58     }
59     int m=(l+r)/2;
60     node res;
61     for(int i=0;i<9;i++){
62         res.mi[i]=res.mmi[i]=h;
63     }
64     if(ql<=m)res=res+qu(ls,l,m,ql,qr);
65     if(qr>m)res=res+qu(rs,m+1,r,ql,qr);
66     return res;
67 }
68 int main(){
69     scanf("%d%d",&n,&q);
70     for(int i=1;i<=n;i++){
71         int x;
72         scanf("%d",&x);
73         up(1,1,n,i,x);
74     }
75     while(q--){
76         int t,x,y;
77         scanf("%d%d%d",&t,&x,&y);
78         if(t==1)up(1,1,n,x,y);
79         else{
80             node res=qu(1,1,n,x,y);
81             ll ans=h;
82             for(int i=0;i<9;i++){
83                 ans=min(ans,1ll*res.mi[i]+res.mmi[i]);
84             }
85             if(ans==h)ans=-1;
86             printf("%d\n",ans);
87         }
88     }
89 }

原文地址:https://www.cnblogs.com/ccsu-kid/p/11488573.html

时间: 2025-01-09 04:40:07

Educational Codeforces Round 72 (Rated for Div. 2)E. Sum Queries?(线段树区间合并)的相关文章

Educational Codeforces Round 47 (Rated for Div. 2)F. Dominant Indices 线段树合并

题意:有一棵树,对于每个点求子树中离他深度最多的深度是多少, 题解:线段树合并快如闪电,每个节点开一个权值线段树,递归时合并即可,然后维护区间最多的是哪个权值,到x的深度就是到根的深度减去x到根的深度复杂度O(nlogn) //#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast,no-stack-protector") //#pragma GCC target("

Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序

Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] ? 给你一个有向图,给用最少的颜色给每条边染色,要保证不存在一个环中的所有边都是同一个颜色. [Solution] ? 用拓扑排序判断图中是否存在环,若图中不存在环,则所有边都是同一种颜色.否则,同一个环中,只要用两种颜色就可以满足题目条件,所以总的颜色数就是两种,对于一个环,一定会存在两种边:1.节点号小

Educational Codeforces Round 72 (Rated for Div. 2)

又垫底了. 和上一场一样,因为 D 题写错了一个小地方,把时间全部耗在上面了. (还不是水平问题,要是能想到 D 题的最优解法,也就不存在这种问题了. A. Creating a Character \[ str + x > int + epx - x \] \[ 2x>int+epx-str \] #include<bits/stdc++.h> #define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne,

Educational Codeforces Round 72 (Rated for Div. 2)E(线段树,思维)

#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;#define BUF_SIZE 100000bool IOerror=0;//加了读入挂才1900ms+卡ddl过的,不加读入代码tleT_Tinline char nc(){ static char buf[BUF_SIZE], *p1=buf+BUF_SIZE, *pend=buf+BUF_SIZE; if(p1==pend){ p1=buf; p

Educational Codeforces Round 44 (Rated for Div. 2)+E. Pencils and Boxes+树状数组

题目链接:E. Pencils and Boxes 题意:N 个数,要求分成任意堆,要求每一堆只要有K个,同一堆中任意数之间差值不能超过d; 题解:用树状数组.排一下序然后从后面开始找,以当前数为最小值看能否成堆,要成堆的话要求i+k,到第一个大于a[i]+d的位置之间有能够成堆的位置.(这里判断的时候树状数组一减看是否大于0就可以了)注意初始化时在n+1的位置加1. 1 #include<bits/stdc++.h> 2 #include <iostream> 3 #includ

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.