链接:https://ac.nowcoder.com/acm/contest/892/D

题意:

给你一个长度为n的序列,初始为1,2,3...n,对其进行m次操作。
操作有两种:
1 l r  表示将区间[l,r]用 [1,2...r-l+1] 覆盖
2 l r 查询[l,r]的区间和

思路:

线段树

比赛时思路对的,但是代码写的太乱,没改出来,赛后重写。

Lazy我开二维数组,记录子节点左右对应的范围。

其他的就根普通线段树一样。

代码:

#include <bits/stdc++.h>
using namespace std; typedef long long LL;
const int MAXN = 1e5+10; LL Seg[MAXN*4];
LL Lazy[MAXN*4][2];
int n, m; void PushUp(int root)
{
Seg[root] = Seg[root<<1]+Seg[root<<1|1];
} void PushDown(int root)
{
if (Lazy[root][1] != 0)
{
LL lenl = Lazy[root][0];
LL lenr = Lazy[root][1];
LL lenmid = (lenl+lenr)/2;
Seg[root<<1] = (lenl+lenmid)*(lenmid-lenl+1)/2;
Seg[root<<1|1] = (lenmid+1+lenr)*(lenr-lenmid)/2;
Lazy[root<<1][0] = lenl;
Lazy[root<<1][1] = lenmid;
Lazy[root<<1|1][0] = lenmid+1;
Lazy[root<<1|1][1] = lenr;
Lazy[root][0] = Lazy[root][1] = 0;
}
} void Build(int root, int l, int r)
{
if (l == r)
{
Seg[root] = l;
return;
}
int mid = (l+r)/2;
Build(root<<1, l, mid);
Build(root<<1|1, mid+1, r);
PushUp(root);
} void Update(int root, int l, int r, int ql, int qr)
{
if (r < ql || qr < l)
return;
if (ql <= l && r <= qr)
{
LL lenl = l-ql+1, lenr = r-ql+1;
Seg[root] = (lenl+lenr)*(r-l+1)/2;
Lazy[root][0] = lenl;
Lazy[root][1] = lenr;
return;
}
PushDown(root);
int mid = (l+r)/2;
Update(root<<1, l, mid, ql, qr);
Update(root<<1|1, mid+1, r, ql, qr);
PushUp(root);
} LL Query(int root, int l, int r, int ql, int qr)
{
if (r < ql || qr < l)
return 0LL;
if (ql <= l && r <= qr)
return Seg[root];
PushDown(root);
LL res = 0;
int mid = (l+r)/2;
res += Query(root<<1, l, mid, ql, qr);
res += Query(root<<1|1, mid+1, r, ql, qr);
return res;
} int main()
{
int op, l, r;
cin >> n >> m;
Build(1, 1, n);
while (m--)
{
cin >> op >> l >> r;
if (op == 1)
Update(1, 1, n, l, r);
else
cout << Query(1, 1, n, l, r) << endl;
} return 0;
}

  

D-温暖的签到题的更多相关文章

  1. 【西北大学集训队选拔赛】D温暖的签到题(自创数据结构)

    题目链接 #include <bits/stdc++.h> #define NUM #define ll long long using namespace std; int n, m; ...

  2. A 洛谷 P3601 签到题 [欧拉函数 质因子分解]

    题目背景 这是一道签到题! 建议做题之前仔细阅读数据范围! 题目描述 我们定义一个函数:qiandao(x)为小于等于x的数中与x不互质的数的个数. 这题作为签到题,给出l和r,要求求. 输入输出格式 ...

  3. fjwc2019 D3T1 签到题 (贪心)

    #184. 「2019冬令营提高组」签到题 每次询问接近O(1).......考虑贪心 怎么贪心呢? 对于相邻的两个数,我们要保证异或x后单调不降 我们找到两个数二进制上最高的相异位 当左边的数相异位 ...

  4. CTF-练习平台-WEB之 签到题

    一.签到题 根据提示直接加群在群公告里就能找到~

  5. 洛谷P3601签到题(欧拉函数)

    题目背景 这是一道签到题! 建议做题之前仔细阅读数据范围! 题目描述 我们定义一个函数:qiandao(x)为小于等于x的数中与x不互质的数的个数. 这题作为签到题,给出l和r,要求求. 输入输出格式 ...

  6. 【洛谷九月月赛T1】签到题(bsgs)(快速乘)

    说好的签到题呢qwq....怎么我签到题都不会啊qwq 之后看了bsgs才发现貌似不是那么那么难fake!!什么东西... 先贴上部分分做法(也就是枚举1的个数,然后每一步都进行取模(这和最后取模结果 ...

  7. WEB新手之签到题

    写一写web新手赛的题. 这是签到题,开始时需要耐心等待页面中字母全部出现. 字母全部出现后,会跳转到另一个界面,如上图所示.F12没什么特别的地方,这题应该有点难度. 按往常一样,先抓包. 按英文提 ...

  8. EOJ Monthly 2019.1 唐纳德先生与这真的是签到题吗 【数学+暴力+multiset】

    传送门:https://acm.ecnu.edu.cn/contest/126/ C. 唐纳德先生与这真的是签到题吗 单测试点时限: 6.0 秒 内存限制: 1024 MB 唐纳德先生在出月赛的过程中 ...

  9. HLJU 1221: 高考签到题 (三分求极值)

    1221: 高考签到题 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 9  Solved: 4 [Submit][id=1221">St ...

  10. Different Integers 牛客多校第一场只会签到题

    Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, r2), ..., (lq, r ...

随机推荐

  1. Hive- Hive安装

    Hive安装 1.1下载Hive安装包 官网:http://hive.apache.org/downloads.html 个人建议到这里下载:http://apache.forsale.plus/ 1 ...

  2. python function with variadic arguments or keywords(dict) 可变参数与关键字参数

    *args 表示任意个普通参数,调用的时候自动组装为一个tuple **kwags 表示任意个字典类型参数, 调用的时候自动组装成一个dict args和kwags是两个约定俗成的用法. 变长参数可以 ...

  3. poj2127——LCIS

    题目:http://poj.org/problem?id=2127 LCIS,注意存储路径的方法. 代码如下: #include<iostream> #include<cstdio& ...

  4. XmlSerialize error: There was an error generating the XML document.

    今天遇到一个很火的问题, 一个c#的class 序列化成xml后抛出异常, 信息为: XmlSerialize error: There was an error generating the XML ...

  5. win 10 无线标志都不出现

    http://jingyan.baidu.com/article/e75057f2fdd2f1ebc91a89f1.html ipconfig /flushdns netsh winsock rese ...

  6. SQL Replication

    http://www.cnblogs.com/CareySon/archive/2012/06/20/IntroductToSQLServerReplicationPart1.html http:// ...

  7. [poj3071]football概率dp

    题意:n支队伍两两进行比赛,求最有可能获得冠军的队伍. 解题关键:概率dp,转移方程:$dp[i][j] +  = dp[i][j]*dp[i][k]*p[j][k]$表示第$i$回合$j$获胜的概率 ...

  8. emacs for OCaml

    (require 'cl) (require 'package) (add-to-list 'package-archives '("melpa" . "https:// ...

  9. Bind 远程连接出现rndc: connect failed: 192.168.1.66#953: connection refused

    远程连接IP地址为192.168.1.66的BIND DNS服务器,出现 rndc: connect failed: 192.168.1.66#953: connection refused 原因:1 ...

  10. 转载ASP.NET MVC中Session的处理机制

    本文章转载自 http://www.cnblogs.com/darrenji/p/3951065.html ASP.NET MVC中的Session以及处理方式   最近在ASP.NET MVC项目中 ...