2016 ACM/ICPC Asia Regional Dalian Online

B Different GCD Subarray Query

题意:长度n的序列, m个询问区间[L, R], 问区间内的所有子段的不同GCD值有多少种.

Sample Input

5 3 1 3 4 6 9 3 5 2 5 1 5

Sample Output

6 6 6

分析:对于固定的r,预处理出每个点向左延伸 的 不同gcd值。扫一遍然后离线询问,对于每个询问按照右端点排序。找到每个位置的时候把这个位置向前看的所有GCD的位置更新到ans[]里,ans[]存的就是每个gcd最后出现的位置,然后对于右端点在当前位置的询问树状数组找一下ans[]在这个区间内的和就是答案了。

代码:

  

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;

#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair

typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 1e6+10, M = 1e2+11, mod = 1e9+7, inf = 2e9;

int n,q,a[N],ans[N];
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b);}
vector<pii> G[N];
struct QQ{
        int l,r,id;
        bool operator < (const QQ &a) const {
             return a.r > r;
        }
}Q[N];
int C[N],vis[N];
void update(int x,int c) {
        for(int i =x; i < N; i+=i&(-i)) C[i] += c;
}
int ask(int x) {
        int s =0 ;
        for(int i = x; i; i-= i & (-i))s += C[i];
        return s;
}
int main() {
        while(scanf("%d%d",&n,&q)!=EOF) {
            for(int i = 1; i <= n; ++i) scanf("%d",&a[i]);
            for(int i = 0; i <= n; ++i) G[i].clear();
            for(int i = 1; i <= n; ++i) {
                    int x = a[i];
                    int y = i;
                    for(int j = 0; j < G[i-1].size(); ++j) {
                        int res = gcd(x,G[i-1][j].first);
                        if(x != res) {
                                cout<<1<<" "<<x<<" "<<y<<endl;
                                G[i].push_back(MP(x,y));
                                x = res;
                                y = G[i-1][j].second;
                        }
                    }
                    cout<<2<<" "<<x<<" "<<y<<endl;
                    G[i].push_back(MP(x,y));
            }
//            测试,对于i这个右端点,得到的G[i][j].first为得到的GCD值,G[i][j].second为固定的左端点
            for(int i=1;i<=n;i++){
                for(int j=0;j<G[i-1].size();j++)
                {
                    cout<<G[i][j].first<<" "<<G[i][j].second;
                }
                cout<<endl;
            }
            memset(C,0,sizeof(C));
            memset(vis,0,sizeof(vis));
            for(int i = 1; i <= q; ++i) {scanf("%d%d",&Q[i].l,&Q[i].r),Q[i].id = i;}
            sort(Q+1,Q+q+1);
            for(int R = 0, i = 1; i <= q; ++i) {
                    while(R < Q[i].r) {
                        R++;
                        for(int j = 0; j < G[R].size(); ++j) {
                            int res = G[R][j].first;
                            int ids = G[R][j].second;
                            if(vis[res]) update(vis[res],-1);
                            vis[res] = ids;
                            update(vis[res],1);
                        }
                    }
                    ans[Q[i].id] = ask(R) - ask(Q[i].l-1);
            }
            for(int i = 1; i <= q; ++i) cout<<ans[i]<<endl;
        }
        return 0;
}

D:Number of Connected Subgraph

题目大意:

说一个岛上有m个人,n种石头,两个人见面要么是朋友要么是敌人,是朋友的要求是两个矮人的身上的项链上至少有一个石头的颜色相同,是敌人的要求就是两个矮人的身上没有石头颜色相同。问给出m,n的值,能否满足使得两个人见面要么是敌人要么是朋友的条件。

思路:

1、到今天的我读完这个题还是觉得只要n>=1就输出T就行了。其实题意隐晦成这个样子:就是让你将m个人分成两个部落,使得部落和部落之间都是朋友,部落和部落之间是敌人。

2、那么最坏的条件就是将m个人分成两个部落,每个部落m/2个人。那么满足两两之间见面都有关系,那么需要m/2*m/2条边,也就是需要m/2*m/2种石头。

按照上述判断一下即可。

#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
    long long int  n,m;
    while(~scanf("%I64d%I64d",&m,&n))
    {
        if(m*m/4>n)printf("F\n");
        else printf("T\n");
    }
}  
时间: 2024-10-13 08:35:55

2016 ACM/ICPC Asia Regional Dalian Online的相关文章

hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)

Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 208    Accepted Submission(s): 101 Problem Description You may not know this but it's a fact that Xinghai Square is

2016 ACM/ICPC Asia Regional Dalian Online 1002/HDU 5869

Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 681    Accepted Submission(s): 240 Problem Description This is a simple problem. The teacher gives Bob a list of prob

2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873

Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 439    Accepted Submission(s): 157 Problem Description A mysterious country will hold a football world championships---Abnormal Cup

HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)

Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 802    Accepted Submission(s): 309 Problem Description A mysterious country will hold a football world championships---Abnormal Cup

HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)

Friends and Enemies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 291    Accepted Submission(s): 160 Problem Description On an isolated island, lived some dwarves. A king (not a dwarf) ruled t

HDU 5785 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)

Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 976    Accepted Submission(s): 375 Problem Description The shorter, the simpler. With this problem, you should be convinced of this tru

2016 ACM/ICPC Asia Regional Dalian Online 1006 Football Games

题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5873 Problem Description A mysterious country will hold a football world championships---Abnormal Cup, attracting football teams and fans from all around the world. This country is so mysterious t

【组队训练】2016 ACM/ICPC Asia Regional Dalian Online

因为不是一队……毫无晋级的压力……反正有压力也进不去呵呵呵…… 开场zr看1006我看1010.. 1010我一直在wa... zr的1006倒是比较轻松的过了...然后我让他帮我看10.... 跟他讲了半天我代码的逻辑...然后我自己看出来的....比赛的代码....写的十分混乱.... #include <cstdio> #include <cstring> #include <algorithm> #include <vector> typedef l

2016 ACM/ICPC Asia Regional Dalian Online HDU 5877 Weak Pair treap + dfs序

Weak Pair Problem Description You are given a rooted tree of N nodes, labeled from 1 to N. To the ith node a non-negative value ai is assigned.An ordered pair of nodes (u,v) is said to be weak if  (1) u is an ancestor of v (Note: In this problem a no