• 2019 ICPC Asia Yinchuan Regional G. Pot!!(线段树 区间更新 区间查询)


     G. Pot!!

    思路:区间维护线段树即可,由题意可知我们只需要区间维护cnt[2, 3, 5, 7]个数的最大值即可。

      1 #include <cstdio>
      2 #include <iostream>
      3 #include <cstdio>
      4 #include <algorithm>
      5 #include <functional>
      6 #include <set>
      7 #include <vector>
      8 #include <queue>
      9 #include <cstring>
     10 #include <stack>
     11 #include <climits>
     12  
     13 using namespace std;
     14  
     15 #define ll long long
     16 #define pb push_back
     17 #define fi first
     18 #define se second
     19 
     20 struct node{
     21     int l, r;
     22     int cnt[10];
     23     int lz[10];
     24     //int number;
     25     int mid(){ return (l + r) >> 1; }
     26 };
     27 
     28 vector<node > tree;
     29 vector<int > inx;
     30 
     31 void fun(int x, vector<int >& vi){
     32     if(x == 2) vi.pb(2);
     33     else if(x == 3) vi.pb(3);
     34     else if(x == 4) vi.pb(2), vi.pb(2);
     35     else if(x == 5) vi.pb(5);
     36     else if(x == 6) vi.pb(2), vi.pb(3);
     37     else if(x == 7) vi.pb(7);
     38     else if(x == 8) vi.pb(2), vi.pb(2), vi.pb(2);
     39     else if(x == 9) vi.pb(3), vi.pb(3);
     40     else if(x == 10) vi.pb(2), vi.pb(5);
     41 }
     42 
     43 void build_tree(int rt, int l, int r){
     44     tree[rt].l = l;
     45     tree[rt].r = r;
     46     if(l == r){
     47         //cout << l << endl;
     48         //tree[rt].number = l;
     49         return;
     50     }
     51     int m = tree[rt].mid();
     52     build_tree(rt << 1, l, m);
     53     build_tree(rt << 1 | 1, m + 1, r);
     54 }
     55 
     56 void Push_down(int rt){
     57     int lson = rt << 1;
     58     int rson = rt << 1 | 1;
     59     for(auto x : inx){
     60         if(tree[rt].cnt[x])
     61             tree[lson].cnt[x] += tree[rt].lz[x];
     62             tree[rson].cnt[x] += tree[rt].lz[x];
     63             tree[lson].lz[x] += tree[rt].lz[x];
     64             tree[rson].lz[x] += tree[rt].lz[x];
     65             tree[rt].lz[x] = 0;
     66     }
     67 }
     68 
     69 void Push_up(int rt){
     70     int lson = rt << 1;
     71     int rson = rt << 1 | 1;
     72     for(auto x : inx){
     73         tree[rt].cnt[x] = max(tree[lson].cnt[x], tree[rson].cnt[x]);
     74     }
     75 }
     76 
     77 void update(int rt, int l, int r, vector<int >& vi){
     78     if(tree[rt].l == l && tree[rt].r == r){
     79         //printf("update [%d. %d]
    ", l, r);
     80         for(auto x : vi){
     81             ++tree[rt].cnt[x];
     82             ++tree[rt].lz[x];
     83         }
     84         return;
     85     }
     86     Push_down(rt);
     87     int m = tree[rt].mid();
     88     if(m >= r) update(rt << 1, l, r, vi);
     89     else if(m < l) update(rt << 1 | 1, l, r, vi);
     90     else{
     91         update(rt << 1, l, m, vi);
     92         update(rt << 1 | 1, m + 1, r, vi);
     93     }
     94     Push_up(rt);
     95 }
     96 
     97 void query(int rt, int l, int r, int& res){
     98     if(tree[rt].l == l && tree[rt].r == r){
     99         for(auto x : inx){
    100             res = max(res, tree[rt].cnt[x]);
    101         }
    102         return;
    103     }
    104     Push_down(rt);
    105     int m = tree[rt].mid();
    106     if(m >= r) query(rt << 1, l, r, res);
    107     else if(m < l) query(rt << 1 | 1, l, r, res);
    108     else{
    109         query(rt << 1, l, m, res);
    110         query(rt << 1 | 1, m + 1, r, res);
    111     }
    112 }
    113 
    114 // void show(int rt, int l, int r){
    115 //     //cout << "show" << endl;
    116 //     //if(l == r) {
    117 //         for(auto i : inx)
    118 //             if(tree[rt].cnt[i])
    119 //             printf("L = %d   R = %d number = %d  cnt[%d] = %d
    ",l, r, tree[rt].number, i, tree[rt].cnt[i]);
    120 //         //return;
    121 //     //}
    122 //         if(l == r) return ;
    123 //     int m = tree[rt].mid();
    124 //     show(rt << 1, l, m);
    125 //     show(rt << 1 | 1, m + 1, r);
    126 // }
    127 
    128 void solve(){
    129 
    130     int n, q;
    131     //cin >> n >> q;
    132     scanf("%d%d",&n, &q);
    133     inx.pb(2); inx.pb(3); inx.pb(5); inx.pb(7);
    134     tree.resize((n << 2) + 10);
    135     build_tree(1, 1, n);
    136     while(q--){
    137         char op[10];
    138         int a, b, c;
    139         scanf("%s", op);
    140         //cout << "op == " << op << endl;
    141         if(op[1] == 'U'){
    142             //cin >> a >> b >> c;
    143             scanf("%d%d%d", &a, &b, &c);
    144             vector<int> vi;
    145             fun(c, vi);
    146             update(1, a, b, vi);
    147         }else{
    148             //cin >> a >> b;
    149             scanf("%d%d",&a, &b);
    150             int cnt = 0;
    151             query(1, a, b, cnt);
    152             //cout << "ANSWER " << cnt << endl;
    153             printf("ANSWER %d
    ", cnt);
    154         }
    155         //show(1, 1, n);
    156     }
    157 }
    158  
    159 int main(){
    160     
    161     // freopen("C:\Users\admin\Desktop\input.txt", "r", stdin);
    162     // freopen("C:\Users\admin\Desktop\output.txt", "w", stdout);
    163     // ios::sync_with_stdio(false);
    164     // cin.tie(0);
    165     // cout.tie(0);
    166     solve();
    167     
    168     return 0;
    169 }
  • 相关阅读:
    Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.
    Spring NoSuchBeanDefinitionException六大原因总结
    深入分析Spring 与 Spring MVC容器
    MyBatis mapper parameterType
    eclipse下的mybatis插件:MyBatipse
    javax.servlet-api 和 servlet-api 区别
    Spring中ClassPathXmlApplication与FileSystemXmlApplicationContext的区别
    dump总结
    操作系统基础知识
    JMM中的Happens-Before原则
  • 原文地址:https://www.cnblogs.com/SSummerZzz/p/12867813.html
Copyright © 2020-2023  润新知