思路

update 11.2 树状数组AC

本题莫队过不去,会TLE

但也是个不错的莫队练手题

毕竟Chen_Zhe还给了100分莫队分 (还会给你小对勾)

莫队&&树状数组代码

// luogu-judger-enable-o2
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 2e6 + 7;
int n, m, a[maxn], sum[maxn << 1];
struct edge {
int size,las1,las2;
} zz[maxn];
int ans[maxn];
struct node {
int s, t,id;
bool operator < (const node b ) const {
return t < b.t;
}
} b[maxn]; int read() {
int x = 0, f = 1; char s = getchar();
for (; s < '0' || s > '9'; s = getchar()) if (s == '-') f = -1;
for (; s >= '0' && s <= '9'; s = getchar()) x = x * 10 + s - '0';
return x * f;
} int lowbit(int x) {
return x & -x;
} void add(int x, int dat) {
for (; x <= n; x += lowbit(x))
sum[x] += dat;
} int query(int x) {
int ans = 0;
for (; x >= 1; x -= lowbit(x))
ans += sum[x];
return ans;
} int main() {
n = read();
m = read();
m=read();
for (int i = 1; i <= n; ++i)
a[i] = read();
for (int i = 1; i <= m; ++i)
b[i].s = read(), b[i].t = read(), b[i].id = i;
sort(b + 1, b + 1 + m);
int js = 1;
for (int i = 1; i <= n; ++i) {
if(!zz[a[i]].size) {
zz[a[i]].las1=i;
} else {
if(zz[a[i]].size>=2)
add(zz[a[i]].las2,-1);
add(zz[a[i]].las1,1);
swap(zz[a[i]].las1,zz[a[i]].las2);
zz[a[i]].las1=i;
}
++zz[a[i]].size;
if(b[js].t==i) {
int end_ans=query(b[js].t);
for(; b[js].t==i; ++js) {
ans[b[js].id]=end_ans-query(b[js].s-1);
}
}
}
for (int i = 1; i <= m; ++i) {
printf("%d\n", ans[i]);
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 2000007
using namespace std;
inline int read()
{
int x=0,f=1;char s=getchar();
while('0'>s||s>'9') {
if(s=='-') f=-1;
s=getchar();
}
while('0'<=s&&s<='9') {
x=x*10+s-'0';
s=getchar();
}
return x*f;
}
int n,m,k,now;
int a[maxn];
int belong[maxn];
int vis[maxn];
int ans[maxn];
struct node{
int l,r,id;
}q[maxn];
inline bool cmp(const node &a,const node &b)
{
return belong[a.l]==belong[b.l] ? a.r<b.r : belong[a.l]<belong[b.l];
}
inline void Add(int x)
{
vis[x]==1 ? ++now : now;
++vis[x];
}
inline void Delet(int x)
{
vis[x]==2 ? --now : now;
--vis[x];
}
int main()
{
n=read();
int meiyongdebianliang_qidaotixingfanweidezuoye__233zhemechangdebianliangming=read();
m=read();
k=sqrt(n);
for(int i=1;i<=n;++i)
a[i]=read();
for(int i=1;i<=n;++i)
belong[i]=(i-1)/k+1;
for(int i=1;i<=m;++i)
{
q[i].l=read();
q[i].r=read();
q[i].id=i;
}
sort(q+1,q+1+m,cmp);
int l=1,r=0;
for(int i=1;i<=m;++i)
{
while(l > q[i].l) Add(a[--l]);
while(l < q[i].l) Delet(a[l++]);
while(r < q[i].r) Add(a[++r]);
while(r > q[i].r) Delet(a[r--]);
ans[q[i].id]=now;
}
for(int i=1;i<=m;++i)
printf("%d\n",ans[i]);
return 0;
}

P4113 [HEOI2012]采花 (莫队TLE)的更多相关文章

  1. 【luogu P4113 [HEOI2012]采花】 假题解

    题目链接:https://www.luogu.org/problemnew/show/P4113 为什么要卡莫队!为什么加强的这么毒瘤! 莫队可以拿100分剩下三个点没治了 // luogu-judg ...

  2. 洛谷 P2056 采花 - 莫队算法

    萧芸斓是 Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了 n 朵花,花有 c 种颜色(用整数 1-c 表示) ,且花是排成一排的,以 ...

  3. P4113 [HEOI2012]采花

    题目描述 萧薰儿是古国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花. 花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一排的,以便于 ...

  4. 洛谷P4113 [HEOI2012]采花

    题目描述 萧薰儿是古国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花. 花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一排的,以便于 ...

  5. LUOGU P4113 [HEOI2012]采花

    传送门 解题思路 莫队题卡莫队...莫队只能拿到100分,满分200.正解主席树??发个莫队100分代码. 代码 #include<iostream> #include<cstdio ...

  6. BZOJ 2743: [HEOI2012]采花

    2743: [HEOI2012]采花 Time Limit: 15 Sec  Memory Limit: 128 MBSubmit: 2056  Solved: 1059[Submit][Status ...

  7. cogs:1619. [HEOI2012]采花/luogu P2056

    1619. [HEOI2012]采花 ★★☆   输入文件:1flower.in   输出文件:1flower.out   简单对比时间限制:5 s   内存限制:128 MB [题目描述] 萧薰儿是 ...

  8. BZOJ_2743_[HEOI2012]采花_离线+树状数组

    BZOJ_2743_[HEOI2012]采花_离线+树状数组 Description 萧芸斓是Z国的公主,平时的一大爱好是采花.今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花 .花园足够大 ...

  9. BZOJ 2743: [HEOI2012]采花 离线树状数组

    2743: [HEOI2012]采花 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2743 Description 萧芸斓是Z国的公主, ...

随机推荐

  1. PL/SQL常用表达式及举例(一)

    IF 判断条件 THEN 满足条件时执行语句 END IF; DECLARE v_countResult NUMBER; BEGIN SELECT COUNT(empno) INTO v_countR ...

  2. xshell免费下载安装使用

    下载链接:https://www.netsarang.com/zh/all-downloads/ 然后点击下载: 然后接下来这一步很关键: 点击免费授权页面,然后填完邮箱会有激活链接,然后就可以下载免 ...

  3. Centos7网桥配置

    CentOS 的网桥虽然配置了很多次,不过总是记不住那几条,还是简单记录下,增加网桥可以通过brctl命令,但是为了简便快捷,直接生成配置文件即可 1.在/etc/sysconfig/network- ...

  4. mysql 数据表的增删改查 目录

    mysql 表的增删改查 mysql 表的增删改查 修改表结构 mysql 复制表 mysql 删除表

  5. phpStudy安装

    以下一直默认安装 访问地址:http://127.0.0.1/vue/2.html

  6. linux下面发布80端口的服务

    1:linux下面发布端口号为80 的服务,要在root用户下面发布.否则提示权限不够 2:在普通用户下面配置的java环境,在root用户下面不可用. 解决方法:2.1  要使用source /et ...

  7. WebService之Axis2 (3):使用services.xml文件发布WebService

    用Axis2实现Web Service,虽然可以将POJO类放在axis2\WEB-INF\pojo目录中直接发布成Web Service,这样做不需要进行任何配置,但这些POJO类不能在任何包中.这 ...

  8. jQ live用法

    我们给元素绑定单击事件.用得最多的都是$("#id").click(function(){}); 但我们动态添加的元素.这样绑定是不行的.必须借助live  $("#id ...

  9. PKU2418_树种统计(map应用||Trie树)

    Description Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nu ...

  10. XDU1024简单逆序对(贪心||分治)

    题目描述 逆序对问题对于大家来说已经是非常熟悉的问题了,就是求i<j时,a[i] > a[j]的组数.现在请你求出一串数字中的逆序对的个数,需要注意的是,这些数字均在[0,9]之内. 输入 ...