zoukankan      html  css  js  c++  java
  • [AH2017/HNOI2017]影魔

    嘟嘟嘟


    这题真的挺神的,我是真没想出来。


    洛谷的第一篇题解说的非常妙,实在是佩服。
    就是我们首先预处理出对于第(i)个数,在(i)左边比第一个比(i)大的数(l_i),在(i)右边第一个比(i)大的数(r_i)
    这个可以用单调栈扫两边分别求出来。


    然后我们考虑位于([l_i, r_i])中的所有数产生的贡献:
    1.(l_i)(r_i)单独产生(p1)的贡献。
    2.位于([l_i + 1, i - 1])的数都和(r_i)产生(p2)的贡献。
    3.位于([i + 1, r_i - 1])的数都和(l_i)产生(p2)的贡献。
    所以我们把所有的(l_i, r_i)排序,然后从1开始扫到(n),对于三种贡献:
    1.遇到(r_i),就在对应的(l_i)上加(p1)
    2.遇到(r_i),就在区间([l_i + 1, i - 1])(p2)
    3.遇到(l_i),就在区间([i + 1, r_i - 1])(p2)
    同时询问也是离线下来排好序的,然后我们用前缀和的思想,把每一个询问([L_i, R_i])拆成(L_i - 1)(R_i),修改的同时遇到(L_i - 1)就查询([L_i, R_i]),并从(ans_i)中减去;遇到(R_i),就再查询一遍([L_i, R_i]),加到(ans_i)里去。
    这也就能解释为什么(p1)的贡献加在(l_i)而不是(r_i)上了:因为扫到(r_i)的时候,查询区间的右端点一定满足(R_i = r_i),而这个贡献能否加上去,就是看(L_i)是否包含(l_i)。所以应该加到(l_i)上。


    我这题之所以调了半天,是因为把当前枚举的位置和区间所在编号弄混了……
    区间修改大佬们似乎都是写的树状数组,我一时想不明白,还是乖乖写线段树去了。
    p.s.区间排序用vector直接存下来是真的方便

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define In inline
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 2e5 + 5;
    inline ll read()
    {
      ll ans = 0;
      char ch = getchar(), last = ' ';
      while(!isdigit(ch)) last = ch, ch = getchar();
      while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
      if(last == '-') ans = -ans;
      return ans;
    }
    inline void write(ll x)
    {
      if(x < 0) x = -x, putchar('-');
      if(x >= 10) write(x / 10);
      putchar(x % 10 + '0');
    }
    
    int n, m, p1, p2, a[maxn];
    ll ans[maxn];
    
    struct Node
    {
      int l, r;
    }t[maxn << 1];
    struct Node2
    {
      int id, flg;  //0:l, 1:r; -1:L, 1:R
    };
    vector<Node2> v[maxn], q[maxn];
    
    int st[maxn], top = 0;
    In void init()
    {
      for(int i = 1; i <= n; ++i)
        {
          while(top && a[st[top]] < a[i]) --top;
          t[i].l = st[top]; st[++top] = i;
        }
      st[top = 0] = n + 1;
      for(int i = n; i; --i)
        {
          while(top && a[st[top]] < a[i]) --top;
          t[i].r = st[top]; st[++top] = i;
        }
      for(int i = 1; i <= n; ++i)
        {
          v[t[i].l].push_back((Node2){i, 0});
          v[t[i].r].push_back((Node2){i, 1});
        }
    }
    
    int l[maxn << 2], r[maxn << 2];
    ll sum[maxn << 2], lzy[maxn << 2];
    In void build(int L, int R, int now)
    {
      l[now] = L; r[now] = R;
      if(L == R) return;
      int mid = (L + R) >> 1;
      build(L, mid, now << 1);
      build(mid + 1, R, now << 1 | 1);
    }
    In void change(int now, int d)
    {
      sum[now] += 1LL * (r[now] - l[now] + 1) * d;
      lzy[now] += d;
    }
    In void pushdown(int now)
    {
      if(lzy[now])
        {
          change(now << 1, lzy[now]), change(now << 1 | 1, lzy[now]);
          lzy[now] = 0;
        }
    }
    In void update(int L, int R, int now, int d)
    {
      if(L > R) return;
      if(l[now] == L && r[now] == R) {change(now, d); return;}
      pushdown(now);
      int mid = (l[now] + r[now]) >> 1;
      if(R <= mid) update(L, R, now << 1, d);
      else if(L > mid) update(L, R, now << 1 | 1, d);
      else update(L, mid, now << 1, d), update(mid + 1, R, now << 1 | 1, d);
      sum[now] = sum[now << 1] + sum[now << 1 | 1];
    }
    In ll query(int L, int R, int now)
    {
      if(l[now] == L && r[now] == R) return sum[now];
      pushdown(now);
      int mid = (l[now] + r[now]) >> 1;
      if(R <= mid) return query(L, R, now << 1);
      else if(L > mid) return query(L, R, now << 1 | 1);
      else return query(L, mid, now << 1) + query(mid + 1, R, now << 1 | 1);
    }
    
    int main()
    {
      //freopen("ha.in", "r", stdin);
      n = read(), m = read(), p1 = read(), p2 = read();
      for(int i = 1; i <= n; ++i) a[i] = read();
      init();
      for(int i = 1; i <= m; ++i)
        {
          int L = read(), R = read();
          t[i + n] = (Node){L, R};
          q[L - 1].push_back((Node2){i, -1});
          q[R].push_back((Node2){i, 1});
        }
      build(1, n, 1);
      for(int i = 1; i <= n; ++i)
        {
          for(int j = 0; j < (int)v[i].size(); ++j)
    	{
    	  int id = v[i][j].id, flg = v[i][j].flg;
    	  if(!flg) update(id + 1, t[id].r - 1, 1, p2);
    	  else
    	    {
    	      if(t[id].l) update(t[id].l, t[id].l, 1, p1);
    	      update(t[id].l + 1, id - 1, 1, p2);
    	    }
    	}
          for(int j = 0, id; j < (int)q[i].size(); ++j)
    	id = q[i][j].id, ans[id] += query(t[id + n].l, t[id + n].r, 1) * q[i][j].flg;
        }
      for(int i = 1; i <= m; ++i)
        write(ans[i] + 1LL * p1 * (t[i + n].r - t[i + n].l)), enter;
      return 0;
    }
    
  • 相关阅读:
    2019-12-2 异常捕获
    类与类之间的6种关系
    关键字与理解
    this与super的语法比较
    单继承与多继承对比
    为什么javaBean要有get/set方法的设计
    多态在面向对象中的意义以及带来的好处
    十四、线程设计
    十三、窗口设计
    十二、SWING界面设计
  • 原文地址:https://www.cnblogs.com/mrclr/p/10571913.html
Copyright © 2011-2022 走看看