zoukankan      html  css  js  c++  java
  • hdu 4638 Group 莫队算法

    题目链接

    很裸的莫队, 就不多说了...

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define pb(x) push_back(x)
     4 #define ll long long
     5 #define mk(x, y) make_pair(x, y)
     6 #define lson l, m, rt<<1
     7 #define mem(a) memset(a, 0, sizeof(a))
     8 #define rson m+1, r, rt<<1|1
     9 #define mem1(a) memset(a, -1, sizeof(a))
    10 #define mem2(a) memset(a, 0x3f, sizeof(a))
    11 #define rep(i, a, n) for(int i = a; i<n; i++)
    12 #define ull unsigned long long
    13 typedef pair<int, int> pll;
    14 const double PI = acos(-1.0);
    15 const double eps = 1e-8;
    16 const int mod = 1e9+7;
    17 const int inf = 1061109567;
    18 const int maxn = 1e5+5;
    19 int vis[maxn];
    20 int a[maxn], ans[maxn];
    21 
    22 struct node {
    23     int l, r, block, id;
    24     bool operator < (node x) const
    25     {
    26         if(block == x.block)
    27             return r < x.r;
    28         return block < x.block;
    29     }
    30 } q[maxn];
    31 int main() {
    32     int t, n, m;
    33     cin>>t;
    34     while (t--) {
    35         scanf("%d%d", &n, &m);
    36         int BLOCK = sqrt(n*1.0);
    37         for(int i=1; i<=n; i++)
    38             scanf("%d", &a[i]);
    39         for(int i=0; i<m; i++) {
    40             scanf("%d%d", &q[i].l, &q[i].r);
    41             q[i].block = q[i].l/BLOCK;
    42             q[i].id = i;
    43         }
    44         sort(q, q+m);
    45         mem(vis);
    46         int tmp = 0;
    47         int x = q[0].l, y = q[0].r;
    48         for(int i=x; i<=y; i++) {
    49             vis[a[i]] = 1;
    50             if (vis[a[i]-1] && vis[a[i]+1])
    51                 tmp--;
    52             else if (!vis[a[i]-1] && !vis[a[i]+1])
    53                 tmp++;
    54         }
    55         ans[q[0].id] = tmp;
    56         int L = x, R = y;
    57         for(int i=1; i<m; i++) {
    58             x = q[i].l, y = q[i].r;
    59             for (int i=x; i<L; i++) {
    60                 vis[a[i]] = 1;
    61                 if (vis[a[i]-1] && vis[a[i]+1])
    62                     tmp--;
    63                 else if (!vis[a[i]-1] && !vis[a[i]+1])
    64                     tmp++;
    65             }
    66             for (int i=R+1; i<=y; i++) {
    67                 vis[a[i]] = 1;
    68                 if (vis[a[i]-1] && vis[a[i]+1])
    69                     tmp--;
    70                 else if (!vis[a[i]-1] && !vis[a[i]+1])
    71                     tmp++;
    72             }
    73             for (int i=L; i<x; i++) {
    74                 vis[a[i]] = 0;
    75                 if (vis[a[i]-1] && vis[a[i]+1])
    76                     tmp++;
    77                 else if (!vis[a[i]-1] && !vis[a[i]+1])
    78                     tmp--;
    79             }
    80             for (int i=y+1; i<=R; i++) {
    81                 vis[a[i]] = 0;
    82                 if (vis[a[i]-1] && vis[a[i]+1])
    83                     tmp++;
    84                 else if (!vis[a[i]-1] && !vis[a[i]+1])
    85                     tmp--;
    86             }
    87             ans[q[i].id] = tmp;
    88             L = x, R = y;
    89         }
    90         for(int i=0; i<m; i++)
    91             printf("%d
    ", ans[i]);
    92     }
    93     return 0;
    94 }
  • 相关阅读:
    django调试思路
    在python中使用Mongodb
    python中Redis的简要介绍以及Redis的安装,配置
    python3.7中SQLAlchemy安装失败,报错Command errored out with exit status 1
    使用jinjia2时报错 (admin.E403) A ‘django.template.backends.django.DjangoTemplates’ instance must be configured in TEMPLATES in order to use the admin application.
    使用豆瓣API时报错invalid keyid
    Http常用状态码
    python开发工具ipython
    解析GenericOptionsParser
    Sql注入
  • 原文地址:https://www.cnblogs.com/yohaha/p/5071692.html
Copyright © 2011-2022 走看看