链接    https://www.codechef.com/FEB18/problems/CHANOQ/

Let's say that a segment [l, r] crosses a point x if l ≤ x ≤ r.

Chef wants you to answer Q queries. In each query, you are given a set of M distinct points X1, X2, ..., XM; you should compute the number of good segments [Li, Ri] among the given N segments. A segment is good if it crosses an odd number of points out of the given M points.

Input

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains a single integer N denoting the number of segments.
  • The following N lines describe the segments. For each i, the i-th of these lines contains two space-separated integers Li and Ri.
  • The next line contains a single integer Q denoting the number of queries.
  • The following Q lines describe the queries. Each of these lines starts with an integer M — the number of points in this query, followed by M space-separated integers X1, X2, ..., XM.

Constraints

  • 1 ≤ T ≤ 100
  • 1 ≤ Q ≤ N ≤ 100,000
  • sum of M for all queries ≤ N
  • sum of N over all test cases ≤ 200,000
  • 1 ≤ Xi ≤ N for each valid i
  • all Xi in each query are distinct
  • 1 ≤ Li ≤ Ri ≤ N for each valid i

Subtasks

Subtask #1 (10 points): N, Q ≤ 300

Subtask #2 (90 points): original constraints

Output

For each query, print a single line containing the number of good segments for that query.

Example

Input:

2
5
4 5
3 5
2 4
1 3
5 5
2
4 1 2 3 4
1 4
5
4 5
3 5
2 4
2 3
5 5
2
2 2 5
3 1 2 5 Output: 3
3
5
5
/////////////////////////////////////////////////
根据M的大小把询问区分开,M足够大的直接一个o(n)处理,M小的把询问离线了处理
离线后复杂度为d/x*x²logn(d为总点数,x为M小的询问的M值,logn来自树状数组)
贴个自己代码
#include <bits/stdc++.h>
#define mst(a,b) memset((a),(b), sizeof a)
#define lowbit(a) ((a)&(-a))
#define IOS ios::sync_with_stdio(0);cin.tie(0);
using namespace std;
typedef long long ll;
const int mod=1e9+;
const int maxn=1e5+;
int n;
int bit[maxn];
void update(int pos){
while(pos<=n){
++bit[pos];
pos+=lowbit(pos);
}
}
int get(int pos){
int ret=;
while(pos){
ret+=bit[pos];
pos-=lowbit(pos);
}
return ret;
}
vector<int>rr[maxn],uu[maxn];
int l[maxn],r[maxn];
int ss[maxn];
int blo;
bool vis[maxn];int ap[maxn];
int x[maxn],sum[maxn];
ll ans[maxn];
struct node{
int l,to,c;
node(int a,int b,int d){l=a;to=b;c=d;}
};
vector<node>kk[maxn];
int main(){
#ifdef local
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif
int t;scanf("%d",&t);
while(t--){
scanf("%d",&n);
blo=sqrt(n);
for(int i=;i<=n;++i)scanf("%d%d",&l[i],&r[i]); for(int i=;i<=n;++i)rr[i].clear(),uu[i].clear(),kk[i].clear(),ss[i]=; for(int i=;i<=n;++i)rr[r[i]].push_back(l[i]),++ss[l[i]],--ss[r[i]+]; for(int i=;i<=n;++i)ss[i]=ss[i-]+ss[i]; int q;scanf("%d",&q);
for(int id=;id<=q;++id){
int m;scanf("%d",&m);
for(int i=;i<=m;++i)scanf("%d",&x[i]);
if(m>=blo-){
for(int i=;i<=m;++i)vis[x[i]]=true;
for(int i=;i<=n;++i)sum[i]=sum[i-]+vis[i];
int tmp=;
for(int i=;i<=n;++i){
int g=sum[r[i]]-sum[l[i]-];
tmp+=(g%?:);
}
for(int i=;i<=m;++i)vis[x[i]]=false;
ans[id]=tmp;
}else{
sort(x+,x++m);
int tmp=;
for(int i=;i<=m;++i)tmp+=ss[x[i]];
ans[id]=tmp;
int cc=-;
for(int len=;len<m;++len){
for(int st=;st+len<=m;++st){
int le=x[st],ri=x[st+len];
kk[ri].push_back(node(le,id,cc));
}
cc=-cc;
}
}
}
for(int i=;i<=n;++i)bit[i]=;
for(int i=n;i>;--i){
for(int j=;j<rr[i].size();++j)update(rr[i][j]);
for(int j=;j<kk[i].size();++j){
node&ha=kk[i][j];
ans[ha.to]+=ha.c*get(ha.l);
}
}
for(int i=;i<=q;++i)printf("%lld\n",ans[i]);
}
return ;
}

系数的设置来自一个表格

被计算次数

系数

在计算中被包含的长度

1

2

3

4

5

6

计算的长度

1

1

1

2

3

4

5

6

2

-2

1

2

3

4

5

3

2

1

2

3

4

4

-2

1

2

3

5

2

1

2

简单来说就是你算覆盖长度为1的实际上把覆盖长度更多的也算进去了,乘上系数后就只剩下奇数的长度了

区分点设置的也不是很好,可能数据不是很变态,0.几秒过了

放个zk的代码,zk  orz,他的代码更容易理解一点(他说也可以用主席树在线查询,emm有道理)

#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <stack>
#include <set>
#include <queue>
#include <assert.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int N = 1e5 + ;
const int M = 3e5 + ;
const LL mod = 1e9 + ;
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define SZ(X) (X.size())
vector<int>F[N];
vector<pii>L;
int ans[N];
int cover[N];
int n,q;
int solve(int x){
for(int i = ; i < F[x].size(); i++){
cover[F[x][i]]++;
}
for(int i = ; i <= n; i++){
cover[i] += cover[i - ];
}
int ret = ;
for(int i = ; i < L.size(); i++){
if((cover[L[i].Y] - cover[L[i].X - ]) & )ret++;
}
for(int i = ; i <= n; i++)cover[i] = ;
return ret;
}
struct Query{
int el,er;
int flag;
int id;
Query(){}
Query(int _el,int _er,int _flag,int _id){
el = _el;er = _er;flag = _flag;id = _id;
}
};
vector<Query>Q[N];
int tree[N];
void update(int o){
while(o <= n){
tree[o]++;
o += o & -o;
}
}
int sum(int l,int r){
l--;
int ret = ;
while(r){
ret += tree[r];
r -= r & -r;
}
while(l){
ret -= tree[l];
l -= l & -l;
}
return ret;
}
void solveQ(){
int l = ;
for(int ql = ; ql <= n; ql++){
while(l < n && L[l].X <= ql){
update(L[l].Y);
l++;
}
for(int j = ; j < Q[ql].size(); j++){
ans[Q[ql][j].id] += Q[ql][j].flag * sum(Q[ql][j].el,Q[ql][j].er);
}
}
for(int i = ; i <= n; i++)tree[i] = ;
}
int main() {
#ifdef local
freopen("input", "r", stdin);
#endif // local
ios::sync_with_stdio();
cin.tie();
int t;
cin>>t;
while(t--){
cin>>n;
for(int i = ,l,r; i < n; i++){
cin>>l>>r;
L.pb(mp(l,r));
}
sort(L.begin(),L.end());
cin>>q;
for(int i = ,xn,x; i < q; i++){
cin>>xn;
while(xn--){
cin>>x;
F[i].pb(x);
}
}
int split = sqrt((n + )/log2(n + ));
for(int i = ; i < q; i++){
if(F[i].size() > split){
ans[i] = solve(i);
}
else {
ans[i] = ;
sort(F[i].begin(),F[i].end());
for(int z = ; z < F[i].size(); z++){
for(int j = z; j < F[i].size(); j++){
if((j - z) & )continue;
int el = F[i][j];
int er = (j + == F[i].size() ? n : F[i][j + ] - );
int x1 = (z == ? : F[i][z - ]);
int x2 = F[i][z];
// cout<<x1 + 1<<" "<<x2<<" "<<el<<" "<<er<<endl;
Q[x1].pb(Query(el,er,-,i));
Q[x2].pb(Query(el,er,,i));
}
}
}
}
solveQ();
for(int i = ; i < q; i++){
cout<<ans[i]<<"\n";
}
cout<<flush;
L.clear();
for(int i = ; i <= n; i++)Q[i].clear();
for(int i = ; i < q; i++)F[i].clear();
}
}

CodeChef---- February Challenge 2018----Chef and odd queries(复杂度分块计算)的更多相关文章

  1. codechef February Challenge 2018 简要题解

    比赛链接:https://www.codechef.com/FEB18,题面和提交记录是公开的,这里就不再贴了 Chef And His Characters 模拟题 Chef And The Pat ...

  2. Codechef August Challenge 2018 : Chef at the River

    传送门 (要是没有tjm(Sakits)的帮忙,我还真不知道啥时候能做出来 结论是第一次带走尽可能少的动物,使未带走的动物不冲突,带走的这个数量就是最优解. 首先这个数量肯定是下界,更少的话连第一次都 ...

  3. CodeChef February Challenge 2018 Broken Clock (三角函数推导 + 矩阵快速幂)

    题目链接  Broken Clock   中文题面链接 令$cos(xα) = f(x)$ 根据三角函数变换公式有 $f(x) = \frac{2d}{l} f(x-1) - f(x-2)$ 我们现在 ...

  4. CodeChef February Challenge 2018 Points Inside A Polygon (鸽笼原理)

    题目链接  Points Inside A Polygon 题意  给定一个$n$个点的凸多边形,求出$[ \frac{n}{10}]\ $个凸多边形内的整点. 把$n$个点分成$4$类: 横坐标奇, ...

  5. 【线段树 泰勒展开】Codechef April Challenge 2018 Chef at the Food Fair

    第一次写泰勒展开:本地和CC差距好大 题目大意 大厨住的城市里办了一场美食节.一条街上开设了$N$个摊位,编号为$1∼N$.这天开始时,第$i$个摊位的食物会导致食物中毒的概率是$P_i$.在这一天中 ...

  6. Codechef October Challenge 2018 游记

    Codechef October Challenge 2018 游记 CHSERVE - Chef and Serves 题目大意: 乒乓球比赛中,双方每累计得两分就会交换一次发球权. 不过,大厨和小 ...

  7. Codechef September Challenge 2018 游记

    Codechef September Challenge 2018 游记 Magician versus Chef 题目大意: 有一排\(n(n\le10^5)\)个格子,一开始硬币在第\(x\)个格 ...

  8. [BZOJ 3514]Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES)

    [BZOJ3514] Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES) 题意 \(N\) 个点 \(M\) 条边的无向图,\(K\) 次询问保 ...

  9. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries

    https://www.codechef.com/DEC17/problems/CHEFEXQ 题意: 位置i的数改为k 询问区间[1,i]内有多少个前缀的异或和为k 分块 sum[i][j] 表示第 ...

随机推荐

  1. laravel5.5入门-安装和认证

    一.安装 在终端CMD里切换到你想要放置该网站的目录下(如 d:\project\laravel),运行命令 composer create-project laravel/laravel learn ...

  2. mysql优化--explain关键字

    MySQL性能优化---EXPLAIN 参见:https://blog.csdn.net/jiadajing267/article/details/81269067 参见:https://www.cn ...

  3. Python 常用库(随时补充)

    1. Python-RSA使用手册 英文文档见Python-RSA使用手册,主要介绍了Python-RSA的消息的加密解密.文件的加密解密以及签名的方法. Installation 使用pip ins ...

  4. apache traffic server安装

    wget http://mirrors.hust.edu.cn/apache/trafficserver/trafficserver-7.1.1.tar.bz2 tar -jxvf trafficse ...

  5. ubuntu 配置smb后无法访问

    配置如下 [/gscloud] path = /gscloud browseable = yes writable = yes guest ok = yes read only = no create ...

  6. Spring框架几种创建bean的方式

    Spring框架下,Bean的创建和装配非常的灵活,提供了三种主要的方式,并且相互见可以互相看见,也就是你可以随意地采用你喜欢且合适的方式创建Bean,而不用担心他们之间的兼容问题. 一.使用XML显 ...

  7. Spring的新注解

    1 @Configuration 1.1 作用 用于指定当前类是一个Spring的配置类,当创建容器的时候会从该类上加载注解.获取容器的时候需要使用AnnotationApplicationConte ...

  8. ks代码助解

    代码实现: data_test_2 = {'gd':[1,1,1,1,1,1,0,0,0,0,0,0,0],'score':[1,2,0,2,2,7,4,5,4,0,4,18,np.nan]} dat ...

  9. java 责任链接模式

  10. windows上部署hadoop(单机版)

    在window系统开发程序时,远程linux服务器上的hadoop速度很慢,影响开发效率,能不能在本地搭建hadoop环境的?答案肯定的,且看下文如何在window上部署hadoop: (源文地址:h ...