zoukankan      html  css  js  c++  java
  • 线段树练习

    写了几道题,对线段树的概念有了一定的认识,这里总结下我写的习惯,方便以后完善及复习。

    线段树所用的函数:

    1. pushup():向上更新父节点
    2. build(): 与普通建树方式相同,最后要pushup()(向上更新(父节点的值)
    3. update():判断当前区间与给定区间的关系;若是点:找到点更新即可,若是区间:更新到rt即可,只有用到rt子节点,才向下更新。递归向下结束后,在向上的过程中更新父节点。
    4. query(): 判断当前rt与给定区间的关系,若用到lazy操作,查询时用到rt子节点则向下更新,递归过程中查询所需的内容
    5. putdown():配合lazy操作,向下更新结点。这里需要明白一段区间 [l, r]的 左儿子[l, (l+r)/2],右儿子[(l+r)/2+1, r],由此知左右子区间长度与父节点的区间长度的关系。(1)、关于pushdown()的部分更新感想见G题。

    因为初学线段树,理解有限,这个总结我想一直完善下去。 

    A - 敌兵布阵

    单点更新,查询区间和。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <algorithm>
     5 using namespace std;
     6 #define LL long long
     7 #define lson l,m,rt<<1
     8 #define rson m+1,r,rt<<1|1
     9 #define midf(a,b) (((a) + (b))>>1)
    10 const int maxnode = 50010;
    11 struct Node{
    12     int value;
    13     int left, right;
    14 }node[maxnode<<2];
    15 void pushup(int rt){
    16     node[rt].value = node[rt<<1].value+node[rt<<1|1].value;
    17 }
    18 void build(int l ,int r, int rt){
    19     node[rt].left=l, node[rt].right=r, node[rt].value=0;
    20     if(l == r){
    21         scanf("%d", &node[rt].value);
    22         return;
    23     }
    24     int m = midf(l, r);
    25     build(lson);
    26     build(rson);
    27     pushup(rt);
    28 }
    29 //单点修改 
    30 void update(int a, int rt, int ans){
    31     if(node[rt].left==a && node[rt].right==a){
    32         node[rt].value += ans;
    33         return;
    34     }else{
    35         int m = midf(node[rt].left, node[rt].right);
    36         if(a <= m) update(a,rt<<1,ans);
    37         if(a > m)  update(a,rt<<1|1, ans);
    38         pushup(rt);  //向下找叶子结点军营的过程中,在每层需向上更新 
    39     }
    40 }
    41 //区间查询 
    42 LL query(int l, int r, int rt){
    43     if(node[rt].left>=l && node[rt].right<=r){
    44         return node[rt].value;
    45     }
    46     LL ans = 0;
    47     int m = midf(node[rt].left, node[rt].right);
    48     if(l <= m) ans += query(l,r,rt<<1);
    49     if(r > m)  ans += query(l,r,rt<<1|1);  //找右儿子结点 
    50     return ans;
    51 }
    52 int main(){
    53 //    freopen("in.txt", "r", stdin);
    54     int tt;
    55     scanf("%d",&tt);
    56     for(int ttt=1; ttt<=tt; ttt++){
    57         printf("Case %d:
    ", ttt);
    58         int n;
    59         scanf("%d", &n);
    60         build(1, n, 1);
    61         char ask[10];
    62         while(scanf("%s", ask) != EOF){
    63             if(ask[0] == 'E'){
    64                 break;
    65             }else if(ask[0] == 'A'){
    66                 int l, c;
    67                 scanf("%d%d", &l, &c);
    68                 update(l, 1, c);
    69             }else if(ask[0] == 'S'){
    70                 int l, c;
    71                 scanf("%d%d", &l, &c);
    72                 update(l, 1, -c);                
    73             }else{
    74                 int l, r;
    75                 scanf("%d%d", &l, &r);
    76                 printf("%lld
    ", query(l, r, 1));                
    77             }
    78         }
    79     }
    80     return 0;
    81 }
    View Code

      

    B - I Hate It

    单点更新,查询区间最大值

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <algorithm>
     5 using namespace std;
     6 #define LL long long
     7 #define lson l,m,rt<<1
     8 #define rson m+1,r,rt<<1|1
     9 #define midf(a,b) (((a) + (b))>>1)
    10 const int maxnode = 200010;
    11 struct Node{
    12     int value;
    13     int left, right;
    14 }node[maxnode<<2];
    15 void pushup(int rt){
    16     node[rt].value = max(node[rt<<1].value, node[rt<<1|1].value);
    17 }
    18 void build(int l, int r, int rt){
    19     node[rt].left=l, node[rt].right=r, node[rt].value=0;
    20     if(l == r){
    21         scanf("%d", &node[rt].value);
    22         return;
    23     }
    24     int m = midf(l, r);
    25     build(lson); build(rson);
    26     pushup(rt);
    27 }
    28 void update(int a, int rt, int b){
    29     if(node[rt].left==a && node[rt].right==a){
    30         node[rt].value = b;
    31         return;
    32     }else{
    33         int m = midf(node[rt].left, node[rt].right);
    34         if(a <= m)  update(a, rt<<1, b);
    35         if(a > m)  update(a, rt<<1|1, b);
    36         pushup(rt);
    37     }
    38 }
    39 int query(int l, int r, int rt){
    40     if(node[rt].left>=l && node[rt].right<=r){
    41         //这里注意,容易写错 
    42         return node[rt].value;
    43     }
    44     int ans = 0;
    45     int m = midf(node[rt].left, node[rt].right);
    46     if(l <= m) ans = max(ans, query(l, r, rt<<1));
    47     if(r > m)  ans = max(ans, query(l, r, rt<<1|1));
    48     return ans;
    49 }
    50 int main(){
    51 //    freopen("in.txt", "r", stdin);
    52     int n, m;
    53     while(scanf("%d%d", &n, &m) != EOF){
    54         build(1, n, 1);
    55         for(int i=0; i<m; i++){
    56             char ch[2];
    57             scanf("%s", ch);
    58             if(ch[0] == 'Q'){
    59                 int l, r;
    60                 scanf("%d%d", &l, &r);
    61                 printf("%d
    ", query(l, r, 1));
    62             }else{
    63                 int a, d;
    64                 scanf("%d%d", &a, &d);
    65                 update(a, 1, d);
    66             }
    67         }
    68     }
    69     return 0;
    70 }
    View Code

    C - RMQ with Shifts

    题意:shift(S):对序列S做一次(left “circular shift”)左移循环; query():查询区间最小值。

    这里比较难想的是:关于shift()这里如何用线段树操作:按题意循环单点更新。这里需要提前保留第一个的值。

    故此题变为:单点更新+查区间最小值

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 const int maxnode = 1e5+10;
     6 const int inf = 0x3f3f3f3f;
     7 #define lson l,m,rt<<1
     8 #define rson m+1,r,rt<<1|1
     9 #define midf(a,b) (((a)+(b))>>1)
    10 struct Node{
    11     int left, right;
    12     int value;
    13 }node[maxnode<<2];
    14 //这里开空间要注意 
    15 void pushup(int rt){
    16     node[rt].value = min(node[rt<<1].value, node[rt<<1|1].value);
    17 }
    18 void build(int l, int r, int rt){
    19     node[rt].left=l, node[rt].right=r, node[rt].value=0;
    20     if(l == r){
    21         scanf("%d", &node[rt].value);
    22         return;
    23     }
    24     int m = midf(l, r);
    25     build(lson); build(rson);
    26     pushup(rt);
    27 }
    28 int query(int l, int r, int rt){
    29     if(node[rt].left>=l && node[rt].right<=r){
    30         return node[rt].value;
    31     }
    32     int ans = inf, m = midf(node[rt].left, node[rt].right);
    33     if(l<=m)  ans = min(ans, query(l, r, rt<<1));
    34     if(r>m)   ans = min(ans, query(l, r, rt<<1|1));
    35     return ans;
    36 }
    37 void update(int a, int rt, int b){
    38     if(node[rt].left==a && node[rt].right==a){
    39         node[rt].value = b;
    40         return;
    41     }
    42     int m = midf(node[rt].left, node[rt].right);
    43     if(a <= m) update(a, rt<<1, b);
    44     else  update(a, rt<<1|1, b);
    45     pushup(rt);
    46 }
    47 int query_node(int a, int rt){
    48     if(node[rt].left==a && node[rt].right==a){
    49         return node[rt].value;
    50     }
    51     int m = midf(node[rt].left, node[rt].right);
    52     if(a<=m) return query_node(a, rt<<1);
    53     else  return query_node(a, rt<<1|1);
    54 }
    55 int main(){
    56 //    freopen("in.txt", "r", stdin);
    57     int n, q;
    58     scanf("%d%d", &n, &q);
    59     build(1, n, 1);
    60     for(int qq=0; qq<q; qq++){
    61         char str[40];
    62         scanf("%s", str);
    63         if(str[0]=='q'){
    64             int a=0, b=0, i;
    65             for(i=6; str[i]!=','; i++){
    66                 a = a*10 + (str[i]-'0');
    67             }
    68             for(i=i+1; str[i]!=')'; i++){
    69                 b = b*10 + (str[i]-'0');
    70             }
    71             printf("%d
    ", query(a, b, 1));
    72         }else{
    73             int ch[40], len=0;
    74             memset(ch, 0, sizeof(ch));
    75             for(int i=6; str[i]!=')'; i++){
    76                 if(str[i]==','){
    77                     len++;
    78                 }
    79                 else{
    80                     ch[len] = ch[len]*10 + (str[i]-'0');
    81                 }
    82             }
    83             /*
    84             cout << "ch[]:   "; 
    85             for(int i=0; i<=len; i++){
    86                 cout << ch[i] << "   ";
    87             }cout << endl;
    88             */
    89             int tmp = query_node(ch[0], 1);
    90             for(int i=1; i<=len; i++){
    91                 update(ch[i-1], 1, query_node(ch[i], 1));
    92             }
    93             update(ch[len], 1, tmp);            
    94         }
    95     }
    96     return 0;
    97 }
    View Code

    D - A Simple Problem with Integers

    区间内每个值均加相同值更新,查询区间求和

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 const int maxnode = 1e5+10;
     6 const int inf = 0x3f3f3f3f;
     7 #define LL long long
     8 #define lson l,m,rt<<1
     9 #define rson m+1,r,rt<<1|1
    10 #define midf(a,b) (((a)+(b))>>1)
    11 struct Node{
    12     int left, right;
    13     LL sum, add;
    14 }node[maxnode<<2];
    15 
    16 void pushup(int rt){
    17     node[rt].sum = node[rt<<1].sum + node[rt<<1|1].sum;
    18 }
    19 void pushdown(int rt, int m){
    20     if(node[rt].add){
    21         node[rt<<1].add += node[rt].add;
    22         node[rt<<1|1].add += node[rt].add;
    23         node[rt<<1].sum += node[rt].add * (m - (m>>1));
    24         node[rt<<1|1].sum += node[rt].add * (m>>1);
    25         node[rt].add = 0;
    26     }
    27 }
    28 void build(int l, int r, int rt){
    29     node[rt].left=l, node[rt].right=r, node[rt].add=0;
    30     if(l == r){
    31         scanf("%lld", &node[rt].sum);
    32         return;
    33     }
    34     int m = midf(l, r);
    35     build(lson);
    36     build(rson);
    37     pushup(rt);
    38 }
    39 /*
    40  * [l,r]内的每个数均加c, rt是根节点 
    41 */ 
    42 void update(LL c, int l, int r, int rt){
    43     if(l<=node[rt].left && node[rt].right<=r){
    44         node[rt].add += c;
    45         //add表示[l,r]的累加数,标记未下传 
    46         node[rt].sum += (LL)(node[rt].right-node[rt].left+1)*c;
    47         //自己更新,并未更新子节点 
    48         return;
    49     }
    50     pushdown(rt, node[rt].right-node[rt].left+1);
    51     //update要用到rt的子节点 
    52     int m = midf(node[rt].left, node[rt].right);
    53     if(l <= m) update(c, l, r, rt<<1);
    54     if(m < r)  update(c, l, r, rt<<1|1);
    55     pushup(rt);
    56 }
    57 LL query(int l, int r, int rt){
    58     if(l<=node[rt].left && node[rt].right<=r){
    59         return node[rt].sum;
    60     }
    61     pushdown(rt, node[rt].right-node[rt].left+1);
    62     LL ans = 0;
    63     int m = midf(node[rt].left, node[rt].right);
    64     if(l <= m) ans+=query(l, r, rt<<1);
    65     if(m < r)  ans+=query(l, r, rt<<1|1);
    66     return ans;
    67 }
    68 int main(){
    69 //    freopen("in.txt", "r", stdin);
    70     int n,q;
    71     while(scanf("%d%d", &n, &q) != EOF){
    72         build(1, n, 1);
    73         while(q--){
    74             char str[2]; 
    75             scanf("%s", str);
    76             int a, b, c;
    77             if(str[0] == 'C'){
    78                 scanf("%d%d%d", &a, &b, &c);
    79                 update(c, a, b, 1);
    80             }else{
    81                 scanf("%d%d", &a, &b);
    82                 printf("%lld
    ", query(a, b, 1));
    83             }
    84         }
    85     }
    86     return 0;
    87 }
    View Code

    E - Just a Hook

    这题开始一直没有看到 You may consider the original hook is made up of cupreous sticks. 这句话,有点懵。

    按要求为区间赋值最后求区间和。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 const int maxnode = 1e5+10;
     6 const int inf = 0x3f3f3f3f;
     7 #define LL long long
     8 #define lson l,m,rt<<1
     9 #define rson m+1,r,rt<<1|1
    10 #define midf(a,b) (((a)+(b))>>1)
    11 struct Node{
    12     int left, right;
    13     LL sum, add;
    14 }node[maxnode<<2];
    15 void pushup(int rt){
    16     node[rt].sum = node[rt<<1].sum + node[rt<<1|1].sum;
    17 }
    18 void pushdown(int rt, int m){
    19     if(node[rt].add){
    20         node[rt<<1].add = node[rt].add;
    21         node[rt<<1|1].add = node[rt].add;
    22         node[rt<<1].sum = node[rt].add * (m - (m>>1));
    23         node[rt<<1|1].sum = node[rt].add * (m>>1);
    24         node[rt].add = 0;
    25     }
    26 }
    27 void build(int l, int r, int rt){
    28     node[rt].left=l, node[rt].right=r, node[rt].add=0;
    29     if(l == r){
    30         node[rt].sum = 1;
    31         return;
    32     }
    33     int m = midf(l ,r);
    34     build(lson);
    35     build(rson);
    36 }
    37 void update(LL c, int l, int r, int rt){
    38     if(l <= node[rt].left && node[rt].right<=r){
    39         node[rt].add = c;
    40         node[rt].sum = (LL)(node[rt].right - node[rt].left + 1) * c;
    41         return;
    42     }
    43     pushdown(rt, node[rt].right-node[rt].left+1);
    44     int m = midf(node[rt].left, node[rt].right);
    45     if(l <= m) update(c, l, r, rt<<1);
    46     if(m < r)  update(c, l, r, rt<<1|1);
    47     pushup(rt);
    48 }
    49 LL query(int l, int r, int rt){
    50     if(l <= node[rt].left && node[rt].right <= r){
    51         return node[rt].sum;
    52     }
    53     pushdown(rt, node[rt].right-node[rt].left+1);
    54     LL ans = 0;
    55     int m = midf(node[rt].right, node[rt].left);
    56     if(l <= m)  ans+=query(l, r, rt<<1);
    57     if(m < r)   ans+=query(l, r, rt<<1|1);
    58     return ans;
    59 }
    60 int main(){
    61 //    freopen("in.txt", "r", stdin);
    62     int tt;
    63     scanf("%d", &tt);
    64     for(int ttt=1; ttt<=tt; ttt++){
    65         int n;  scanf("%d", &n);
    66         build(1, n, 1);
    67         int q;  scanf("%d", &q);
    68         while(q--){
    69             int l, r, a;
    70             scanf("%d%d%d", &l, &r, &a);
    71             update(a, l, r, 1);
    72         }
    73         printf("Case %d: The total value of the hook is %d.
    ",ttt,query(1, n, 1));
    74     }
    75     return 0;
    76 }
    View Code

    F - Count Color

    题意:长度为L 的木棒,给T种颜色,两种操作:C:区间赋值  P:询问区间内颜色个数。

    颜色这里由范围可知用状态压缩或运算处理。这里pushdown()操作向下更新子节点与前面题目不同的是这里是直接区间赋值,因为sum代表的是颜色。

    其余的就是细心点,传递更新rt根节点信息时认真点。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 const int maxnode = 1e5+10;
     6 const int inf = 0x3f3f3f3f;
     7 #define LL long long
     8 #define lson l,m,rt<<1
     9 #define rson m+1,r,rt<<1|1
    10 #define midf(a,b) (((a)+(b))>>1)
    11 struct Node{
    12     int left, right;
    13     int sum, add;
    14 }node[maxnode<<2];
    15 void pushup(int rt){
    16     node[rt].sum = node[rt<<1].sum | node[rt<<1|1].sum;
    17 }
    18 void pushdown(int rt){
    19     if(node[rt].left == node[rt].right)return;
    20     if(node[rt].add){
    21         node[rt<<1].add = node[rt].add;
    22         node[rt<<1|1].add = node[rt].add;
    23         node[rt<<1].sum = node[rt].add;
    24         node[rt<<1|1].sum = node[rt].add;
    25         node[rt].add = 0;
    26     }
    27 }
    28 void build(int l, int r, int rt){
    29     node[rt].left=l, node[rt].right=r, node[rt].add=0;
    30     if(l == r){
    31         node[rt].sum=1;
    32         return;
    33     }
    34     int m = midf(l, r);
    35     build(lson);
    36     build(rson);
    37     pushup(rt);
    38 }
    39 void update(int c, int l ,int r, int rt){
    40     if(l<=node[rt].left && node[rt].right<=r){
    41         node[rt].sum = node[rt].add = c;
    42         return;
    43     }
    44     pushdown(rt);
    45     int m = midf(node[rt].left, node[rt].right);
    46     if(l <= m)  update(c,l,r, rt<<1);
    47     if(m < r)   update(c,l,r, rt<<1|1);
    48     pushup(rt);
    49 }
    50 int query(int l, int r, int rt){
    51     if(l<=node[rt].left && node[rt].right<=r){
    52         return node[rt].sum;
    53     }
    54     pushdown(rt);
    55     int m = midf(node[rt].left, node[rt].right);
    56     int ans = 0;
    57     if(l <= m) ans |= query(l,r, rt<<1);
    58     if(m < r)  ans |= query(l,r, rt<<1|1);
    59     return  ans;
    60 }
    61 int get_num(int c){
    62     int tmp = 0;
    63     while(c){
    64         if(c & 1) tmp++;
    65         c >>= 1;
    66     }
    67     return tmp;
    68 }
    69 int main(){
    70 //    freopen("in.txt", "r", stdin);
    71     int L, T, O;
    72     while(scanf("%d%d%d", &L, &T, &O) != EOF){
    73         build(1, L, 1);
    74         while(O--){
    75             char str[2];
    76             scanf("%s", str);
    77             if(str[0] == 'C'){
    78                 int l, r, c;
    79                 scanf("%d%d%d", &l, &r, &c);
    80                 if(l>r)swap(l, r);
    81                 update(1<<(c-1), l, r, 1);
    82             }else{
    83                 int l, r;
    84                 scanf("%d%d", &l, &r);
    85                 if(l>r)swap(l, r);
    86                 printf("%d
    ", get_num(query(l, r, 1)));
    87             }
    88         }
    89     }
    90     return 0;
    91 }
    View Code

     G - Count the Colors(zoj1610)

    题意:在[0,8000]的区间里染色[l, r],询问这个区间每种颜色所占的连续(颜色相同视为连续)区间的个数。

    这道题看似与上道题目相似,可是坑点太多,写了很久,总是过不了样例,学了一些时间,也感觉收获很多。

    这里先总结下我没考虑到的问题和思维上出错的地方:

    1、上道题目染色给出的(l, r)代表的是从区间l区间r,而这道题目中染色(l, r)是代表从点l染色到点r,这里思考错,就gg了。

           这个问题如何处理:其实不难,从原来维护点到现在维护单位区间,就将这个单位区间视为点。在code时什么时候更新(或查询或build)到点还是到区 间,需要注意。到点(l==r),到区间(l+1 == r)。

    2、如何进行查询?这里写法难易与更新update()的写法有有关。先思考查询我们可以按遍历顺序进行判断前一个区间的颜色与当前区间的颜色是否相同来决定是否计数颜色,这种顺序可以视为先序遍历(这个思路感觉很神奇,反正我没想到)。然后是如何计数的问题,这里就需要对根节点做标记了,这个在3中讨论。

    3、如何更新、计数?这里更新一定要更新到点才停止,查询是到区间才停止。如果更新到区间,会漏掉一种情况有可能就是当前这个区间,也有可能是如(l, m, rt<<1)和(m+1, r, rt<<1|1),这种会漏掉(m, m+1)这个单位区间。至于2中更新时如何对根节点进行标记,这里借助pushdown(),因为lazy操作后,需要用到子节点时,要将信息传递下去,同时标记根节点,可以在这个时候进行标记。我认为这是染色的精髓,当更新区间颜色时,导致左右子区间颜色不同时,进行标记,这其实是对区间的标记,代表其在计数时需要向下遍历。标记-2:左右子区间颜色不同。标记-1:这段区间未染色或者存在未染色的子区间。

    上面的心得可能因表述能力写的有点乱。这道题目对现在的我来说真的是道很好的题,最然没有独立AC,但是学到了一些东西。

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 using namespace std;
      5 const int maxnode = 8010;
      6 const int inf = 0x3f3f3f3f;
      7 #define LL long long
      8 #define lson l,m,rt<<1
      9 #define rson m,r,rt<<1|1
     10 #define midf(a,b) (((a)+(b))>>1)
     11 struct     Node{
     12     int left, right;
     13     int add;
     14 }node[maxnode<<2];
     15 void build(int l, int r, int rt){
     16     node[rt].left=l, node[rt].right=r, node[rt].add=-1;
     17     if(l+1 == r){
     18         return;
     19     }
     20     int m = midf(l, r);
     21     build(lson);
     22     build(rson);
     23     //rson = m,r,rt<<1|1
     24     //因为这里是区间的概念,要保证连续性 
     25 }
     26 void pushdown(int rt){
     27     if(node[rt].add >= 0){
     28         node[rt<<1|1].add = node[rt<<1].add = node[rt].add;
     29         node[rt].add = -2;
     30     }
     31 }
     32 void update(int c, int l, int r, int rt){
     33     if((l == r) || (node[rt].add == c)){
     34         return;
     35     }
     36     /*
     37      * 这里剪枝掉两种情况:
     38      *  1、l==r: 这不是一个区间, 是一个点, 无法染色
     39      *  2、node[rt].add==c: rt点所在的大区间颜色相同。 
     40     */
     41     if(l <= node[rt].left && node[rt].right <= r){
     42         node[rt].add = c;
     43         return;
     44     }
     45     pushdown(rt);
     46     //这里是染色的精髓,当更新区间颜色时,导致rt的左右子区间
     47     //颜色不同时,则标记node[rt].add=-2,代表其子区间颜色不同 
     48     //心得:pushdown()在向下更新的同时,其实也可以对根节点做标记
     49     // 这种标记既可以作为信息已经下传的作用(清除lazy标记的意思吧)
     50     // 也可以对根进行特殊标记,这种标记的对象其实是区间。 
     51     int m = midf(node[rt].left, node[rt].right);
     52     if(r <= m) update(c,l,r, rt<<1);
     53     else if(l >= m) update(c, l, r, rt<<1|1);
     54     else{
     55         update(c,lson);
     56         update(c,rson);
     57     }
     58     /*
     59     if(l <= m) update(c,l,r, rt<<1);
     60     if(m < r) update(c,l,r, rt<<1|1);
     61     这里会漏掉一种情况(m, m+1)这个区间
     62     而上面的else里:rson-->m,r,rt<<1|1
     63         并非平常的  rson-->m+1,r,rt<<1|1 
     64     */
     65 }
     66 int ans[maxnode], color;
     67 void query(int rt){
     68     if(node[rt].add >= 0){
     69         //有区间,这段区内颜色相同 
     70         if(node[rt].add != color){  //与前面区间的颜色比较 
     71             ans[node[rt].add]++;
     72             color = node[rt].add;    
     73         }
     74         return; 
     75     }
     76     if(node[rt].left+1 != node[rt].right){ 
     77         //不是一个单位区间时(视单位区间为点) 
     78         query(rt<<1);
     79         query(rt<<1|1);
     80     }else{
     81         // 这个线段没有颜色 或这是一个点 
     82         color = -1;
     83     }
     84 }
     85 int main(){
     86 //    freopen("in.txt", "r", stdin);
     87     int n;
     88     while( scanf("%d", &n) != EOF){
     89         build(0, 8000, 1);
     90         //cout << "debug
    ";
     91         for(int i=0; i<n; i++){
     92             int a, b, c;
     93             scanf("%d%d%d", &a, &b, &c);
     94             if(a >= b) continue;
     95             update(c, a, b, 1);
     96             //这里a+1则将单位线段视作一个点 
     97         }
     98         color = -1; memset(ans, 0, sizeof(ans));
     99         query(1);
    100         for(int i=0; i<maxnode; i++){
    101             if(ans[i])
    102                 printf("%d %d
    ", i, ans[i]);
    103         }
    104         putchar('
    ');
    105     }
    106     return 0;
    107 }
    View Code

    上面几题内容有  区间加 区间乘 区间赋值 区间替换(赋值)  询问区间和


    H - Can you answer these queries?

    区间内所有数开根号 + 查询区间和。

    因为是所有数,所以这里不用lazy,反而简单。有个剪枝的操作:单个数开根号一定会收敛到1,更新区间时,区间内的值都为1,则剪枝。

    这题比上题好写的多,就是题目在l,r设置上有点坑,有l>r的数据。

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <cmath>
     5 using namespace std;
     6 #define LL long long
     7 #define midf(l,r) ((l+r)>>1)
     8 #define lson l,m,rt<<1
     9 #define rson m+1,r,rt<<1|1
    10 const int maxnode = 1e5+10;
    11 struct Node{
    12     int left, right;
    13     LL sum;
    14 }node[maxnode<<2];
    15 void pushup(int rt){
    16     node[rt].sum = node[rt<<1].sum + node[rt<<1|1].sum;
    17 }
    18 void build(int l, int r, int rt){
    19     node[rt].left=l, node[rt].right=r;
    20     if(l == r){
    21         scanf("%lld", &node[rt].sum);
    22         return;
    23     }
    24     int m = midf(l, r);
    25     build(lson);  build(rson);
    26     pushup(rt);
    27 }
    28 void update(int l, int r, int rt){
    29     if(node[rt].left == node[rt].right){
    30         node[rt].sum = sqrt(1.0*node[rt].sum);
    31         return;
    32     }
    33     if(l<=node[rt].left && node[rt].right<=r && node[rt].sum==(node[rt].right-node[rt].left+1)){
    34         return;
    35     }
    36     int m = midf(node[rt].left, node[rt].right);
    37     if(l <= m) update(l,r, rt<<1);
    38     if(m < r)  update(l,r, rt<<1|1);
    39     pushup(rt);
    40 }
    41 LL query(int l, int r, int rt){
    42     if(l <= node[rt].left && node[rt].right<=r){
    43         return node[rt].sum;
    44     }
    45     LL ans = 0;
    46     int m = midf(node[rt].left, node[rt].right);
    47     if(l <= m) ans+=query(l,r, rt<<1);
    48     if(m < r)  ans+=query(l,r, rt<<1|1);
    49     return ans;
    50 }
    51 int main(){
    52 //    freopen("in.txt", "r", stdin);
    53     int n, qq=1;
    54     while(scanf("%d", &n) != EOF){
    55         printf("Case #%d:
    ", qq++);
    56         build(1, n, 1);
    57         int tt;  scanf("%d", &tt);
    58         for(int ttt=1; ttt<=tt; ttt++){
    59             int op, l, r;
    60             scanf("%d%d%d", &op, &l, &r);
    61             if(l>r)swap(l,r);
    62             if(op){
    63                 printf("%lld
    ", query(l, r, 1));
    64             }else{
    65                 update(l, r, 1);
    66             }
    67         }
    68         putchar('
    ');
    69     }
    70     return 0;
    71 }
    View Code

    积累一个小知识-------约数个数定理。

    1、打表:

     1 /*
     2  * 约数个数定理 求解约数个数 
     3 */ 
     4 #include <bits/stdc++.h>
     5 using namespace std;
     6 const int maxn = 1e6+5;
     7 int prim[maxn], vis[maxn], tot;
     8 int d[maxn], cnt[maxn], n;
     9 void euler(){
    10     d[1]=1,  cnt[1]=0, tot=0;
    11     for(int i=2; i<=n; i++){
    12         if(!vis[i]){
    13             prim[++tot] = i;
    14             d[i] = 2;
    15             cnt[i] = 1;
    16         }
    17         for(int j=1; j<=tot&&prim[j]<=n/i; j++){
    18             vis[i*prim[j]] = 1;
    19             if(i % prim[j] == 0){
    20                 cnt[i * prim[j]] = cnt[i] + 1;
    21                 d[i*prim[j]] = d[i]/(cnt[i]+1)*(cnt[i*prim[j]]+1);
    22                 break;
    23             }
    24             cnt[i*prim[j]] = 1;
    25             d[i * prim[j]] = d[i]*2;
    26         }
    27     }
    28 }
    29 int main(){
    30     cin >> n;
    31     euler();
    32     int ans =0;
    33     for(int i=1; i<=n; i++) ans+=d[i];
    34     cout << ans << endl;
    35     return 0;
    36 }
    View Code

     2、非打表

     1 /*
     2  * 约数个数定理 求解约数个数
     3  * 非打表直接求 
     4 */ 
     5 #include <bits/stdc++.h>
     6 using namespace std;
     7 const int maxn = 1e6+5;
     8 int prim[maxn], vis[maxn], tot;
     9 int d[maxn], cnt[maxn];
    10 int fac_num(int n){
    11     int ans = 1;
    12     for(int i=2; i*i<=n; i++){
    13         int k = 0;
    14         while(n % i == 0){
    15             n /= i;
    16             k++;
    17         }
    18         if(k)  ans*=(k+1);
    19     }
    20     if(n != 1) ans*=2;
    21     if(ans == 1){
    22         if(n == 1) return 1;
    23         else return 2;
    24     }
    25     return ans;
    26 }
    27 int main(){
    28     freopen("in.txt", "r", stdin);
    29     int m; cin >> m;
    30     cout << fac_num(m) << endl;
    31     return 0;
    32 }
    View Code

     I - SUM and REPLACE

     (区间更新)所有数变为约数的个数 + (查询)区间求和

    设置一个标记max,代表区间内的最大值(不是用来查询),是用来剪枝(约数个数收敛)。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 1e6+5;
     4 int prim[maxn], vis[maxn], tot;
     5 int d[maxn], cnt[maxn];
     6 void euler(){
     7     d[1]=1,  cnt[1]=0, tot=0;
     8     for(int i=2; i<=maxn; i++){
     9         if(!vis[i]){
    10             prim[++tot] = i;
    11             d[i] = 2;
    12             cnt[i] = 1;
    13         }
    14         for(int j=1; j<=tot&&prim[j]<=maxn/i; j++){
    15             vis[i*prim[j]] = 1;
    16             if(i % prim[j] == 0){
    17                 cnt[i * prim[j]] = cnt[i] + 1;
    18                 d[i*prim[j]] = d[i]/(cnt[i]+1)*(cnt[i*prim[j]]+1);
    19                 break;
    20             }
    21             cnt[i*prim[j]] = 1;
    22             d[i * prim[j]] = d[i]*2;
    23         }
    24     }
    25 }
    26 #define LL long long
    27 #define lson l,m,rt<<1
    28 #define rson m+1,r,rt<<1|1
    29 const int maxnode = 3e5+10;
    30 int n, m;
    31 struct     Node{
    32     LL sum, maxval;
    33 }node[maxnode<<2];
    34 void pushup(int rt){
    35     node[rt].maxval = max(node[rt<<1].maxval, node[rt<<1|1].maxval);
    36     node[rt].sum = node[rt<<1].sum + node[rt<<1|1].sum;
    37 }
    38 void build(int l, int r, int rt){
    39     if(l == r){
    40         scanf("%d", &node[rt].sum);
    41         node[rt].maxval = node[rt].sum;
    42         return;
    43     }
    44     int m = (l+r)>>1;
    45     build(lson);  build(rson);
    46     pushup(rt); 
    47 }
    48 void update(int L, int R, int l, int r, int rt){
    49     if(node[rt].maxval <= 2){
    50         return;
    51     }    
    52     if(l == r){  //更新区间内的所有点 
    53         node[rt].maxval = node[rt].sum = d[node[rt].maxval];
    54         return;
    55     }
    56     int m = (l+r)>>1;
    57     if(L<=m) update(L, R, lson);
    58     if(m<R) update(L,R, rson);
    59     pushup(rt);
    60 }
    61 LL query(int L, int R, int l, int r, int rt){
    62     if(L<=l && r<=R){
    63         return node[rt].sum;
    64     }
    65     LL ans=0;  int m=(l+r)>>1;
    66     if(L<=m) ans+=query(L,R, lson);
    67     if(m<R) ans+=query(L,R, rson);
    68     return ans;
    69 }
    70 int main(){
    71     euler();
    72 //    freopen("in.txt", "r", stdin);
    73     scanf("%d%d", &n, &m);
    74     build(1, n, 1);
    75     for(int i=0; i<m; i++){
    76         int op, l, r;
    77         scanf("%d%d%d", &op, &l, &r);
    78         if(op == 1){
    79             update(l,r, 1, n, 1);
    80         }else{
    81             printf("%lld
    ", query(l,r, 1, n, 1));
    82         }
    83     }
    84     return 0;
    85 }
    View Code

    上面几题内容有:区间加,区间除(下取整),询问区间和。


    还有3个部分待学。


    hdu6447 YJJ's Salesman

    这个dp很好想,离散化也想到了,但我太菜,从来没写过离散化,大佬队友也因为一点点小问题场上一直没解决,所以这题跪了。

    dp方程很好想 dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]+w[i][j]) 更新的时候从上到下,从右到左(与01背包相似),找到离散化后列的最大值。

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <vector>
     5 using namespace std;
     6 #define lson l,m,rt<<1
     7 #define rson m+1,r,rt<<1|1
     8 const int maxn = 1e5+10;
     9 struct Tree{
    10     int sum;
    11 }tr[maxn<<2];
    12 struct Node{
    13     int x, y, w;
    14     bool operator < (const Node &cp) const {
    15         return (x == cp.x) ? y > cp.y : x < cp.x;
    16     }
    17     //x从小到达,y从大到小 
    18 }node[maxn];
    19 //单点修改+区间查询最大值 
    20 void pushup(int rt){
    21     tr[rt].sum = max(tr[rt<<1].sum, tr[rt<<1|1].sum);
    22 }
    23 void build(int l, int r, int rt){
    24     if(l == r){
    25         tr[rt].sum = 0;
    26         return;
    27     }
    28     int m = (l+r)>>1; 
    29     build(lson); build(rson);
    30     pushup(rt);
    31 }
    32 void update(int c, int pos, int l, int r, int rt){
    33     if(l == r){
    34         tr[rt].sum = c;
    35         return;
    36     }
    37     int m = (l+r)>>1;
    38     if(pos <= m) update(c, pos, lson);
    39     else       update(c, pos, rson);
    40     pushup(rt);
    41 }
    42 int query(int L, int R, int l, int r, int rt){
    43     if(L <= l && R >= r){
    44         return tr[rt].sum;
    45     }
    46     int m = (l+r)>>1, ans=0;
    47     if(L <= m) ans = max(ans, query(L, R, lson));
    48     if(m < R)  ans = max(ans, query(L, R, rson));
    49     return ans;
    50 }
    51 vector<int> v;
    52 int main(){
    53     freopen("in.txt", "r", stdin);
    54     int t; scanf("%d", &t);
    55     while(t--){
    56         int n;scanf("%d", &n);
    57         for(int i=1; i<=n; i++){
    58             scanf("%d%d%d", &node[i].x, &node[i].y, &node[i].w);
    59             v.push_back(node[i].y);
    60         }
    61         sort(v.begin(), v.end());
    62         sort(node+1, node+n+1);
    63         v.erase(unique(v.begin(), v.end()), v.end());
    64         int cnt = v.size();
    65         build(0, cnt, 1);
    66         for(int i=1; i<=n; i++){
    67             int y = lower_bound(v.begin(), v.end(), node[i].y) - v.begin();
    68             int ans = query(0, y, 0, cnt, 1) + node[i].w;
    69             update(ans, y+1, 0, cnt, 1);
    70         }
    71         printf("%d
    ", query(0, cnt, 0, cnt, 1));
    72     }
    73     return 0;
    74 }
    View Code
  • 相关阅读:
    【转】Windows Phone的应用程序认证要求
    ObservableCollection删除问题
    国庆总结?
    .net dll破解实战
    理理头绪
    创建Metro风格的WPF界面
    Alpha项目测试
    原型设计
    团队项目总结
    最常用的35中心里效应
  • 原文地址:https://www.cnblogs.com/seaupnice/p/9515312.html
Copyright © 2011-2022 走看看