P4113 [HEOI2012]采花 (莫队TLE)
思路
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)的更多相关文章
- 【luogu P4113 [HEOI2012]采花】 假题解
题目链接:https://www.luogu.org/problemnew/show/P4113 为什么要卡莫队!为什么加强的这么毒瘤! 莫队可以拿100分剩下三个点没治了 // luogu-judg ...
- 洛谷 P2056 采花 - 莫队算法
萧芸斓是 Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了 n 朵花,花有 c 种颜色(用整数 1-c 表示) ,且花是排成一排的,以 ...
- P4113 [HEOI2012]采花
题目描述 萧薰儿是古国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花. 花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一排的,以便于 ...
- 洛谷P4113 [HEOI2012]采花
题目描述 萧薰儿是古国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花. 花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一排的,以便于 ...
- LUOGU P4113 [HEOI2012]采花
传送门 解题思路 莫队题卡莫队...莫队只能拿到100分,满分200.正解主席树??发个莫队100分代码. 代码 #include<iostream> #include<cstdio ...
- BZOJ 2743: [HEOI2012]采花
2743: [HEOI2012]采花 Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 2056 Solved: 1059[Submit][Status ...
- cogs:1619. [HEOI2012]采花/luogu P2056
1619. [HEOI2012]采花 ★★☆ 输入文件:1flower.in 输出文件:1flower.out 简单对比时间限制:5 s 内存限制:128 MB [题目描述] 萧薰儿是 ...
- BZOJ_2743_[HEOI2012]采花_离线+树状数组
BZOJ_2743_[HEOI2012]采花_离线+树状数组 Description 萧芸斓是Z国的公主,平时的一大爱好是采花.今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花 .花园足够大 ...
- BZOJ 2743: [HEOI2012]采花 离线树状数组
2743: [HEOI2012]采花 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2743 Description 萧芸斓是Z国的公主, ...
随机推荐
- short url短链接原理
一.什么是短链接 含义:就是把普通网址,转换成比较短的网址.比如:http://t.cn/RlB2PdD 这种,比如:微博:这些限制字数的应用里都用到这种技术. 优点:短.字符少.美观.便于发布.传播 ...
- 快捷键(SourceInsight)
选择一块 : Ctrl+-选择一行 : Shift+F6到下一个函数 : 小键盘 +上一个函数 : 小键盘 -高亮当前单词 : Shift+F8回退.前进 alt + , alt + .最后一个窗口 ...
- crawlspider爬虫:定义url规则
spider爬虫,适合meta传参的爬虫(列表页,详情页都有数据要爬取的时候) crawlspider爬虫,适合不用meta传参的爬虫 scrapy genspider -t crawl it it. ...
- python中执行shell命令行read结果
+++++++++++++++++++++++++++++ python执行shell命令1 os.system 可以返回运行shell命令状态,同时会在终端输出运行结果 例如 ipython中运行如 ...
- python 基础 内置函数
内置参数 print(all([5,-1,5])) # 非0都是真 true print(all([0,-1,5])) # false print(any([1,0,5])) # 有一个数据为真,就为 ...
- 帝国cms调用最新文章 利用文字调用标签phomenews
最近建站时,朋友要求在头部用帝国cms调用最新文章,当时想了用灵动标签调用,但需要设置一个具体的栏目id,这样就不是调用全站的最新文章了,后面查看了一下标签说明,想到了文字调用标签phomenews. ...
- Domino代理运行问题
当Server出现“operation is disallowed in this session”此命令时为代理权限问题,修改后即可正常运行代理.
- [py][mx]django get方法返回login页面
get方法返回login.html users/views.py def login(request): if request.method == "POST": pass eli ...
- 五、Mosquitto 高级应用之权限管理
本文将讲解 Mosquitto 权限管理.如果还没有搭建 Mosquitto 服务的可以参考我的另外两篇文章<< 一.Mosquitto 介绍&安装>> << ...
- (转)跨域的另一种解决方案——CORS(Cross-Origin Resource Sharing)跨域资源共享
在我们日常的项目开发时使用AJAX,传统的Ajax请求只能获取在同一个域名下面的资源,但是HTML5打破了这个限制,允许Ajax发起跨域的请求.浏览器是可以发起跨域请求的,比如你可以外链一个外域的图片 ...