zoukankan      html  css  js  c++  java
  • 线段树之老年康复

    hdu - 1166 敌兵布阵(求和) 题目链接:戳这里

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <string>
     4 #include <algorithm>
     5 #include <cmath>
     6 #include <vector>
     7 #include <queue>
     8 #include <iostream>
     9 using namespace std;
    10 typedef long long ll;
    11 #define lson l,mid,rt<<1
    12 #define rson mid+1,r,rt<<1|1
    13 #define lef rt<<1
    14 #define rig rt<<1|1
    15 const int maxn = 5e4 + 10;
    16 int t, n;
    17 int sum[maxn << 2];
    18 char st[maxn];
    19 void update(int rt)
    20 {
    21     sum[rt] = sum[lef] + sum[rig];
    22 }
    23 void build(int l, int r,int rt) //l 与 r代表的是数组里值的位置,rt用来找该值在树中的位置
    24 {
    25     if(l == r)
    26     {
    27          scanf("%d", &sum[rt]);
    28          return;
    29     }
    30     //cout<<1<<endl;
    31     int mid = l + (r - l) / 2;
    32     build(lson), build(rson);
    33     update(rt);
    34 }
    35 void add(int p, int c, int l, int r,int rt)
    36 {
    37     if(l == r)
    38     {
    39         sum[rt] += c;
    40         return;
    41     }
    42     //cout<<l<<" "<<r<<endl;
    43     int mid = l + (r - l) / 2;
    44     if(p <= mid) add(p, c, lson);
    45     else add(p, c, rson);
    46     update(rt);
    47 }
    48 int qy(int L, int R, int l, int r, int rt)
    49 {
    50     if(L <= l && R >= r)
    51     {
    52         return sum[rt];
    53     }
    54     //cout<<2<<endl;
    55     int mid = l + (r - l) / 2;
    56     int ans = 0;
    57     if(L <= mid) ans += qy(L, R, lson);
    58     if(R > mid) ans += qy(L, R, rson);
    59     return ans;
    60 }
    61 int main(){
    62 
    63     scanf("%d", &t);
    64     for(int cas = 1; cas <= t; ++cas)
    65     {
    66         printf("Case %d:
    ", cas);
    67         scanf("%d", &n);
    68         build(1, n, 1);
    69         //printf("111");
    70         while(scanf("%s", st))
    71         {
    72             if(st[0] == 'E') break;
    73             int u, v;
    74             scanf("%d %d", &u, &v);
    75             if(st[0] == 'Q')
    76             {
    77                 printf("%d
    ", qy(u, v, 1, n, 1));
    78             }
    79             if(st[0] == 'A')
    80             {
    81                 add(u, v, 1, n, 1);
    82             }
    83             if(st[0] == 'S')
    84             {
    85                 add(u, -v, 1, n, 1);
    86             }
    87         }
    88     }
    89     return 0;
    90 }
    View Code

    牛客网多校第6场 I Team Rocket(维护最大值) 题目链接:戳这里

      1 #include <iostream>
      2 #include<bits/stdc++.h>
      3 using namespace std;
      4 typedef long long ll;
      5 #define lson l,mid,rt<<1
      6 #define rson mid+1,r,rt<<1|1
      7 #define lef rt<<1
      8 #define rig rt<<1|1
      9 const int maxn = 2e5 + 10;
     10 const ll mod = 998244353;
     11 const ll inf = 0x3f3f3f3f;
     12 struct nod
     13 {
     14     int l;
     15     int r;
     16     int id;
     17     nod(){}
     18     nod(int L, int R, int Id)
     19     {
     20         l = L, r = R, id = Id;
     21     }
     22     bool operator < (const nod &b) const
     23     {
     24         return l < b.l;
     25     }
     26 }nu[maxn], tre[maxn << 2];
     27 int ans[maxn];
     28 ll res = 0;
     29 ll x, y;
     30 int pos = 0, cnt = 0;
     31 void updat(int rt)
     32 {
     33     tre[rt].r = max(tre[lef].r, tre[rig].r);
     34     return;
     35 }
     36 void build(int l, int r, int rt)
     37 {
     38     if(l == r)
     39     {
     40         tre[rt] = nu[l];
     41         return;
     42     }
     43     int mid = l + (r - l) / 2;
     44     build(lson), build(rson);
     45     updat(rt);
     46 }
     47 void qy(int l, int r, int rt, int i,int x)
     48 {
     49     if(tre[rt].r < x) return;
     50     if(l == r)
     51     {
     52         ++cnt;
     53         tre[rt].r = -inf;
     54         res = (res * tre[rt].id) % mod;
     55         ans[tre[rt].id] = i;
     56         return;
     57     }
     58     int mid = l + (r - l) / 2;
     59     qy(lson, i, x);
     60     if(pos >= mid + 1) qy(rson, i, x);
     61     updat(rt);
     62 }
     63 int main()
     64 {
     65     int t, n, m;
     66     scanf("%d", &t);
     67     for(int cas = 1; cas <= t; ++cas)
     68     {
     69         memset(tre, 0, sizeof(tre));
     70         memset(ans, 0, sizeof(ans));
     71         scanf("%d %d", &n, &m);
     72         for(int i = 1; i <= n; ++i)
     73         {
     74             scanf("%lld %lld", &nu[i].l, &nu[i].r);
     75             nu[i].id = i;
     76         }
     77         sort(nu + 1, nu + 1 + n);
     78         build(1, n, 1);
     79         res = 0;
     80         printf("Case #%d:
    ", cas);
     81         for(int i = 1; i <= m; ++i)
     82         {
     83             scanf("%lld", &y);
     84             x = y ^ res;
     85             cnt = 0, res = 1;
     86             pos = upper_bound(nu + 1, nu + 1 + n,nod(int(x), 0, 0)) - nu - 1;
     87             if(pos > 0)
     88                 qy(1, n, 1, i, x);
     89             if(cnt == 0)
     90                 res = 0;
     91             printf("%d
    ", cnt);
     92         }
     93         for(int i = 1; i <= n; ++i)
     94         {
     95 
     96             printf("%d%c", ans[i], i==n?'
    ':' ');
     97         }
     98 
     99     }
    100     return 0;
    101 }
    View Code
  • 相关阅读:
    题解 POJ1149 Pigs
    题解 【网络流24题】运输问题
    题解 【网络流24题】太空飞行计划
    题解 【网络流24题】方格取数问题
    题解 密码锁
    题解 【重庆八中模拟赛】寻找代表元
    题解 [SHOI2010]最小生成树
    题解 【ZJOI2009】 假期的宿舍
    题解 [ZJOI2008]树的统计Count
    JSP页面中的pageEncoding和contentType两种属性(转)
  • 原文地址:https://www.cnblogs.com/zmin/p/9427204.html
Copyright © 2011-2022 走看看