zoukankan      html  css  js  c++  java
  • 狂K 线段树

    想写好树剖~~线段树very important

    HDU 1166 敌兵布阵

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 83545    Accepted Submission(s): 35301

    Problem Description
    C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。
    中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.
    Input
    第一行一个整数T,表示有T组数据。
    每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。
    接下来每行有一条命令,命令有4种形式:
    (1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
    (2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
    (3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
    (4)End 表示结束,这条命令在每组数据最后出现;
    每组数据最多有40000条命令
    Output
    对第i组数据,首先输出“Case i:”和回车,
    对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。
    Sample Input
    1 10 1 2 3 4 5 6 7 8 9 10 Query 1 3 Add 3 6 Query 2 7 Sub 10 2 Add 6 3 Query 3 10 End
    Sample Output
    Case 1: 6 33 59
    Author
    Windbreaker
     
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 using namespace std;
     5 #define maxn 50010
     6 struct Tree{
     7     int l,r,sum;
     8 }tre[maxn * 4+10];
     9 int T,n;
    10 void Built(int l,int r,int pos){
    11     tre[pos].l=l;tre[pos].r=r;
    12     if(l==r){ scanf("%d",&tre[pos].sum);return; }
    13     int mid=(l+r)/2;
    14     Built(l,mid,pos*2);
    15     Built(mid+1,r,pos*2+1);
    16     tre[pos].sum=tre[pos<<1].sum+tre[pos<<1|1].sum;
    17 }
    18 int Query(int l,int r,int pos){
    19     int ans=0;
    20     if(l<=tre[pos].l&&tre[pos].r<=r)return tre[pos].sum;
    21     int mid=(tre[pos].l+tre[pos].r)/2;
    22     if(mid>=l)ans+=Query(l,r,pos<<1);
    23     if(mid<r)ans+=Query(l,r,pos<<1|1);
    24     tre[pos].sum=tre[pos<<1].sum+tre[pos<<1|1].sum;
    25     return ans;
    26 }
    27 void Add(int pos,int num,int now){
    28     if(tre[now].l==tre[now].r){ tre[now].sum+=num;return ;}
    29     int mid=(tre[now].l+tre[now].r)>>1;
    30     if(pos<=mid)Add(pos,num,now<<1);
    31     if(pos>mid)Add(pos,num,now<<1|1);
    32     tre[now].sum=tre[now<<1].sum+tre[now<<1|1].sum;
    33 }
    34 int main()
    35 {
    36     cin>>T;
    37     int k=1,x,y;
    38     char str[50];
    39     while(T--){
    40         memset(tre,0,sizeof(tre));
    41         scanf("%d",&n);
    42         printf("Case %d:
    ",k++);
    43         Built(1,n,1);
    44         while(1){
    45             scanf("%s",str);
    46             if(strcmp(str,"End")==0) break;
    47             scanf("%d%d",&x,&y);
    48             if(strcmp(str,"Query")==0){
    49                 printf("%d
    ",Query(x,y,1));
    50             }
    51             if(strcmp(str,"Add")==0){// zeng jia
    52                 Add(x,y,1);
    53                }
    54             if(strcmp(str,"Sub")==0){//jian shao
    55                 Add(x,-y,1);
    56             }
    57         }
    58     }
    59     return 0;
    60 }

    1698  Just a Hook

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 30143    Accepted Submission(s): 14884

    Problem Description
    In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.



    Now Pudge wants to do some operations on the hook.

    Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
    The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

    For each cupreous stick, the value is 1.
    For each silver stick, the value is 2.
    For each golden stick, the value is 3.

    Pudge wants to know the total value of the hook after performing the operations.
    You may consider the original hook is made up of cupreous sticks.
    Input
    The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
    For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
    Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
    Output
    For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
    Sample Input
    1 10 2 1 5 2 5 9 3
    Sample Output
    Case 1: The total value of the hook is 24.
    Source
    Recommend
    wangye   |   We have carefully selected several similar problems for you:  1754 1542 1394 2795 1255 
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 using namespace std;
     5 #define N 100000
     6 struct Node{
     7     int l,r,data,sum;
     8 }tre[N*4];
     9 int n,m,x1,x2,x3,t; 
    10 void Built(int now,int l,int r){
    11     tre[now].l=l;tre[now].r=r;
    12     if(l==r)tre[now].sum=tre[now].data=1;
    13     else{
    14         int mid=(l+r)>>1;
    15         Built(now<<1,l,mid);Built(now<<1|1,mid+1,r);
    16         tre[now].data=0;
    17         tre[now].sum=tre[now<<1].sum+tre[now<<1|1].sum;
    18     }
    19 }
    20 void UpData(int now,int l,int r,int val){
    21     if(tre[now].l==l&&tre[now].r==r){
    22         tre[now].data=val;
    23         tre[now].sum=(r-l+1)*val;
    24     }
    25     else{
    26         if(tre[now].data>0){
    27             tre[now<<1].data=tre[now].data;
    28             tre[now<<1].sum=(tre[now<<1].r-tre[now<<1].l+1)*tre[now<<1].data;
    29             
    30             tre[now<<1|1].data=tre[now].data;
    31             tre[now<<1|1].sum=(tre[now<<1|1].r-tre[now<<1|1].l+1)*tre[now<<1|1].data;
    32         }
    33         tre[now].data=0;
    34         int mid=(tre[now].l+tre[now].r)>>1;
    35         if(r<=mid) UpData(now<<1,l,r,val);
    36         else if(l>mid) UpData(now<<1|1,l,r,val);
    37             else{
    38                 UpData(now<<1,l,mid,val);
    39                 UpData(now<<1|1,mid+1,r,val);
    40             }
    41             
    42         tre[now].sum=tre[now<<1].sum+tre[now<<1|1].sum;
    43     }
    44 }
    45 int main(){
    46     scanf("%d",&t);
    47     for(int i=1;i<=t;i++){
    48         scanf("%d",&n);
    49         Built(1,1,n);
    50         scanf("%d",&m);
    51         while(m--){
    52             scanf("%d%d%d",&x1,&x2,&x3);
    53             UpData(1,x1,x2,x3);
    54         }
    55         printf("Case %d: The total value of the hook is %d.
    ",i,tre[1].sum);  
    56     }
    57     return 0;
    58 }

    1082 线段树练习 3

    时间限制: 3 s    空间限制: 128000 KB    题目等级 : 大师 Master

    题目描述 Description

    给你N个数,有两种操作:

    1:给区间[a,b]的所有数增加X

    2:询问区间[a,b]的数的和。

    输入描述 Input Description

    第一行一个正整数n,接下来n行n个整数,

    再接下来一个正整数Q,每行表示操作的个数,

    如果第一个数是1,后接3个正整数,

    表示在区间[a,b]内每个数增加X,如果是2,

    表示操作2询问区间[a,b]的和是多少。

    pascal选手请不要使用readln读入

    输出描述 Output Description

    对于每个询问输出一行一个答案

    样例输入 Sample Input

    3

    1

    2

    3

    2

    1 2 3 2

    2 2 3

    样例输出 Sample Output

    9

    数据范围及提示 Data Size & Hint

    数据范围

    1<=n<=200000

    1<=q<=200000

     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 int n,m,type;
     5 struct node{
     6     int l,r;long long sum,flag;
     7 }tre[800001];
     8 long long read(){
     9     long long x=0,f=1;char c=getchar();
    10     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    11     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    12     return x*f;
    13 }
    14 void build(int now,int l,int r){
    15     tre[now].l=l;tre[now].r=r;
    16     if(l==r){ tre[now].sum=read();return; }
    17     int mid=(l+r)>>1;
    18     build(now<<1,l,mid);
    19     build(now<<1|1,mid+1,r);
    20     tre[now].sum=tre[now<<1].sum+tre[now<<1|1].sum;
    21 }
    22 void tree_flag(int now){    
    23     if(tre[now].r==tre[now].l) return;
    24     tre[now<<1].flag+=tre[now].flag;
    25     tre[now<<1|1].flag+=tre[now].flag;
    26     tre[now<<1].sum+=tre[now].flag*(tre[now<<1].r-tre[now<<1].l+1);
    27     tre[now<<1|1].sum+=tre[now].flag*(tre[now<<1|1].r-tre[now<<1|1].l+1);
    28     tre[now].flag=0;
    29 }
    30 void UpData(int now,int l,int r,int k){
    31     if(tre[now].l==l&&tre[now].r==r){
    32         tre[now].sum+=(r-l+1)*k;
    33         tre[now].flag+=k;
    34         return;
    35     }
    36     if(tre[now].flag) tree_flag(now);
    37     int mid=(tre[now].l+tre[now].r)>>1;
    38     if(l>mid) UpData(now<<1|1,l,r,k);
    39     else if(r<=mid) UpData(now<<1,l,r,k);
    40     else{
    41         UpData(now<<1,l,mid,k);
    42         UpData(now<<1|1,mid+1,r,k);
    43     }
    44     tre[now].sum=tre[now<<1].sum+tre[now<<1|1].sum;
    45 }
    46 long long int Query(int now,int l,int r){
    47     if(tre[now].l==l&&tre[now].r==r){
    48         return tre[now].sum;
    49     }
    50     if(tre[now].flag)tree_flag(now);
    51     tre[now].sum=tre[now<<1].sum+tre[now<<1|1].sum;
    52     int mid=(tre[now].l+tre[now].r)>>1;
    53     if(l>mid)return Query(now<<1|1,l,r);
    54     else if(r<=mid)return Query(now<<1,l,r);
    55     else{
    56         return Query(now<<1,l,mid)
    57         +Query(now<<1|1,mid+1,r);
    58     }
    59 }
    60 int main(){
    61     n=read();
    62     build(1,1,(int)n);
    63     m=read();
    64     long long int l,r,x;
    65     for(int i=1;i<=m;i++){
    66         type=read();
    67         if(type==1){
    68             l=read(),r=read(),x=read();
    69             UpData(1,(int)l,(int)r,(int)x);
    70         }
    71         else{
    72             l=read(),r=read();
    73             printf("%lld
    ",Query(1,(int)l,(int)r));
    74         }
    75     }
    76     return 0;
    77 }
  • 相关阅读:
    一个布局里面引用另一个布局的控件
    字符串的操作
    代码中使用bitmap资源并加载到控件上
    ui主线程控件的更新就让这个activity的异步任务做完整
    java语法:字符串数组的赋值
    文件路径的格式
    Android布局及属性归总(查询用)
    代码里语句的顺序狠重要。。。
    社交里互评、点赞的实现
    小功能——类似微信里,评论内容里面,点击每个人的用户名进入个人主页
  • 原文地址:https://www.cnblogs.com/suishiguang/p/6505822.html
Copyright © 2011-2022 走看看