P8969 幻梦 | Dream with Dynamic

简介: P8969 幻梦 | Dream with Dynamic

怎么比 Ntokisq 还简略

思路

线段树 + 势能分析。

popcount 看起来不好维护,每次都需要对整个序列大力做。

注意到 popcount 的值域只有 O(logV)O(logV),所以考虑在线段树上的每个结点维护一个置换标记 ff 来维护 popcount.

可以认为 popcount 操作等价于应用一次置换:(1popcount(1)2popcount(2)3popcount(3)log(V)popcount(log(V)))(123⋯log⁡(V)popcount⁡(1)popcount⁡(2)popcount⁡(3)⋯popcount⁡(log(V))).

并且多次 popcount 操作等价于对置换进行多次乘法。

假设在这些 popcount 操作中间出现区间加法,形式上等价于 f(popcount(x+A))+Bf(popcount⁡(x+A))+B.

所以只需要合理地对置换和加法标记进行合并。

当只有置换标记的时候,只需要简单复合两种置换标记。

假设当前是区间置换,那么在下一次区间置换前进行的区间加法一定是非负整数次。只需要维护加法标记。

下一次区间置换显然需要清空整个区间的加法标记。这里只能大力向下清空子树的标记,然后再重新给区间打上置换标记。

对于清空子树外的操作,显然复杂度为 O(nlogn)O(nlog⁡n)。上面的复杂度看起来很劣,考虑分析一下清空子树的复杂度。

清空子树只需要递归到第一个有置换标记的结点。记这些结点为终止结点,令势能为终止结点的个数。

每次操作至多增加 O(logn)O(log⁡n) 个终止结点,共 O(qlogn)O(qlog⁡n) 个;每次 push_down 至多增加 22 个终止结点,总数也是 O(qlogn)O(qlog⁡n).

清空两个终止结点的复杂度是 O(logV)O(log⁡V),所以复杂度均摊下来是 O(nlogn+qlognlogV)O(nlog⁡n+qlog⁡nlogV).

代码

hide code#include<cstdio>

usingnamespace std;


typedeflonglong ll;


constint maxn = 3e5 + 5;

constint lg_sz = 32;

constint sgt_sz = maxn << 2;


int n, q;

int a[maxn];


namespace SGT

{

   #define ls (k << 1)

   #define rs (k << 1 | 1)


   int per[sgt_sz][lg_sz];

   ll tag[sgt_sz];

   bool comp[sgt_sz];


   voidbuild(int k, int l, int r)

   {

       for (int i = 0; i < lg_sz; i++) per[k][i] = i;

       if (l == r) return tag[k] = a[l], comp[k] = true, void();

       int mid = (l + r) >> 1;

       build(ls, l, mid), build(rs, mid + 1, r);

   }


   voidcls_tag(int k)

   {

       for (int i = 0; i < lg_sz; i++) per[k][i] = __builtin_popcountll(per[k][i] + tag[k]);

       tag[k] = 0;

   }


   voidpush_down(int k)

   {

       if (comp[k])

       {

           for (int i = 0; i < lg_sz; i++) per[ls][i] = per[k][per[ls][i]], per[rs][i] = per[k][per[rs][i]];

           for (int i = 0; i < lg_sz; i++) per[k][i] = i;

           comp[ls] = comp[rs] = true, comp[k] = false;

       }

       if (tag[k]) tag[ls] += tag[k], tag[rs] += tag[k], tag[k] = 0;

   }


   voidcls_comp(int k, int l, int r)

   {

       if (comp[k]) returncls_tag(k), void();

       push_down(k);

       int mid = (l + r) >> 1;

       cls_comp(ls, l, mid), cls_comp(rs, mid + 1, r);

   }


   voidupd_comp(int k, int l, int r, int ql, int qr)

   {

       if ((l >= ql) && (r <= qr)) returncls_comp(k, l, r), comp[k] = true, void();

       push_down(k);

       int mid = (l + r) >> 1;

       if (ql <= mid) upd_comp(ls, l, mid, ql, qr);

       if (qr > mid) upd_comp(rs, mid + 1, r, ql, qr);

   }


   voidupd_tag(int k, int l, int r, int ql, int qr, int w)

   {

       if ((l >= ql) && (r <= qr)) return tag[k] += w, void();

       push_down(k);

       int mid = (l + r) >> 1;

       if (ql <= mid) upd_tag(ls, l, mid, ql, qr, w);

       if (qr > mid) upd_tag(rs, mid + 1, r, ql, qr, w);

   }


   ll query(int k, int l, int r, int p)

   {

       if (l == r) return per[k][0] + tag[k];

       push_down(k);

       int mid = (l + r) >> 1;

       if (comp[k])

       {

           if (p <= mid) return per[k][query(ls, l, mid, p)] + tag[k];

           return per[k][query(rs, mid + 1, r, p)] + tag[k];

       }

       if (p <= mid) returnquery(ls, l, mid, p) + tag[k];

       returnquery(rs, mid + 1, r, p) + tag[k];

   }

}

usingnamespace SGT;


intmain()

{

   scanf("%d%d", &n, &q);

   for (int i = 1; i <= n; i++) scanf("%d", &a[i]);

   build(1, 1, n);

   while (q--)

   {

       int l, r, v;

       char ch = getchar();

       while ((ch < 'A') || (ch > 'Z')) ch = getchar();

       if (ch == 'A') scanf("%d%d%d", &l, &r, &v), upd_tag(1, 1, n, l, r, v);

       elseif (ch == 'P') scanf("%d%d", &l, &r), upd_comp(1, 1, n, l, r);

       elsescanf("%d", &l), printf("%lld\n", query(1, 1, n, l));

       // puts("done");

   }

   return0;

}

相关文章
|
机器学习/深度学习 编解码 人工智能
Reading Notes: Human-Computer Interaction System: A Survey of Talking-Head Generation
由于人工智能的快速发展,虚拟人被广泛应用于各种行业,包括个人辅助、智能客户服务和在线教育。拟人化的数字人可以快速与人接触,并在人机交互中增强用户体验。因此,我们设计了人机交互系统框架,包括语音识别、文本到语音、对话系统和虚拟人生成。接下来,我们通过虚拟人深度生成框架对Talking-Head Generation视频生成模型进行了分类。同时,我们系统地回顾了过去五年来在有声头部视频生成方面的技术进步和趋势,强调了关键工作并总结了数据集。 对于有关于Talking-Head Generation的方法,这是一篇比较好的综述,我想着整理一下里面比较重要的部分,大概了解近几年对虚拟人工作的一些发展和
|
机器学习/深度学习 算法
Data Structures and Algorithms (English) - 7-28 Review of Programming Contest Rules(30 分)
Data Structures and Algorithms (English) - 7-28 Review of Programming Contest Rules(30 分)
193 0
Data Structures and Algorithms (English) - 7-28 Review of Programming Contest Rules(30 分)
|
人工智能
Practical Skill Test——AT
题目描述 We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j). The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is Ai,j.
99 0
Data Structures and Algorithms (English) - 7-10 Saving James Bond - Easy Version(25 分)
Data Structures and Algorithms (English) - 7-10 Saving James Bond - Easy Version(25 分)
83 0
Data Structures and Algorithms (English) - 6-5 Evaluate Postfix Expression(25 分)
Data Structures and Algorithms (English) - 6-5 Evaluate Postfix Expression(25 分)
106 0
How China's Developers Are Defining The Information Age (Infographic 5)
China’s Developers: the technologies of tomorrow that are defining the information age
1611 0
How China's Developers Are Defining The Information Age (Infographic 5)
How China's Developers Are Defining The Information Age (Infographic 4)
China’s Developers: the essentials of today that are defining the information age
1463 0
How China's Developers Are Defining The Information Age (Infographic 4)
How China's Developers Are Defining The Information Age (Infographic 3)
China’s Developers: the trending technologies that are defining the information age
1565 0
How China's Developers Are Defining The Information Age (Infographic 3)
How China's Developers Are Defining The Information Age (Infographic 2)
China’s Developers: the tools that are defining the information age
1580 0
How China's Developers Are Defining The Information Age (Infographic 2)
How China's Developers Are Defining The Information Age (Infographic 1)
China's Developers at a Glance: 9 key takeaways from China's Developer Survey Report 2017
1858 0
How China's Developers Are Defining The Information Age (Infographic 1)