zoukankan      html  css  js  c++  java
  • 总结-莫队

    常用技巧

    • 奇偶优化
    • (frac{n}{sqrt{m}})

    普通莫队

    LOJ6164「美团 CodeM 初赛 Round A」数列互质

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define R(a,b,c) for(register int a = (b); a <= (c); ++a)
    #define nR(a,b,c) for(register int a = (b); a >= (c); --a)
    #define Fill(a,b) memset(a, b, sizeof(a))
    #define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
    
    #define ON_DEBUGG
    
    #ifdef ON_DEBUGG
    
    #define D_e_Line printf("
    -----------
    ")
    #define D_e(x) std::cout << (#x) << " : " <<x << "
    "
    #define FileOpen() freopen("in.txt", "r", stdin)
    #define FileSave() freopen("out.txt", "w", stdout)
    #define Pause() system("pause")
    #include <ctime>
    #define TIME() fprintf(stderr, "
    TIME : %.3lfms
    ", clock() * 1000.0 / CLOCKS_PER_SEC)
    
    #else
    
    #define D_e_Line ;
    #define D_e(x) ;
    #define FileOpen() ;
    #define FilSave ;
    #define Pause() ;
    #define TIME() ;
    
    #endif
    
    struct ios {
    	template<typename ATP> ios& operator >> (ATP &x) {
    		x = 0; int f = 1; char c;
    		for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
    		while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
    		x *= f;
    		return *this;
    	}
    }io;
    
    using namespace std;
    
    template<typename ATP> inline ATP Min(ATP a, ATP b) {
    	return a < b ? a : b;
    }
    template<typename ATP> inline ATP Max(ATP a, ATP b) {
    	return a > b ? a : b;
    }
    
    #include <assert.h>
    
    const int N = 50007;
    
    int n, m;
    
    struct Ques {
    	int l, r, K, pos, id;
    	bool operator < (const Ques &com) const {
    		return pos != com.pos ? l < com.l : (pos & 1) ? r < com.r : r > com.r;
    	}
    } q[N];
    
    int ans[N], a[N], tot[N], sum;
    int appear[N];
    int nxt[N], lst[N], st = -1;
    
    inline void Add(int x) {
    	if(x <= 0) return;
    	if(!tot[x]){
    		nxt[x] = st;
    		if(st != -1) lst[st] = x;
    		st = x, lst[x] = -1;
    	}
    	++tot[x];
    }
    
    inline void Del(int x) {
    	if(x <= 0) return;
    	if(--tot[x]) return;
    	assert(x >= 0);
    	if(lst[x] != -1) nxt[lst[x]] = nxt[x];
    	else st = nxt[x];
    	if(nxt[x] != -1) lst[nxt[x]] = lst[x];
    }
    
    inline void Modify(int x, int w) {
    	assert(a[x] >= 0);
    	assert(x >= 0);
    	Del(appear[a[x]]);
    	appear[a[x]] += w;
    	Add(appear[a[x]]);
    }
    
    inline int Gcd(int a, int b) {
    	while(b ^= a ^= b ^= a %= b);
    	return a;
    }
    
    int main() {
    //FileOpen();
    //FileSave();
    	io >> n >> m;
    	
    	R(i,1,n){
    		io >> a[i];
    	}
    	
    	int blockSize = (int)sqrt((double)(n + 0.5));
    	
    	R(i,1,m){
    		io >> q[i].l >> q[i].r >> q[i].K;
    		q[i].pos = (q[i].l - 1) / blockSize + 1;
    		q[i].id = i;
    	}
    	
    	sort(q + 1, q + m + 1);
    	
    	int l = 1, r = 0;
    	R(i,1,m){
    		while(q[i].l < l) Modify(--l, 1);
    		while(q[i].l > l) Modify(l++, -1);
    		while(q[i].r > r) Modify(++r, 1);
    		while(q[i].r < r) Modify(r--, -1);
    		int sum = 0;
    		for(register int j = st; ~j; j = nxt[j]){
    			if(Gcd(j, q[i].K) == 1){
    				assert(j >= 0);
    				sum += tot[j];
    			}
    		}
    		ans[q[i].id] = sum;
    	}
    	
    	R(i,1,m){
    		printf("%d
    ", ans[i]);
    	}
    	
    	return 0;
    }
    

    没看的了,咕了

  • 相关阅读:
    安装Python及pip
    关于软件测试培训
    终于也为自己开了技术博客
    全球地址联动js包2021最新
    约瑟夫斯问题
    添加二级域名
    mysql导出数据
    mysql导入数据
    shopify
    MySQL数据库简介及常用命令
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11675762.html
Copyright © 2011-2022 走看看