线段树

 1 #include<cstdio>
 2 const int maxn=100000+10;
 3 typedef long long ll;
 4 ll a[maxn],tree[4*maxn],lazy[4*maxn];
 5 void Build(int rt,int l,int r){
 6      if(l==r) {
 7         tree[rt]=a[l];
 8         return;
 9      }   
10      int mid=(l+r)>>1;
11      Build(rt<<1,l,mid);
12      Build(rt<<1|1,mid+1,r);
13      tree[rt]=tree[rt<<1]+tree[rt<<1|1];
14 }
15 void update(int rt,int l,int r,ll w){
16      tree[rt]+=(r-l+1)*w;
17      lazy[rt]+=w;
18 }
19 void pushdown(int rt,int l,int r){
20      int mid=(l+r)>>1;
21      update(rt<<1,l,mid,lazy[rt]);
22      update(rt<<1|1,mid+1,r,lazy[rt]);
23      lazy[rt]=0;
24 }
25 ll query(int rt,int l,int r,int x){
26     if(l==r) return tree[rt];
27     int mid=(l+r)>>1;
28     pushdown(rt,l,r);
29     if(x<=mid) return query(rt<<1,l,mid,x);
30     else return query(rt<<1|1,mid+1,r,x); 
31 }
32 void modify(int rt,int l,int r,int s,int t,ll w){
33      if(s<=l&&t>=r){
34           update(rt,l,r,w);
35           return ;
36      }
37      pushdown(rt,l,r);
38      int mid=(l+r)>>1;
39      if(s<=mid) modify(rt<<1,l,mid,s,t,w);
40      if(t>mid) modify(rt<<1|1,mid+1,r,s,t,w);
41      tree[rt]=tree[rt<<1]+tree[rt<<1|1];
42 }
43 int main(){
44     int n;
45     scanf("%d",&n);
46     for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
47     Build(1,1,n);
48     int m;
49     scanf("%d",&m);
50     for(int i=1;i<=m;i++){
51          char s[8];
52          scanf("%s",s);
53          if(s[0]=='Q'){
54             int x;
55             scanf("%d",&x);
56             ll a=query(1,1,n,x);
57             printf("%lld\n",a);
58          }
59          else {
60              int l,r;
61              ll w;
62              scanf("%d%d%lld",&l,&r,&w);
63              modify(1,1,n,l,r,w);
64          }
65     }
66    return 0;
67 }
View Code
 1 #include<cstdio>
 2 #define lson rt<<1
 3 #define rson rt<<1|1
 4 const int maxn=1e6+10;
 5 int a[maxn]={},tree[4*maxn]={};
 6 void Build(int rt,int l,int r){
 7     if(l==r){
 8         tree[rt]=a[l];
 9         return ;
10     }
11     int mid=(l+r)>>1;
12     Build(lson,l,mid);
13     Build(rson,mid+1,r);
14     tree[rt]=tree[lson]+tree[rson];
15 }
16 int query(int rt,int l,int r,int s,int t){
17     if(s<=l&&t>=r) return tree[rt];
18     int mid=(l+r)>>1;
19     if(t<=mid) return query(lson,l,mid,s,t);
20     else if(s>mid) return query(rson,mid+1,r,s,t);
21     else return query(lson,l,mid,s,t)+query(rson,mid+1,r,s,t);
22 }
23 void modify(int rt,int l,int r,int x,int y){
24     if(l==r){
25         tree[rt]+=y;
26         return;
27     }
28     int mid=(l+r)>>1;
29     if(x<=mid) modify(lson,l,mid,x,y);
30     else modify(rson,mid+1,r,x,y);
31     tree[rt]=tree[lson]+tree[rson];
32 } 
33 int main(){
34     int n,m;
35     scanf("%d",&n);
36     if(n!=0){
37         for(int i=1;i<=n;i++){
38             scanf("%d",&a[i]);
39         }
40         Build(1,1,n);
41         scanf("%d",&m);
42         char s[5];
43         int k,d,tot=0;
44         for(int i=1;i<=m;i++){
45             scanf("%s%d%d",s,&k,&d);
46             if(s[0]=='S'){
47                 printf("%d\n",query(1,1,n,k,d));
48             }
49             else modify(1,1,n,k,d);
50         }
51     }
52     else {
53         scanf("%d",&m);
54         char s[5];
55         int a,b;
56         for(int i=1;i<=m;i++){
57             scanf("%s%d%d",s,&a,&b);
58             if(s[0]=='S') printf("0\n");
59         }
60     }
61     return 0;
62 } 
View Code
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=1e6+10;
 6 int a[maxn],tree[maxn*4];
 7 void Build(int rt,int l,int r){
 8     if(l==r){
 9         tree[rt]=a[l];
10         return;
11     }
12     int mid=(l+r)>>1;
13     Build(rt<<1,l,mid);
14     Build(rt<<1|1,mid+1,r);
15     tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
16 }
17 int query(int rt,int l,int r,int k,int d){
18     if(k<=l&&d>=r) return tree[rt];
19     int mid=(l+r)>>1;
20     if(d<=mid) return query(rt<<1,l,mid,k,d);
21     else if(k>mid) return query(rt<<1|1,mid+1,r,k,d);
22     else return max(query(rt<<1,l,mid,k,d),query(rt<<1|1,mid+1,r,k,d));
23 }
24 int main(){
25     int n;
26     scanf("%d",&n);
27     for(int i=0;i<=n;i++) scanf("%d",&a[i]);
28     Build(1,1,n);
29     int q;
30     scanf("%d",&q);
31     int a,b;
32     for(int i=1;i<=q;i++){
33         scanf("%d%d",&a,&b);
34         int j=query(1,1,n,a,b);
35         printf("%d\n",j);      
36     }
37     return 0;
View Code

 

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 const int maxn=200000+10,mod=1000000007;
 5 int a[maxn],tree[4*maxn],b[maxn];
 6 int cnt[4*maxn];
 7 void Build(int rt,int l,int r){
 8      if(l==b[1]&&r==b[1]){
 9          tree[rt]+=b[1];
10          cnt[rt]++;
11          return ;
12      }
13      int mid=(l+r)>>1;
14      if(b[1]<=mid) Build(rt<<1,l,mid);
15      else Build(rt<<1|1,mid+1,r);
16      tree[rt]=tree[rt<<1]+tree[rt<<1|1];
17      cnt[rt]=cnt[rt<<1]+cnt[rt<<1|1];
18 }
19 void Modify(int rt,int l,int r,int x,int w){
20      if(l==x&&r==x){
21         tree[rt]=w;
22         cnt[rt]++;
23         return ;
24      }
25      int mid=(l+r)>>1;
26      if(x<=mid) Modify(rt<<1,l,mid,x,w);
27      else Modify(rt<<1|1,mid+1,r,x,w);
28      tree[rt]=tree[rt<<1]+tree[rt<<1|1];
29      cnt[rt]=cnt[rt<<1]+cnt[rt<<1|1];
30 }
31 int query(int rt,int l,int r,int s,int t){
32      if(s<=l&&t>=r){
33          return tree[rt];
34      }
35      int mid=(l+r)>>1;
36      if(t<=mid) return query(rt<<1,l,mid,s,t);
37      else if(s>mid) return query(rt<<1|1,mid+1,r,s,t);
38      else return query(rt<<1,l,mid,s,t)+query(rt<<1|1,mid+1,r,s,t);
39 }
40 int main(){
41     int n,l,x,r,e;
42     scanf("%d%d%d%d%d",&n,&l,&x,&r,&e);
43     a[0]=x%l;
44     b[1]=a[0];
45     Build(1,1,l);  
46     int sum=b[1];
47     int q=1;
48     for(int i=1;i<=n-1;i++){
49        a[i]=(a[i-1]*r+e)%l;
50        b[i+1]=a[i];
51        Modify(1,1,l,b[i+1],b[i+1]);
52        int Min=query(1,1,l,1,b[i]);
53        int Max=sum-Min;
54        int ans;
55        ans=cnt[1]*b[i+1]-Min+Max-(i-cnt[1])*b[i+1];
56        q=(q*ans)%mod;
57        sum+=a[i];
58     }
59      printf("%d\n",q);
60     return 0;
61 }
View Code

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=5e5+10,maxm=1e6+10;
 6 typedef long long ll;
 7 ll a[maxn],c[maxn];
 8 int main(){
 9     int n,m;
10     scanf("%d%d",&n,&m);
11     ll Max=0;
12     for(int i=1;i<=n;i++) {
13          scanf("%lld",&a[i]);
14          c[i]=a[i];     
15     }
16     for(int i=1;i<=m;i++){
17        ll d,bb;
18        scanf("%lld%lld",&d,&bb);
19        int ans=0;
20        for(int j=1;j<=n;j++){
21           if(c[j]>bb){
22              ans+=(c[j]-bb);
23              c[j]=bb+a[j];
24            }
25            else{
26               c[j]+=a[j];
27            }   
28        }
29        printf("\n%d",ans);
30     }
31    return 0;
32 }
View Code

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int inf=0x3f3f3f3f;
 6 int d[11][11],w[11][11][35],f[1000+10][11];
 7 int main(){
 8     int n,k;
 9     while(scanf("%d%d",&n,&k)&&(n+k)){
10        scanf("%d%d",&n,&k);
11        memset(f,0x3f,sizeof(f));
12        for(int i=1;i<=n;i++){
13           for(int j=1;j<=n;j++){
14              if(i==j) continue;
15              scanf("%d",&d[i][j]);
16              for(int m=1;m<=d[i][j];m++){
17                  scanf("%d",&w[i][j][m]);
18              }
19           }
20        }
21        f[0][1]=0;
22        for(int i=1;i<=k;i++){
23            for(int j=1;j<=n;j++){
24               for(int m=1;m<=n;m++){
25                  int s=i;//%(d[m][j]);
26                  if(i!=d[m][j]&&w[m][j][m]==0||j==m) continue;
27                  if(i!=d[m][j])f[i][j]=min(f[i][j],f[i-1][m]+w[m][j][s]);          
28                  if(i==d[m][j]) f[i][j]=min(f[i][j],f[i-1][m]+w[m][j][i]);
29               }
30            }
31         }
32         if(f[k][n]==inf) printf("0\n");
33         else printf("%d\n",f[k][n]);
34     }
35     return 0;
36 }
View Code

 

posted @ 2020-07-01 17:16  ddoodd  阅读(137)  评论(0编辑  收藏  举报