hdu 4638 Group

來源:互聯網
上載者:User

標籤:blog   http   os   io   for   art   ar   cti   

離線處理+樹狀數組

//http://blog.csdn.net/zz_1215/#pragma comment(linker, "/STACK:102400000,102400000")#include<iostream>#include<vector>#include<algorithm>#include<cstdio>#include<queue>#include<stack>#include<string>#include<map>#include<set>#include<cmath>#include<cassert>#include<cstring>#include<iomanip>#include<ctime>using namespace std;typedef long long i64;#define S64(a)          scanf(in64,&a)#define SS(a)           scanf("%d",&a)#define LL(a)           ((a)<<1)#define RR(a)           (((a)<<1)+1)#define pb              push_back#define pf              push_front#define X               first#define Y               second#define CL(Q)           while(!Q.empty())Q.pop()#define MM(name,what)   memset(name,what,sizeof(name))#define MC(a,b)memcpy(a,b,sizeof(b))#define MAX(a,b)        ((a)>(b)?(a):(b))#define MIN(a,b)        ((a)<(b)?(a):(b))#define read            freopen("in.txt","r",stdin)#define write           freopen("out.txt","w",stdout)const int inf = 0x3f3f3f3f;const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;const double oo = 10e9;const double eps = 10e-9;const double pi = acos(-1.0);const int maxn = 100000 + 111;struct zz{int l, r, id;bool operator < (const zz& cmp) const{return l < cmp.l;}};int n, m;int a[maxn];bool have[maxn];int pos[maxn];int ta[maxn];int ans[maxn];vector<zz>v;int lowbit(int x){return x & (-x);}void add(int pos, int num, int ary[]){while (pos <= n){ary[pos] += num;pos += lowbit(pos);}}int sum(int x, int ary[]){   // 1 to xint ans = 0;while (x){ans += ary[x];x -= lowbit(x);}return ans;}int query(int x, int y){return sum(y, ta) - sum(x - 1, ta);}inline int find(int x){if (!have[x - 1] && !have[x + 1]){return 1;}else if (have[x - 1] && have[x + 1]){return -1;}else{return 0;}}void start(){for (int i = 1; i <= n; i++){have[i] = false;ta[i] = 0;}for (int i = 1; i <= n; i++){pos[a[i]] = i;}for (int i = 1; i <= n; i++){add(i, find(a[i]), ta);have[a[i]] = true;}int temp = 0;for (int i = 1; i <= n; i++){while (temp <(int) v.size()){if (v[temp].l == i){ans[v[temp].id] = query(v[temp].l, v[temp].r);temp++;}else{break;}}have[a[i]] = false;if (have[a[i] + 1]){add(pos[a[i] + 1], 1, ta);}if (have[a[i] - 1]){add(pos[a[i] - 1], 1, ta);}}return;}int main(){int T;cin >> T;while (T--){cin >> n >> m;for (int i = 1; i <= n; i++){cin >> a[i];}zz temp;v.clear();for (int i = 1; i <= m; i++){//cin >> temp.l >> temp.r;SS(temp.l); SS(temp.r);temp.id = i;v.push_back(temp);}sort(v.begin(), v.end());start();for (int i = 1; i <= m; i++){//cout << ans[i] << endl;printf("%d\n", ans[i]);}}return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.