1. 程式人生 > >noip_最後一遍_3-資料結構

noip_最後一遍_3-資料結構

noip基礎資料結構太多了又太撈了 所以也就那麼幾個了

單調佇列滑動視窗

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define maxn 1000005
 4 struct node{int id,val;};
 5 int n,m,k,l,a,b,c,mx[maxn],ma[maxn];
 6 deque<node>q1;deque<node>q2;
 7 int main(){cin>>n>>m;
 8     for(int i=1;i<=n;i++){cin>>a;
9 while(!q1.empty()&&q1.back().id<i-m+1)q1.pop_back(); 10 while(!q2.empty()&&q2.back().id<i-m+1)q2.pop_back(); 11 while(!q1.empty()&&q1.front().val<a)q1.pop_front(); 12 q1.push_front({i,a}); 13 while(!q2.empty()&&q2.front().val>a)q2.pop_front();
14 q2.push_front({i,a}); 15 if(i>=k)mx[i]=q1.back().val,ma[i]=q2.back().val; 16 }for(int i=m;i<=n;i++)cout<<ma[i]<<" ";cout<<endl; 17 for(int i=m;i<=n;i++)cout<<mx[i]<<" "; 18 }
View Code

線段樹1

 1 #include<bits/stdc++.h>
 2
using namespace std; 3 #define ll long long 4 #define maxn 100005 5 ll n,m,k,l,a,b,c,d,tree[maxn*4],lazy[maxn*4],s[maxn]; 6 #define lson (node<<1) 7 #define rson (lson|1) 8 void build(int node,int l,int r){ 9 if(l==r){tree[node]=s[l];return ;}int mid=(l+r)>>1; 10 build(lson,l,mid);build(rson,mid+1,r);tree[node]=tree[lson]+tree[rson]; 11 } 12 void pushup(int node,int l,int r){if(lazy[node]==0){return ;} 13 int mid=(l+r)>>1; 14 tree[lson]+=lazy[node]*(mid-l+1);tree[rson]+=lazy[node]*(r-mid); 15 lazy[lson]+=lazy[node],lazy[rson]+=lazy[node];lazy[node]=0; 16 } 17 void add(int node,int l,int r,int x,int y,int k){ 18 if(l==x&&y==r){tree[node]+=k*(r-l+1);lazy[node]+=k;return ;} 19 int mid=(l+r)>>1;pushup(node,l,r); 20 if(y<=mid)add(lson,l,mid,x,y,k);else if(x>=mid+1)add(rson,mid+1,r,x,y,k); 21 else add(lson,l,mid,x,mid,k),add(rson,mid+1,r,mid+1,y,k); 22 tree[node]=tree[lson]+tree[rson]; 23 } 24 ll query(int node,int l,int r,int x,int y){ 25 if(l==x&&y==r){return tree[node];} 26 int mid=(l+r)>>1;pushup(node,l,r); 27 if(y<=mid)return query(lson,l,mid,x,y);else if(x>=mid+1)return query(rson,mid+1,r,x,y); 28 else return query(lson,l,mid,x,mid)+query(rson,mid+1,r,mid+1,y); 29 } 30 int main(){ 31 cin>>n>>m;for(int i=1;i<=n;i++)cin>>s[i];build(1,1,n); 32 for(int i=1;i<=m;i++){ 33 cin>>a;if(a==1){ 34 cin>>a>>b>>c;add(1,1,n,a,b,c); 35 }else{ 36 cin>>a>>b;cout<<query(1,1,n,a,b)<<endl; 37 } 38 } 39 }
View Code

線段樹2

 1 #include<iostream>
 2 #include<cstdio>
 3 #define ll long long
 4 #define lson (node<<1)
 5 #define rson ((node<<1)+1)
 6 using namespace std;
 7 const int maxn=100010;
 8 int n,m;
 9 ll tree[maxn*4],lazy_product[maxn*4];
10 ll lazy_sum[maxn*4];
11 ll s[maxn],p;
12 void build(int node,int l,int r){lazy_product[node]=1;
13     if(l==r){tree[node]=s[l];return;}
14     int mid=(l+r)>>1;
15     build(lson,l,mid);build(rson,mid+1,r);
16     tree[node]=tree[lson]+tree[rson];
17 }
18 
19 void pushdown(int node,int l,int r,int mid){    
20     tree[lson]*=lazy_product[node];tree[rson]*=lazy_product[node];
21     tree[lson]+=(mid-l+1)*lazy_sum[node];tree[rson]+=(r-mid)*lazy_sum[node];
22     tree[lson]%=p;tree[rson]%=p;
23     lazy_product[lson]*=lazy_product[node];lazy_product[rson]*=lazy_product[node];
24     lazy_sum[lson]*=lazy_product[node];lazy_sum[rson]*=lazy_product[node];
25     lazy_sum[lson]+=lazy_sum[node];lazy_sum[rson]+=lazy_sum[node];
26     lazy_sum[lson]%=p;lazy_sum[rson]%=p;
27     lazy_product[lson]%=p;lazy_product[rson]%=p;
28     lazy_sum[node]=0;lazy_product[node]=1;
29 }
30 void add(int node,int l,int r,int x,int y,int val){
31     if(x==l&&r==y){tree[node]+=(r-l+1)*val,tree[node]%=p,lazy_sum[node]+=val;return;}
32     int mid=(l+r)>>1;pushdown(node,l,r,mid);
33     if(y<=mid)add(lson,l,mid,x,y,val);
34     else if(x>=mid+1)add(rson,mid+1,r,x,y,val);
35     else{add(lson,l,mid,x,mid,val);add(rson,mid+1,r,mid+1,y,val);}
36     tree[node]=tree[lson]+tree[rson];tree[node]%=p;
37 }
38 
39 void product(int node,int l,int r,int x,int y,int val){
40     if(x==l&&r==y){tree[node]*=val;tree[node]%=p;lazy_product[node]*=val;lazy_product[node]%=p;
41         lazy_sum[node]*=val;lazy_sum[node]%=p;return;
42     }int mid=(l+r)>>1;pushdown(node,l,r,mid);
43     if(y<=mid)product(lson,l,mid,x,y,val);
44     else if(x>=mid+1)product(rson,mid+1,r,x,y,val);
45     else product(lson,l,mid,x,mid,val),product(rson,mid+1,r,mid+1,y,val);
46     tree[node]=tree[lson]+tree[rson];tree[node]%=p;
47 }
48 
49 ll find(int node,int l,int r,int x,int y){
50     if(x==l&&r==y)return tree[node]%p;
51     int mid=(l+r)>>1;pushdown(node,l,r,mid);
52     if(y<=mid)return find(lson,l,mid,x,y);
53     if(x>=mid+1)return find(rson,mid+1,r,x,y);
54     return (find(lson,l,mid,x,mid)+find(rson,mid+1,r,mid+1,y))%p;
55 }
56     
57 int main(){cin>>n>>m>>p;
58     for(int i=1;i<=n;i++)scanf("%d",&s[i]);
59     build(1,1,n);
60     for(int i=1;i<=m;i++){int a;cin>>a;
61         if(a==1){int x,y,k;
62             scanf("%d%d%d",&x,&y,&k);
63             product(1,1,n,x,y,k);
64         }if(a==2){int x,y,k;
65             scanf("%d%d%d",&x,&y,&k);
66             add(1,1,n,x,y,k);
67         }if(a==3){int x,y;
68             scanf("%d%d",&x,&y);
69             cout<<find(1,1,n,x,y)%p<<endl;
70         }
71     }return 0;
72 }
View Code